This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
wbDirectory.cpp
88 lines (80 loc) · 1.98 KB
/
wbDirectory.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "wb.h"
#ifndef PATH_MAX
#ifdef FILENAME_MAX
#define PATH_MAX FILENAME_MAX
#else /* FILENAME_MAX */
#define PATH_MAX 4096
#endif /* FILENAME_MAX */
#endif /* PATH_MAX */
#ifdef WB_USE_UNIX
const char wbDirectorySeperator = '/';
static char *getcwd_(char *buf, int maxLen) {
return getcwd(buf, maxLen);
}
static void mkdir_(const char *dir) {
mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
#else /* WB_USE_LINUX */
const char wbDirectorySeperator = '\\';
static char *getcwd_(char *buf, int maxLen) {
return _getcwd(buf, maxLen);
}
static void mkdir_(const char *dir) {
_mkdir(dir);
}
#endif /* WB_USE_LINUX */
EXTERN_C const char *wbDirectory_create(const char *dir) {
char tmp[PATH_MAX];
char *p = nullptr;
size_t len;
#ifdef WB_USE_WINDOWS
_snprintf(tmp, sizeof(tmp), "%s", dir);
#else /* WB_USE_WINDOWS */
snprintf(tmp, sizeof(tmp), "%s", dir);
#endif /* WB_USE_WINDOWS */
len = strlen(tmp);
if (tmp[len - 1] == wbDirectorySeperator) {
tmp[len - 1] = 0;
}
for (p = tmp + 1; *p; p++) {
if (*p == wbDirectorySeperator) {
*p = 0;
mkdir_(tmp);
*p = wbDirectorySeperator;
}
}
mkdir_(tmp);
return dir;
}
EXTERN_C char *wbDirectory_name(const char *pth0) {
char *pth = wbString_duplicate(pth0);
char *p = strrchr(pth, wbDirectorySeperator);
if (p) {
p[0] = 0;
}
return pth;
}
EXTERN_C char *wbDirectory_current() {
char *tmp = wbNewArray(char, PATH_MAX + 1);
if (getcwd_(tmp, PATH_MAX)) {
return tmp;
}
wbDelete(tmp);
int error = errno;
switch (error) {
case EACCES:
std::cerr
<< "Cannot get current directory :: access denied. exiting..."
<< std::endl;
exit(-1);
case ENOMEM:
std::cerr << "Cannot get current directory :: insufficient storage. "
"exiting..."
<< std::endl;
exit(-1);
default:
std::cerr << "Cannot get current directory :: unrecognised error "
<< error << std::endl;
exit(-1);
}
}