-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_windows.c
More file actions
745 lines (624 loc) · 21 KB
/
main_windows.c
File metadata and controls
745 lines (624 loc) · 21 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include "vidir.c"
#include "miniwin32.h"
// Additional types for UTF-8/UTF-16 handling
typedef i32 c32; // Use i32 directly instead of char32_t for compatibility
enum {
REPLACEMENT_CHARACTER = 0xfffd
};
typedef struct {
s8 tail;
c32 rune;
} utf8;
typedef struct {
c16 *s;
iz len;
} s16;
typedef struct {
s16 tail;
c32 rune;
} utf16;
static s8 cuthead(s8 s, iz off) {
assert(off <= s.len);
s.s += off;
s.len -= off;
return s;
}
static s16 s16cuthead_(s16 s, iz off) {
assert(off >= 0);
assert(off <= s.len);
s.s += off;
s.len -= off;
return s;
}
static utf8 utf8decode_(s8 s) {
assert(s.len);
utf8 r = {0};
switch (s.s[0]&0xf0) {
default : r.rune = s.s[0];
if (r.rune > 0x7f) break;
r.tail = cuthead(s, 1);
return r;
case 0xc0:
case 0xd0: if (s.len < 2) break;
if ((s.s[1]&0xc0) != 0x80) break;
r.rune = (c32)(s.s[0]&0x1f) << 6 |
(c32)(s.s[1]&0x3f) << 0;
if (r.rune < 0x80) break;
r.tail = cuthead(s, 2);
return r;
case 0xe0: if (s.len < 3) break;
if ((s.s[1]&0xc0) != 0x80) break;
if ((s.s[2]&0xc0) != 0x80) break;
r.rune = (c32)(s.s[0]&0x0f) << 12 |
(c32)(s.s[1]&0x3f) << 6 |
(c32)(s.s[2]&0x3f) << 0;
if (r.rune < 0x800) break;
if (r.rune>=0xd800 && r.rune<=0xdfff) break;
r.tail = cuthead(s, 3);
return r;
case 0xf0: if (s.len < 4) break;
if ((s.s[1]&0xc0) != 0x80) break;
if ((s.s[2]&0xc0) != 0x80) break;
if ((s.s[3]&0xc0) != 0x80) break;
r.rune = (c32)(s.s[0]&0x0f) << 18 |
(c32)(s.s[1]&0x3f) << 12 |
(c32)(s.s[2]&0x3f) << 6 |
(c32)(s.s[3]&0x3f) << 0;
if (r.rune < 0x10000) break;
if (r.rune > 0x10ffff) break;
r.tail = cuthead(s, 4);
return r;
}
r.rune = REPLACEMENT_CHARACTER;
r.tail = cuthead(s, 1);
return r;
}
static i32 utf16encode_(c16 *dst, c32 rune)
{
if (rune<0 || (rune>=0xd800 && rune<=0xdfff) || rune>0x10ffff) {
rune = REPLACEMENT_CHARACTER;
}
if (rune >= 0x10000) {
rune -= 0x10000;
dst[0] = (c16)((rune >> 10) + 0xd800);
dst[1] = (c16)((rune&0x3ff) + 0xdc00);
return 2;
}
dst[0] = (c16)rune;
return 1;
}
// Encode code point returning the output length (1-4).
static i32 utf8encode_(u8 *s, c32 rune)
{
if (rune<0 || (rune>=0xd800 && rune<=0xdfff) || rune>0x10ffff) {
rune = REPLACEMENT_CHARACTER;
}
switch ((rune >= 0x80) + (rune >= 0x800) + (rune >= 0x10000)) {
case 0: s[0] = (u8)(0x00 | ((rune >> 0) )); return 1;
case 1: s[0] = (u8)(0xc0 | ((rune >> 6) ));
s[1] = (u8)(0x80 | ((rune >> 0) & 63)); return 2;
case 2: s[0] = (u8)(0xe0 | ((rune >> 12) ));
s[1] = (u8)(0x80 | ((rune >> 6) & 63));
s[2] = (u8)(0x80 | ((rune >> 0) & 63)); return 3;
case 3: s[0] = (u8)(0xf0 | ((rune >> 18) ));
s[1] = (u8)(0x80 | ((rune >> 12) & 63));
s[2] = (u8)(0x80 | ((rune >> 6) & 63));
s[3] = (u8)(0x80 | ((rune >> 0) & 63)); return 4;
}
assert(0);
}
static utf16 utf16decode_(s16 s)
{
assert(s.len);
utf16 r = {0};
r.rune = s.s[0];
if (r.rune>=0xdc00 && r.rune<=0xdfff) {
goto reject; // unpaired low surrogate
} else if (r.rune>=0xd800 && r.rune<=0xdbff) {
if (s.len < 2) {
goto reject; // missing low surrogate
}
i32 hi = r.rune;
i32 lo = s.s[1];
if (lo<0xdc00 || lo>0xdfff) {
goto reject; // expected low surrogate
}
r.rune = 0x10000 + ((hi - 0xd800)<<10) + (lo - 0xdc00);
r.tail = s16cuthead_(s, 2);
return r;
}
r.tail = s16cuthead_(s, 1);
return r;
reject:
r.rune = REPLACEMENT_CHARACTER;
r.tail = s16cuthead_(s, 1);
return r;
}
static s16 towide_(arena *perm, s8 s)
{
iz len = 0;
utf8 state = {0};
state.tail = s;
while (state.tail.len) {
state = utf8decode_(state.tail);
c16 tmp[2];
len += utf16encode_(tmp, state.rune);
}
s16 w = {0};
w.s = new(perm, c16, len + 1); // +1 for null terminator
state.tail = s;
while (state.tail.len) {
state = utf8decode_(state.tail);
w.len += utf16encode_(w.s + w.len, state.rune);
}
w.s[w.len] = 0;
return w;
}
static s8 fromwide_(arena *perm, s16 w)
{
iz len = 0;
utf16 state = {0};
state.tail = w;
while (state.tail.len) {
state = utf16decode_(state.tail);
u8 tmp[4];
len += utf8encode_(tmp, state.rune);
}
s8 s = {0};
s.s = new(perm, u8, len);
state.tail = w;
while (state.tail.len) {
state = utf16decode_(state.tail);
s.len += utf8encode_(s.s+s.len, state.rune);
}
return s;
}
// For communication with os_write()
struct os {
// 4 handles: stdin stdout stderr tempfile
struct {
iptr h;
b32 isconsole;
b32 err;
} handles[4];
c16 *temp_file_path_w; // UTF-16 path to temp file
};
static arena newarena_(os *ctx, iz cap)
{
arena arena = {0};
arena.beg = VirtualAlloc(0, cap, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
if (!arena.beg) {
os_write(ctx, 2, S("vidir: failed to allocate memory\n"));
os_exit(ctx, 1);
}
arena.end = arena.beg + cap;
arena.ctx = ctx;
return arena;
}
// Get environment variable as UTF-16
static s16 fromenv_w(arena *perm, c16 *name)
{
i32 wlen = GetEnvironmentVariableW(name, 0, 0);
if (!wlen) {
s16 r = {0};
return r;
}
c16 *wbuf = new(perm, c16, wlen);
GetEnvironmentVariableW(name, wbuf, wlen);
return (s16){wbuf, wlen - 1}; // Exclude null terminator
}
static config *newconfig_(os *ctx)
{
arena perm = newarena_(ctx, 1<<26);
config *conf = new(&perm, config, 1);
conf->perm = perm;
c16 *cmdline = GetCommandLineW();
i32 argc = 0;
c16 **wargv = CommandLineToArgvW(cmdline, &argc);
if (argc > 1) {
// Skip argv[0], only pass actual arguments
conf->nargs = argc - 1;
conf->args = new(&perm, u8*, conf->nargs);
for (i32 i = 1; i < argc; i++) {
i32 wlen = 0;
while (wargv[i][wlen]) wlen++;
s16 wide = {wargv[i], wlen};
s8 utf8_arg = fromwide_(&perm, wide);
// Extend the allocation for null terminator
u8 *null_term = new(&perm, u8, 1);
*null_term = 0;
conf->args[i-1] = utf8_arg.s;
}
} else {
conf->nargs = 0;
conf->args = 0;
}
conf->perm = perm;
return conf;
}
// Buffer for converted UTF-16 output.
typedef struct {
c16 buf[256];
iptr handle;
i32 len;
b32 err;
} u16buf;
static void flushconsole_(u16buf *b)
{
if (!b->err && b->len) {
i32 written;
if (!WriteConsoleW(b->handle, b->buf, b->len, &written, 0) ||
written != b->len) {
b->err = 1;
}
}
b->len = 0;
}
static void os_write(os *ctx, i32 fd, s8 s)
{
assert(fd > 0 && fd <= 3);
if (ctx->handles[fd].err) return;
if (ctx->handles[fd].isconsole) {
u16buf b = {0};
b.handle = ctx->handles[fd].h;
utf8 state = {0};
state.tail = s;
while (state.tail.len) {
state = utf8decode_(state.tail);
if (b.len > countof(b.buf)-2) {
flushconsole_(&b);
}
b.len += utf16encode_(b.buf+b.len, state.rune);
}
flushconsole_(&b);
ctx->handles[fd].err = b.err;
} else {
i32 dummy;
ctx->handles[fd].err = !WriteFile(ctx->handles[fd].h, s.s, s.len, &dummy, 0);
}
}
static i32 os_read(os *ctx, i32 fd, u8 *buf, i32 len)
{
assert(fd >= 0 && fd <= 3);
if (ctx->handles[fd].err) return -1;
i32 bytesRead = 0;
if (!ReadFile(ctx->handles[fd].h, buf, len, &bytesRead, 0)) {
ctx->handles[fd].err = 1;
return -1;
}
return bytesRead;
}
static b32 os_path_is_dir(os *ctx, arena scratch, s8 path)
{
s16 wpath = towide_(&scratch, path);
i32 attr = GetFileAttributesW(wpath.s);
if (attr == -1) {
return 0; // Path doesn't exist or access denied
}
// Check if it's a directory
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
static b32 os_path_exists(os *ctx, arena scratch, s8 path)
{
s16 wpath = towide_(&scratch, path);
i32 attr = GetFileAttributesW(wpath.s);
return attr != -1; // Path exists (file or directory)
}
static s8node *os_list_dir(os *ctx, arena *perm, s8 path)
{
arena scratch = *perm;
s16 wbase = towide_(&scratch, path);
// Extend the allocation for search pattern "/*\0"
(void) new(&scratch, c16, 3);
// Append search pattern to UTF-16 string
if (wbase.len > 0 && wbase.s[wbase.len-1] != L'\\' && wbase.s[wbase.len-1] != L'/') {
wbase.s[wbase.len++] = L'/';
}
wbase.s[wbase.len++] = L'*';
wbase.s[wbase.len] = 0;
// Start directory search
finddata fd;
iptr handle = FindFirstFileW(wbase.s, &fd);
if (handle == INVALID_HANDLE_VALUE) {
return 0;
}
s8node *head = 0;
s8node **tail = &head;
do {
i32 name_len_w = 0;
while (fd.name[name_len_w]) name_len_w++;
// Skip . and .. entries
if ((name_len_w == 1 && fd.name[0] == '.') ||
(name_len_w == 2 && fd.name[0] == '.' && fd.name[1] == '.')) {
continue;
}
// Convert filename from UTF-16 to UTF-8
s16 wide_name = {fd.name, name_len_w};
s8 utf8_filename = fromwide_(perm, wide_name);
if (!utf8_filename.len) continue;
// Build full path string once and insert directly in linked list
iz separator_needed = (path.len > 0 && path.s[path.len-1] != '\\' && path.s[path.len-1] != '/') ? 1 : 0;
iz full_len = path.len + separator_needed + utf8_filename.len;
u8 *full_path = new(perm, u8, full_len);
// Build the full path string
iz pos = 0;
for (iz i = 0; i < path.len; i++) {
full_path[pos++] = path.s[i];
}
if (separator_needed) {
full_path[pos++] = '/';
}
for (iz i = 0; i < utf8_filename.len; i++) {
full_path[pos++] = utf8_filename.s[i];
}
// Insert directly in linked list
s8node *node = new(perm, s8node, 1);
node->str = (s8){full_path, full_len};
node->next = 0;
*tail = node;
tail = &node->next;
} while (FindNextFileW(handle, &fd));
FindClose(handle);
return head;
}
// Create a temp file, and open "file descriptor 3" with it
// NOTE: MAX_PATH hardcoded buffer uses here are okay.
// GetTempPathW documents:
// The maximum possible return value is MAX_PATH+1 (261).
// GetTempFileNameW documents:
// The string cannot be longer than MAX_PATH–14 characters or GetTempFileName will fail.
static void os_create_temp_file(os *ctx, arena *perm)
{
c16 temp_dir[261];
i32 temp_dir_len = GetTempPathW(260, temp_dir);
if (temp_dir_len == 0) {
os_write(ctx, 2, S("vidir: failed to get temp directory\n"));
os_exit(ctx, 1);
}
c16 temp_file[261];
if (!GetTempFileNameW(temp_dir, L"vdr", 0, temp_file)) {
os_write(ctx, 2, S("vidir: failed to create temp file name\n"));
os_exit(ctx, 1);
}
i32 path_len = 0;
while (temp_file[path_len]) path_len++;
ctx->temp_file_path_w = new(perm, c16, path_len + 1);
for (i32 i = 0; i <= path_len; i++) { // Include null terminator
ctx->temp_file_path_w[i] = temp_file[i];
}
// Open the temp file
ctx->handles[3].h = CreateFileW(
temp_file,
GENERIC_WRITE,
0, // No sharing
0, // Default security
CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY,
0
);
if (ctx->handles[3].h == INVALID_HANDLE_VALUE) {
os_write(ctx, 2, S("vidir: failed to open temp file for writing\n"));
os_exit(ctx, 1);
}
ctx->handles[3].isconsole = 0;
ctx->handles[3].err = 0;
}
// Close temp file so editor can open it
static void os_close_temp_file(os *ctx)
{
if (ctx->handles[3].h && ctx->handles[3].h != INVALID_HANDLE_VALUE) {
CloseHandle(ctx->handles[3].h);
ctx->handles[3].h = INVALID_HANDLE_VALUE;
}
}
// Open for reading
static void os_open_temp_file(os *ctx)
{
if (!ctx->temp_file_path_w) {
ctx->handles[3].err = 1;
return;
}
ctx->handles[3].h = CreateFileW(
ctx->temp_file_path_w,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_TEMPORARY,
0
);
if (ctx->handles[3].h == INVALID_HANDLE_VALUE) {
os_write(ctx, 2, S("vidir: failed to open temp file for reading\n"));
os_exit(ctx, 1);
}
ctx->handles[3].isconsole = 0;
ctx->handles[3].err = 0;
}
// Remove temp file from filesystem
static void os_remove_temp_file(os *ctx)
{
// Close the file first if it's still open
if (ctx->handles[3].h && ctx->handles[3].h != INVALID_HANDLE_VALUE) {
CloseHandle(ctx->handles[3].h);
ctx->handles[3].h = INVALID_HANDLE_VALUE;
}
if (ctx->temp_file_path_w) {
DeleteFileW(ctx->temp_file_path_w);
}
}
#ifndef DEFAULT_EDTIOR
#define DEFAULT_EDITOR L"notepad"
#endif
// Invoke editor on temp file using busybox sh -c
// This is okay for w64devkit, but might need adjustment in other environments.
static b32 os_invoke_editor(os *ctx, arena scratch)
{
if (!ctx->temp_file_path_w) {
assert(0 && "Implement proper error"); // No temp file to edit
}
// Check VISUAL first, then EDITOR, then fall back to default
s16 editor = fromenv_w(&scratch, L"VISUAL");
if (!editor.len) {
editor = fromenv_w(&scratch, L"EDITOR");
}
if (!editor.len) {
editor = (s16){ DEFAULT_EDITOR, countof(DEFAULT_EDITOR) - 1};
}
// Build command line: busybox sh -c "editor \"tempfile\""
i32 tempfile_len = 0;
while (ctx->temp_file_path_w[tempfile_len]) tempfile_len++;
i32 total_len = countof(L"busybox sh ") - 1 + // "busybox sh " (exclude null)
countof(L"-c ") - 1 + // "-c " (exclude null)
1 + // opening quote
editor.len + // editor name
1 + // space between editor and file
1 + // opening quote for tempfile
tempfile_len + // temp file path
1 + // closing quote for tempfile
1 + // closing quote for -c
1; // null terminator
c16 *cmdline = new(&scratch, c16, total_len);
i32 pos = 0;
// Copy "busybox sh "
c16 busybox[] = L"busybox sh ";
for (i32 i = 0; i < countof(busybox) - 1; i++) { // Exclude null terminator
cmdline[pos++] = busybox[i];
}
// Copy "-c "
cmdline[pos++] = L'-';
cmdline[pos++] = L'c';
cmdline[pos++] = L' ';
// Opening quote
cmdline[pos++] = L'"';
// Copy editor
for (i32 i = 0; i < editor.len; i++) {
cmdline[pos++] = editor.s[i];
}
// Space
cmdline[pos++] = L' ';
// Opening quote for temp file path
cmdline[pos++] = L'"';
// Copy temp file path, converting \ to /
for (i32 i = 0; i < tempfile_len; i++) {
if (ctx->temp_file_path_w[i] == L'\\') {
cmdline[pos++] = L'/';
} else {
cmdline[pos++] = ctx->temp_file_path_w[i];
}
}
// Closing quote for temp file path
cmdline[pos++] = L'"';
// Closing quote for -c argument
cmdline[pos++] = L'"';
// Null terminator
cmdline[pos] = 0;
startupinfo si = {0};
si.cb = sizeof(startupinfo);
processinfo pi = {0};
// Create the process
b32 success = CreateProcessW(
0, // Application name (use command line)
cmdline, // Command line
0, // Process security attributes
0, // Thread security attributes
0, // Inherit handles
0, // Creation flags
0, // Environment
0, // Current directory
&si, // Startup info
&pi // Process info
);
if (!success) {
return 0; // Failed to start process
}
// Wait for the process to complete
WaitForSingleObject(pi.hProcess, INFINITE);
// Get exit code
i32 exit_code = 0;
GetExitCodeProcess(pi.hProcess, &exit_code);
// Clean up handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exit_code == 0; // Return success if editor exited cleanly
}
// Delete a file or directory
static b32 os_delete_path(os *ctx, arena scratch, s8 path)
{
s16 wpath = towide_(&scratch, path);
// Check if it's a directory
i32 attr = GetFileAttributesW(wpath.s);
if (attr == -1) {
return 0; // File doesn't exist
}
if (attr & FILE_ATTRIBUTE_DIRECTORY) {
return RemoveDirectoryW(wpath.s) != 0;
} else {
return DeleteFileW(wpath.s) != 0;
}
}
// Create directories recursively (mkdir -p)
static b32 os_create_dir(os *ctx, arena scratch, s8 path)
{
s16 wpath = towide_(&scratch, path);
// Check if directory already exists
i32 attr = GetFileAttributesW(wpath.s);
if (attr != -1 && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
return 1; // Already exists and is a directory
}
// Create directories iteratively by null-terminating at each slash
for (i32 i = 0; i < wpath.len; i++) {
if (wpath.s[i] == L'\\' || wpath.s[i] == L'/') {
c16 saved = wpath.s[i];
wpath.s[i] = 0;
// Skip empty path components and drive letters
if (i > 0 && !(i == 2 && wpath.s[1] == L':')) {
attr = GetFileAttributesW(wpath.s);
if (attr == -1) {
// Directory doesn't exist, create it
if (!CreateDirectoryW(wpath.s, 0)) {
return 0; // Failed to create
}
} else if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
return 0; // Exists but is not a directory
}
}
wpath.s[i] = saved; // Restore the slash
}
}
// Create final directory if it doesn't exist
attr = GetFileAttributesW(wpath.s);
if (attr == -1) {
return CreateDirectoryW(wpath.s, 0) != 0;
}
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
// Rename/move a file or directory
static b32 os_rename_file(os *ctx, arena scratch, s8 src, s8 dst)
{
s16 wsrc = towide_(&scratch, src);
s16 wdst = towide_(&scratch, dst);
return MoveFileW(wsrc.s, wdst.s) != 0;
}
// Exit the program with the given exit code
static void os_exit(os *ctx, i32 code)
{
ExitProcess(code);
}
#if 1
__attribute((force_align_arg_pointer))
void mainCRTStartup(void) {
os ctx[1] = {0};
i32 dummy;
ctx->handles[0].h = GetStdHandle(STD_INPUT_HANDLE);
ctx->handles[0].isconsole = GetConsoleMode(ctx->handles[0].h, &dummy);
ctx->handles[1].h = GetStdHandle(STD_OUTPUT_HANDLE);
ctx->handles[1].isconsole = GetConsoleMode(ctx->handles[1].h, &dummy);
ctx->handles[2].h = GetStdHandle(STD_ERROR_HANDLE);
ctx->handles[2].isconsole = GetConsoleMode(ctx->handles[2].h, &dummy);
config *conf = newconfig_(ctx);
// Initialize temp file for fd 3
os_create_temp_file(ctx, &conf->perm);
vidir(conf);
ExitProcess(ctx->handles[1].err || ctx->handles[2].err);
}
#endif