-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.h
More file actions
57 lines (44 loc) · 1.01 KB
/
util.h
File metadata and controls
57 lines (44 loc) · 1.01 KB
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
#pragma once
#include <string>
#include <sstream>
#include <unistd.h>
#include <libgen.h>
#include <linux/limits.h>
#include <stdio.h>
#include <strings.h>
// Mac OSX / Unix specific implementations
inline bool compareNoCase(const char* arg1, const char* arg2)
{
return 0 == strcasecmp(arg1, arg2);
}
inline std::string getExeDir()
{
/*
Linux:
/proc/<pid>/exe
Solaris:
/proc/<pid>/object/a.out (filename only)
/proc/<pid>/path/a.out (complete pathname)
BSD:
/proc/<pid>/file
*/
pid_t pid = getpid();
char proc_link[256];
sprintf(proc_link,"/proc/%d/exe",pid);
char exe_path[PATH_MAX];
int len = readlink(proc_link, exe_path, sizeof(exe_path) - 1);
if(len > 0)
{
exe_path[len] = 0;
}
else
{
return std::string();
}
char * exe_dir = dirname(exe_path);
return std::string(exe_dir);
}
inline void deleteFile(const char* file)
{
remove(file);
}