-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathstdlib_system.c
More file actions
117 lines (97 loc) · 2.59 KB
/
stdlib_system.c
File metadata and controls
117 lines (97 loc) · 2.59 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif /* ifdef _WIN32 */
// Returns the string describing the meaning of `errno` code (by calling `strerror`).
char* stdlib_strerror(size_t* len){
char* err = strerror(errno);
*len = strlen(err);
return err;
}
// Wrapper to the platform's `mkdir`(make directory) call.
// Uses `mkdir` on unix, `_mkdir` on windows.
// Returns 0 if successful, otherwise returns the `errno`.
int stdlib_make_directory(const char* path){
int code;
#ifdef _WIN32
code = _mkdir(path);
#else
// Default mode 0777
code = mkdir(path, 0777);
#endif /* ifdef _WIN32 */
return (!code) ? 0 : errno;
}
// Wrapper to the platform's `rmdir`(remove directory) call.
// Uses `rmdir` on unix, `_rmdir` on windows.
// Returns 0 if successful, otherwise returns the `errno`.
int stdlib_remove_directory(const char* path){
int code;
#ifdef _WIN32
code = _rmdir(path);
#else
code = rmdir(path);
#endif /* ifdef _WIN32 */
return (!code) ? 0 : errno;
}
// Wrapper to the platform's `getcwd`(get current working directory) call.
// Uses `getcwd` on unix, `_getcwd` on windows.
// Returns the cwd, sets the length of cwd and the `stat` of the operation.
char* stdlib_get_cwd(size_t* len, int* stat){
*stat = 0;
#ifdef _WIN32
char* buffer;
buffer = _getcwd(NULL, 0);
if (buffer == NULL) {
*stat = errno;
return NULL;
}
*len = strlen(buffer);
return buffer;
#else
char buffer[PATH_MAX + 1];
if (!getcwd(buffer, sizeof(buffer))) {
*stat = errno;
}
*len = strlen(buffer);
char* res = malloc(*len);
strncpy(res, buffer, *len);
return res;
#endif /* ifdef _WIN32 */
}
// Wrapper to the platform's `chdir`(change directory) call.
// Uses `chdir` on unix, `_chdir` on windows.
// Returns 0 if successful, otherwise returns the `errno`.
int stdlib_set_cwd(char* path) {
int code;
#ifdef _WIN32
code = _chdir(path);
#else
code = chdir(path);
#endif /* ifdef _WIN32 */
return (code == -1) ? errno : 0;
}
int stdlib_get_file_size(char* path, int64_t* size){
int code;
*size = 0;
#ifdef _WIN32
struct _stati64 buf = {0};
code = _stati64(path, &buf);
if (code == -1) return errno;
*size = (int64_t) buf.st_size;
#else
struct stat buf = {0};
code = stat(path, &buf);
if (code == -1) return errno;
*size = (int64_t) buf.st_size;
#endif /* ifdef _WIN32 */
return 0;
}