-
Notifications
You must be signed in to change notification settings - Fork 22
/
posix.c
68 lines (64 loc) · 1.44 KB
/
posix.c
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
#define _POSIX_SOURCE
#define _BSD_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <slash/platform.h>
char*
sl_realpath(sl_vm_t* vm, char* path)
{
char *cpath, *gcbuff;
if(path[0] != '/') {
gcbuff = sl_alloc_buffer(vm->arena, strlen(vm->cwd) + strlen(path) + 10);
strcpy(gcbuff, vm->cwd);
strcat(gcbuff, "/");
strcat(gcbuff, path);
path = gcbuff;
}
#ifdef PATH_MAX
cpath = sl_alloc_buffer(vm->arena, PATH_MAX + 1);
(void)realpath(path, cpath);
return cpath;
#else
cpath = realpath(path, NULL);
gcbuff = sl_alloc_buffer(vm->arena, strlen(cpath) + 1);
strcpy(gcbuff, cpath);
return gcbuff;
#endif
}
int
sl_file_exists(sl_vm_t* vm, char* path)
{
struct stat s;
return !stat(sl_realpath(vm, path), &s);
}
int sl_abs_file_exists(char* path)
{
struct stat s;
if(path[0] != '/') {
return 0;
}
return !stat(path, &s);
}
int sl_seed()
{
struct timeval a;
struct stat s;
FILE* f;
int seed;
if(!stat("/dev/urandom", &s)) {
f = fopen("/dev/urandom", "rb");
if(f) {
int success = fread(&seed, sizeof(int), 1, f);
fclose(f);
if(success) {
return seed;
}
}
}
gettimeofday(&a, NULL);
return (int)(a.tv_usec ^ a.tv_sec);
}