-
Notifications
You must be signed in to change notification settings - Fork 2
/
macos.h
470 lines (398 loc) · 8.67 KB
/
macos.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <stdlib.h>
#include <cpuid.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/syslimits.h>
#include <sys/utsname.h>
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CFURL.h>
#include <dlfcn.h>
#include <unistd.h>
const char *g_file_path_separator = "/";
b32 os_string_from_utf8(char *dst, size_t dstlen, const char *src)
{
strncpy(dst, src, dstlen);
return true;
}
size_t os_string_from_utf8_size(const char *src)
{
return strlen(src) + 1;
}
b32 os_string_to_utf8(char *dst, size_t dstlen, const char *src)
{
strncpy(dst, src, dstlen);
return true;
}
size_t os_string_to_utf8_size(const char *src)
{
return strlen(src) + 1;
}
const char *os_imstr_from_utf8(const char *str)
{
return str;
}
const char *os_imstr_to_utf8(const char *str)
{
return str;
}
b32 file_exists(const char *path)
{
struct stat s;
return stat(path, &s) == 0 && !S_ISDIR(s.st_mode);
}
s64 file_modified_time(const char *path)
{
struct stat s;
return stat(path, &s) == 0 ? s.st_mtime : 0;
}
b32 dir_exists(const char *path)
{
struct stat s;
return stat(path, &s) == 0 && S_ISDIR(s.st_mode);
}
void path_append(char *lhs, const char *rhs)
{
strcat(lhs, g_file_path_separator);
strcat(lhs, rhs);
}
void path_appendn(char *lhs, const char *rhs, u32 sz)
{
strcat(lhs, g_file_path_separator);
strncat(lhs, rhs, sz);
}
b32 mkpath(const char *path_)
{
if (dir_exists(path_))
return true;
char path[PATH_MAX];
if (strlen(path_) > sizeof(path))
return false;
if (path_[0] == '.' && path_[1] == '/')
strcpy(path, path_ + 2);
else
strcpy(path, path_);
if (strlen(path) == 0)
return true;
u32 pos = 0;
char c = 0;
for (char *p = path + 1; *p; ++p) {
if (*p == g_file_path_separator[0]) {
memswp(c, *p, char);
if (!dir_exists(path) && mkdir(path, S_IRWXU) != 0 && errno != EEXIST)
return false;
memswp(c, *p, char);
pos = 0;
} else {
++pos;
}
}
return mkdir(path, S_IRWXU) == 0;
}
#ifdef VLT_USE_TINYDIR
b32 rmdir_f(const char *path)
{
b32 success = false;
tinydir_dir dir;
if (tinydir_open(&dir, path) == -1) {
log_error("rmdir: error reading directory '%s'", path);
goto out;
}
while (dir.has_next) {
tinydir_file file;
if (tinydir_readfile(&dir, &file) != -1) {
if ( strcmp(file.name, ".") != 0
&& strcmp(file.name, "..") != 0) {
if (file.is_dir) {
rmdir_f(file.path);
} else if (remove(file.path)) {
log_error("rmdir: error removing '%s'", file.path);
goto out;
}
}
} else {
log_error("rmdir: error examining file");
goto out;
}
if (tinydir_next(&dir) == -1) {
log_error("rmdir: error iterating directory");
goto out;
}
}
if (rmdir(path))
log_error("error removing '%s'", path);
else
success = true;
out:
tinydir_close(&dir);
return success;
}
#endif // VLT_USE_TINYDIR
char *impathcat(char *imstr, const char *path)
{
return imstrcatn(imstrcatn(imstr, g_file_path_separator), path);
}
char *impathcatprintf(char *imstr, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
imstrcatprintfv(imstrcatn(imstr, g_file_path_separator), fmt, args);
va_end(args);
return imstr;
}
char *imappdir(void)
{
static thread_local char path[PATH_MAX] = {0};
if (path[0] != 0)
goto out;
CFBundleRef bundle = CFBundleGetMainBundle();
if (bundle == NULL)
goto out;
CFURLRef url = CFBundleCopyExecutableURL(bundle);
if (url == NULL)
goto out;
if (CFURLGetFileSystemRepresentation(url, true, (UInt8*)B2PC(path))) {
char *last = strrchr(path, '/');
if (last)
*last = 0;
else
path[0] = 0;
} else {
path[0] = 0;
}
CFRelease(url);
out:
return imstrcpy(path);
}
char *imapppath(const char *resource)
{
return impathcat(imappdir(), resource);
}
char *imresdir(void)
{
#ifdef MACOS_BUNDLE_ID
imstrcpy("");
CFBundleRef bundle = CFBundleGetMainBundle();
if (bundle == NULL)
goto out;
CFURLRef url = CFBundleCopyResourcesDirectoryURL(bundle);
if (url == NULL)
goto out;
if (!CFURLGetFileSystemRepresentation(url, true, (UInt8*)imstr(), IMPRINT_BUFFER_SIZE))
imstrcpy("");
CFRelease(url);
out:
return imstr();
#else
return imappdir();
#endif
}
char *imrespath(const char *resource)
{
#ifdef MACOS_BUNDLE_ID
imstrcpy("");
CFBundleRef bundle = CFBundleGetMainBundle();
if (bundle == NULL)
goto out;
CFStringRef str = CFStringCreateWithCString(NULL, resource, kCFStringEncodingUTF8);
CFURLRef url = CFBundleCopyResourceURL(bundle, str, NULL, NULL);
if (url == NULL)
goto err;
if (!CFURLGetFileSystemRepresentation(url, true, (UInt8*)imstr(), IMPRINT_BUFFER_SIZE))
imstrcpy("");
CFRelease(url);
err:
CFRelease(str);
out:
return imstr();
#else
return imapppath(resource);
#endif
}
char *imhomedir(void);
static
char *imuserdir(const char *bundle_dir, const char *nbundle_dir)
{
#ifdef MACOS_BUNDLE_ID
return impathcat(impathcat(impathcat(imhomedir(), "Library"), bundle_dir), MACOS_BUNDLE_ID);
#else
return imapppath(nbundle_dir);
#endif
}
char *imlogdir(void)
{
return imuserdir("Logs", ".logs");
}
char *imlogpath(const char *resource)
{
return impathcat(imlogdir(), resource);
}
char *imcachedir(void)
{
return imuserdir("Caches", ".cache");
}
char *imcachepath(const char *resource)
{
return impathcat(imcachedir(), resource);
}
char *imdatadir(void)
{
return imuserdir("Application Support", ".data");
}
char *imdatapath(const char *resource)
{
return impathcat(imdatadir(), resource);
}
b32 is_data_dir_in_use_by_another_instance(void)
{
static b32 logged_once = false; // don't spam logs with this warning
if (!logged_once) {
log_warn("is_data_dir_in_use_by_another_instance is not implemented for this platform");
logged_once = true;
}
return false;
}
char *imuserdatadir(void)
{
return imdatadir();
}
char *imuserdatapath(const char *resource)
{
return imdatapath(resource);
}
FILE *file_open(const char *fname, const char *mode)
{
return fopen(fname, mode);
}
void *file_read_all(const char *fname, const char *mode, size_t *sz, allocator_t *a)
{
FILE *fp;
size_t file_size;
void *bytes = NULL;
fp = file_open(fname, mode);
if (!fp)
return NULL;
if (fseek(fp, 0, SEEK_END) == -1)
goto out;
file_size = ftell(fp);
bytes = amalloc(file_size, a);
fseek(fp, 0, SEEK_SET);
if (fread(bytes, 1, file_size, fp) != file_size) {
afree(bytes, a);
bytes = NULL;
goto out;
}
if (sz)
*sz = file_size;
out:
fclose(fp);
return bytes;
}
/* Dynamic library */
#ifndef VIOLET_NO_LIB
lib_handle lib_load(const char *_filename)
{
const u32 sz = strlen(_filename);
char *filename = amalloc(sz + 4, g_temp_allocator);
strcpy(filename, _filename);
strcat(filename, ".so");
lib_handle hnd = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
afree(filename, g_temp_allocator);
return hnd;
}
void *lib_func(lib_handle hnd, const char *name)
{
return dlsym(hnd, name);
}
b32 lib_close(lib_handle hnd)
{
return dlclose(hnd) == 0;
}
const char *lib_err(void)
{
return dlerror();
}
#endif // VIOLET_NO_LIB
/* IO */
size_t vgetdelim(char **lineptr, size_t *n, int delim, FILE *stream, allocator_t *a)
{
if (!lineptr || !n || !stream) {
errno = EINVAL;
return -1;
}
if (!*lineptr) {
*n = 32;
*lineptr = amalloc(*n, a);
} else if (*n == 0) {
errno = EINVAL;
return -1;
}
int i = 0, c;
while ((c = fgetc(stream)) != EOF) {
if (i == *n-1) {
*n *= 2;
*lineptr = arealloc(*lineptr, *n, a);
}
(*lineptr)[i++] = c;
if (c == delim)
break;
}
if (ferror(stream) || feof(stream))
return -1;
(*lineptr)[i] = '\0';
return i;
}
size_t vgetline(char **lineptr, size_t *n, FILE *stream, allocator_t *a)
{
return vgetdelim(lineptr, n, '\n', stream, a);
}
/* Other applications */
void exec(char *const argv[])
{
execv(argv[0], argv);
}
b32 open_file_external(const char *filename)
{
b32 result;
str_t cmd = str_create(g_temp_allocator);
str_cpy(&cmd, "open ");
str_cat(&cmd, filename);
result = (system(cmd) == 0);
str_destroy(&cmd);
return result;
}
/* System */
s32 cpu_max_sse(void)
{
unsigned int info[4] = {0};
unsigned int max_function_id = __get_cpuid_max(0, NULL);
if (max_function_id < 1) {
log_error("failed to fetch highest cpuid functionid");
return OS_SSE_VERSION_UNKNOWN;
}
__get_cpuid(1, &info[0], &info[1], &info[2], &info[3]);
return cpu_max_sse_(info);
}
b32 cpu_supports_sse41(void)
{
return cpu_max_sse() >= OS_SSE_VERSION_41;
}
void cpu_vendor(char vendor[32])
{
unsigned int info[4] = {0};
__get_cpuid(0, &info[0], &info[1], &info[2], &info[3]);
cpu_vendor_(info, vendor);
}
os_utsname_t os_uname(void)
{
os_utsname_t os_utsname = {0};
struct utsname os ={0};
if (uname(&os) == 0) {
strbcpy(os_utsname.sysname, os.sysname);
strbcpy(os_utsname.release, os.release);
strbcpy(os_utsname.version, os.version);
strbcpy(os_utsname.machine, os.machine);
} else {
strbcpy(os_utsname.sysname, "MacOS");
}
return os_utsname;
}