forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.cc
65 lines (50 loc) · 1.06 KB
/
os.cc
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
64
65
#ifndef _WIN32
#include "co/os.h"
#include <unistd.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
namespace os {
fastring env(const char* name) {
char* x = getenv(name);
return x ? fastring(x) : fastring();
}
fastring homedir() {
return os::env("HOME");
}
fastring cwd() {
char buf[4096];
char* s = getcwd(buf, 4096);
return s ? fastring(s) : fastring(1, '.');
}
fastring exename() {
fastring s = os::exepath();
return s.substr(s.rfind('/') + 1);
}
int pid() {
return (int) getpid();
}
int cpunum() {
static int ncpu = (int) sysconf(_SC_NPROCESSORS_ONLN);
return ncpu;
}
#ifdef __linux__
fastring exepath() {
char buf[4096] = { 0 };
int r = (int) readlink("/proc/self/exe", buf, 4096);
return r > 0 ? fastring(buf, r) : fastring();
}
void daemon() {
int r = ::daemon(1, 0); (void) r;
}
#else
fastring exepath() {
char buf[4096] = { 0 };
uint32_t size = sizeof(buf);
int r = _NSGetExecutablePath(buf, &size);
return r == 0 ? fastring(buf) : fastring();
}
void daemon() {}
#endif
} // os
#endif