-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
63 lines (56 loc) · 1.2 KB
/
path.c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include "path.h"
bool fileExists(char *path) {
struct stat statbuf;
if(path == 0) return false;
if(stat(path, &statbuf) != 0)
return false;
return true;
}
char *getFilename(char *path) {
if(path == 0) return 0;
char *filename = strrchr(path, '/');
if(filename == 0)
return path;
filename++;
return filename;
}
void getExePath(char *arg, char *buf, size_t bufLen) {
if(buf == 0 || bufLen == 0) return;
buf[0] = 0;
if(fileExists("/proc/self/exe")) {
int len = readlink("/proc/self/exe", buf, bufLen - 1);
if(len != -1) {
buf[len] = 0;
return;
}
}
if(fileExists("/proc/curproc/file")) {
int len = readlink("/proc/curproc/file", buf, bufLen - 1);
if(len != -1) {
buf[len] = 0;
return;
}
}
if(fileExists("/proc/self/path/a.out")) {
int len = readlink("/proc/self/path/a.out", buf, bufLen - 1);
if(len != -1) {
buf[len] = 0;
return;
}
}
strncpy(buf, arg, bufLen);
}
void getDefaultPath(char *arg, char *buf, size_t bufLen) {
if(buf == 0 || bufLen == 0) return;
getExePath(arg, buf, bufLen);
char *exe = strrchr(buf, '/');
if(exe) {
*exe = 0;
} else {
getcwd(buf, bufLen);
}
}