aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sys.c
blob: 8d895248c5bd9e78e49491286fdf78a33e9d1d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "sys.h"
#include <libgen.h>
#include <linux/limits.h>
#include <stdlib.h>
#include "std.h"

// TODO: #inclue some header.
#ifndef __ducc__
typedef long ssize_t;
#endif
ssize_t readlink(const char*, char*, size_t);

static char* get_self_path() {
    char* buf = calloc(PATH_MAX, sizeof(char));
    ssize_t len = readlink("/proc/self/exe", buf, PATH_MAX - 1);
    if (len == -1 || len == PATH_MAX - 1) {
        return NULL;
    }
    buf[len] = '\0';
    return buf;
}

// It returns a path not including final / except for root directory.
char* get_self_dir() {
    char* path = get_self_path();
    return dirname(path);
}