/
shims.c
326 lines (292 loc) · 9.82 KB
/
shims.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
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
/*-*- vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi -*-│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 1fishe2fishe │
│ │
│ See end of file for extended copyright information and citations. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include <windows.h>
#include <wincon.h>
#include <winerror.h>
#include <direct.h> /* _mkdir */
#include <io.h> /* _chsize_s */
#include <memoryapi.h>
#include <processthreadsapi.h>
#include <profileapi.h>
#include <shlwapi.h>
/* mingw does not supply this (???) */
#ifndef ERROR_CONTROL_C_EXIT
#define ERROR_CONTROL_C_EXIT 0x23C
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /* _O_* */
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h> /* big brother Microsoft wants you to use stdio.h for SEEK_SET */
#include <stdlib.h>
#include "vendor/vec.h"
#include "ffi.h"
#include "misc.h"
#include "shims.h"
noret void terminate(int i) {
TerminateProcess(GetCurrentProcess(), i);
Unreachable();
}
i64 writefd(int fd, u8 *data, u64 sz) {
return _write(fd, data, sz);
}
i64 readfd(int fd, u8 *buf, u64 sz) {
return _read(fd, buf, sz);
}
int openfd(char const *path, bool rw) {
return open(path, rw ? (_O_RDWR | _O_CREAT) : _O_RDONLY,
_S_IREAD | _S_IWRITE);
}
void closefd(int fd) {
_close(fd);
}
bool fexists(char const *path) {
if (strchr(path, '*') || strchr(path, '?'))
return false;
return PathFileExistsA(path);
}
bool isdir(char const *path) {
if (strchr(path, '*') || strchr(path, '?'))
return false;
return !!PathIsDirectoryA(path);
}
/* Ugly because I manually flattened out the recursive function.
* SHFileOperation is too slow to be used here. */
struct stack {
int cur, size;
void *states[];
};
static struct stack *stacknew(void) {
struct stack *st = malloc(sizeof *st);
st->size = 0;
st->cur = -1;
return st;
}
static void stackpush(struct stack *restrict *sp, void *p) {
struct stack *s = *sp;
/* size is always 1 bigger than cur because indexing, so add 2 */
if (s->cur + 2 > s->size) {
s->size |= 1;
s->size *= 2;
s = realloc(s, sizeof(struct stack) + sizeof(void *) * s->size);
*sp = s;
}
s->cur++;
s->states[s->cur] = p;
}
static void *stackcur(struct stack *restrict s) {
return s->states[s->cur];
}
static void *stackpop(struct stack *restrict s) {
return s->states[s->cur--];
}
struct deleteall {
HANDLE fh;
char const *cwd;
char *strcur, *fnp; /* ptr to filename
↓
<path>/????? */
char findbuf[0x200], entbuf[0x200];
WIN32_FIND_DATAA data;
};
/* works in the same spirit as std::filesystem::remove_all */
void deleteall(char *s) {
if (!PathIsDirectoryA(s)) {
DeleteFileA(s);
return;
}
void *lbl = &&func;
struct stack *restrict stk = stacknew();
struct deleteall *restrict cur, *tmp;
func:
cur = malloc(sizeof *cur);
if (stk->cur == -1)
cur->cwd = s;
else {
tmp = stackcur(stk); // parent dir
cur->cwd = tmp->entbuf;
}
cur->data = (WIN32_FIND_DATAA){0};
cur->strcur = stpcpy2(cur->findbuf, cur->cwd);
strcpy(cur->strcur, "\\*.*");
cur->fh = FindFirstFileA(cur->findbuf, &cur->data);
if (veryunlikely(cur->fh == INVALID_HANDLE_VALUE))
goto endfunc;
cur->strcur = stpcpy2(cur->entbuf, cur->cwd);
do {
if (!strcmp(cur->data.cFileName, ".") || !strcmp(cur->data.cFileName, ".."))
continue;
cur->strcur = stpcpy2(cur->entbuf, cur->cwd);
cur->strcur = stpcpy2(cur->strcur, "\\");
strcpy(cur->strcur, cur->data.cFileName);
if (PathIsDirectoryA(cur->entbuf)) {
stackpush(&stk, cur);
lbl = &&dir;
goto func;
} else {
DeleteFileA(cur->entbuf);
}
dir:
} while (FindNextFileA(cur->fh, &cur->data));
FindClose(cur->fh);
RemoveDirectoryA(cur->cwd);
endfunc:
free(cur);
if (stk->cur == -1) {
free(stk);
return;
}
cur = stackpop(stk);
goto *lbl;
}
char **listdir(char const *path) {
vec_str_t ls;
WIN32_FIND_DATAA data;
char findbuf[0x200];
char *cur = stpcpy2(findbuf, path);
cur = stpcpy2(cur, "\\*.*");
/* FindFirstFileA/FindNextFileA also returns ./.., see above */
HANDLE fh = FindFirstFileA(findbuf, &data);
if (veryunlikely(fh == INVALID_HANDLE_VALUE))
return NULL;
vec_init(&ls);
do {
if (strlen(data.cFileName) <= 37)
vec_push(&ls, HolyStrDup(data.cFileName));
} while (FindNextFileA(fh, &data));
FindClose(fh);
vec_push(&ls, NULL);
u64 sz = ls.length * sizeof ls.data[0];
char **ret = memcpy(HolyMAlloc(sz), ls.data, sz);
vec_deinit(&ls);
return ret;
}
bool dirmk(char const *path) {
return !mkdir(path);
}
bool truncfile(char const *path, i64 sz) {
int fd = _open(path, _O_RDWR | _O_BINARY);
if (veryunlikely(fd == -1))
return false;
/* I'm not particularly fond of the fact that _chsize_s
* is one of those "safe" routines, but I have to use it since
* it's the one that supports 64-bit sizes */
bool ret = !_chsize_s(fd, sz);
_close(fd);
return ret;
}
i64 fsize(char const *path) {
if (!isdir(path)) {
struct _stati64 st;
if (veryunlikely(-1 == _stati64(path, &st)))
return -1;
return st.st_size;
} else {
WIN32_FIND_DATAA data;
char findbuf[0x200];
char *cur = stpcpy2(findbuf, path);
cur = stpcpy2(cur, "\\*.*");
HANDLE fh = FindFirstFileA(findbuf, &data);
if (veryunlikely(fh == INVALID_HANDLE_VALUE))
return -1;
i64 ret = 0;
do {
if (strlen(data.cFileName) <= 37)
++ret;
} while (FindNextFileA(fh, &data));
FindClose(fh);
return ret;
}
}
u64 unixtime(char const *path) {
struct _stati64 st;
_stati64(path, &st);
return st.st_mtime;
}
bool readfile(char const *path, u8 *buf, i64 sz) {
int fd = _open(path, _O_RDONLY | _O_BINARY);
bool ret = sz == _read(fd, buf, sz);
_close(fd);
return ret;
}
bool writefile(char const *path, u8 const *data, i64 sz) {
int fd = _open(path, _O_WRONLY | _O_TRUNC | _O_CREAT | _O_BINARY,
_S_IREAD | _S_IWRITE);
bool ret = sz == _write(fd, data, sz);
_close(fd);
return ret;
}
i64 getticksus(void) {
static LARGE_INTEGER freq;
if (veryunlikely(!freq.QuadPart)) {
QueryPerformanceFrequency(&freq);
freq.QuadPart /= INT64_C(1000000);
}
LARGE_INTEGER ticks;
QueryPerformanceCounter(&ticks);
return ticks.QuadPart / freq.QuadPart;
}
bool seekfd(int fd, i64 off) {
return -1 != _lseeki64(fd, off, SEEK_SET);
}
noret static BOOL WINAPI ctrlchndlr(argign DWORD dw) {
const char s[] = "User abort.\n";
write(2, s, sizeof s - 1);
terminate(ERROR_CONTROL_C_EXIT);
// return TRUE;
}
void handlectrlc(void) {
SetConsoleCtrlHandler(&ctrlchndlr, 1);
}
u64 mp_cnt(void) {
static u64 cnt;
if (veryunlikely(!cnt)) {
SYSTEM_INFO si;
GetSystemInfo(&si);
cnt = si.dwNumberOfProcessors;
cnt = Min(cnt, UINT64_C(128) /* MP_PROCESSORS_NUM */);
}
return cnt;
}
void unblocksigs(void) {}
bool isvalidptr(void *p) {
const DWORD mask = PAGE_READONLY //
| PAGE_READWRITE //
| PAGE_WRITECOPY //
| PAGE_EXECUTE //
| PAGE_EXECUTE_READ //
| PAGE_EXECUTE_READWRITE //
| PAGE_EXECUTE_WRITECOPY;
MEMORY_BASIC_INFORMATION mbi;
/* Thanks [1] */
if (VirtualQuery(p, &mbi, sizeof mbi))
return !!(mbi.Protect & mask);
return false;
}
/* CITATIONS:
* [1] https://stackoverflow.com/a/35576777 (https://archive.md/ehBq4)
*/
/*═════════════════════════════════════════════════════════════════════════════╡
│ EXODUS: Executable Divine Operating System in Userspace │
│ Copyright 2024 1fishe2fishe │
│ │
│ This file is part of EXODUS. │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/