pnd release 3.
[ginge.git] / common / cmn.c
CommitLineData
4d045184 1// vim:shiftwidth=2:expandtab
3adc9ccb 2#ifdef LOADER
3#include "../loader/realfuncs.h"
4#endif
5#include <stdio.h>
6#include <string.h>
7#include <unistd.h>
8
9#include "cmn.h"
10
11int make_local_path(char *buf, size_t size, const char *file)
12{
13 ssize_t ret;
14 char *p;
15
4d045184 16 p = getenv("GINGE_ROOT");
17 if (p != NULL) {
18 strncpy(buf, p, size);
19 buf[size - 1] = 0;
20 p = buf + strlen(buf);
3adc9ccb 21 }
4d045184 22 else {
23 ret = readlink("/proc/self/exe", buf, size - 1);
24 if (ret < 0) {
25 perror("readlink");
26 goto err;
27 }
28 buf[ret] = 0;
3adc9ccb 29
4d045184 30 p = strrchr(buf, '/');
3adc9ccb 31 if (p == NULL)
32 goto err;
4d045184 33 p++;
3adc9ccb 34 }
35
36 snprintf(p, size - (p - buf), "%s", file);
37
38 return 0;
39
40err:
41 fprintf(stderr, "ginge: can't determine root, buf: \"%s\"\n", buf);
42 return -1;
43}
44