-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtee.c
728 lines (621 loc) · 21.6 KB
/
tee.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
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
/*
* tee for Windows
* Copyright (c) 2024 "dEajL3kA" <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sub license, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: The above copyright notice and this
* permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
#include <ShellAPI.h>
#include <intrin.h>
#include <stdarg.h>
#include "include/cpu.h"
#include "include/version.h"
#pragma intrinsic(_InterlockedIncrement, _InterlockedDecrement)
#define BUFFER_SIZE (PROCESSOR_BITNESS * 128U)
#define BUFFERS 3U
#define MAX_THREADS MAXIMUM_WAIT_OBJECTS
// --------------------------------------------------------------------------
// Assertions
// --------------------------------------------------------------------------
#ifndef NDEBUG
#define ASSERT(CONDIATION, HANDLE_OUT, MESSAGE) do { \
static const wchar_t *const _message = L"[tee] Assertion Failed: " MESSAGE L"\n"; \
if (!(CONDIATION)) { \
write_text((HANDLE_OUT), _message); \
FatalExit(-1); \
} \
} while(0)
#else
#define ASSERT(CONDIATION, HANDLE_OUT, MESSAGE) ((void)0)
#endif
// --------------------------------------------------------------------------
// Utilities
// --------------------------------------------------------------------------
static wchar_t to_lower(const wchar_t c)
{
return ((c >= L'A') && (c <= L'Z')) ? (L'a' + (c - L'A')) : c;
}
static BOOL is_terminal(const HANDLE handle)
{
DWORD mode;
return GetConsoleMode(handle, &mode);
}
static DWORD count_handles(const HANDLE *const array, const size_t maximum)
{
DWORD counter;
for (counter = 0U; counter < maximum; ++counter)
{
if (!array[counter])
{
break;
}
}
return counter;
}
static const wchar_t *get_filename(const wchar_t *filePath)
{
for (const wchar_t *ptr = filePath; *ptr != L'\0'; ++ptr)
{
if ((*ptr == L'\\') || (*ptr == L'/'))
{
filePath = ptr + 1U;
}
}
return filePath;
}
static BOOL is_null_device(const wchar_t *filePath)
{
filePath = get_filename(filePath);
if ((to_lower(filePath[0U]) == L'n') && (to_lower(filePath[1U]) == L'u') && (to_lower(filePath[2U]) == L'l'))
{
return ((filePath[3U] == L'\0') || (filePath[3U] == L'.'));
}
return FALSE;
}
static wchar_t *format_string(const wchar_t *const format, ...)
{
wchar_t* buffer = NULL;
va_list ap;
va_start(ap, format);
const DWORD result = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, format, 0U, 0U, (LPWSTR)&buffer, 1U, &ap);
va_end(ap);
return result ? buffer : NULL;
}
static wchar_t *concat_va(const wchar_t *const first, ...)
{
const wchar_t *ptr;
va_list ap;
va_start(ap, first);
size_t len = 0U;
for (ptr = first; ptr != NULL; ptr = va_arg(ap, const wchar_t*))
{
len = lstrlenW(ptr);
}
va_end(ap);
wchar_t *const buffer = (wchar_t*)LocalAlloc(LPTR, sizeof(wchar_t) * (len + 1U));
if (buffer)
{
va_start(ap, first);
for (ptr = first; ptr != NULL; ptr = va_arg(ap, const wchar_t*))
{
lstrcatW(buffer, ptr);
}
va_end(ap);
}
return buffer;
}
#define CONCAT(...) concat_va(__VA_ARGS__, NULL)
#define VALID_HANDLE(HANDLE) (((HANDLE) != NULL) && ((HANDLE) != INVALID_HANDLE_VALUE))
#define FILL_ARRAY(ARRAY, VALUE) do \
{ \
for (size_t _index = 0U; _index < ARRAYSIZE(ARRAY); ++_index) \
{ \
ARRAY[_index] = (VALUE); \
} \
} \
while (0)
#define CLOSE_HANDLE(HANDLE) do \
{ \
if (VALID_HANDLE(HANDLE)) \
{ \
CloseHandle((HANDLE)); \
(HANDLE) = NULL; \
} \
} \
while (0)
// --------------------------------------------------------------------------
// Console CTRL+C handler
// --------------------------------------------------------------------------
static volatile BOOL g_stop = FALSE;
static BOOL WINAPI console_handler(const DWORD ctrlType)
{
switch (ctrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
g_stop = TRUE;
return TRUE;
default:
return FALSE;
}
}
// --------------------------------------------------------------------------
// Text output
// --------------------------------------------------------------------------
static char *utf16_to_utf8(const wchar_t *const input)
{
const int buff_size = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
if (buff_size > 0)
{
char *const buffer = (char*)LocalAlloc(LPTR, buff_size);
if (buffer)
{
const int result = WideCharToMultiByte(CP_UTF8, 0, input, -1, buffer, buff_size, NULL, NULL);
if ((result > 0) && (result <= buff_size))
{
return buffer;
}
LocalFree(buffer);
}
}
return NULL;
}
static BOOL write_text(const HANDLE handle, const wchar_t *const text)
{
BOOL result = FALSE;
DWORD written;
if (GetConsoleMode(handle, &written))
{
result = WriteConsoleW(handle, text, lstrlenW(text), &written, NULL);
}
else
{
char *const utf8_text = utf16_to_utf8(text);
if (utf8_text)
{
result = WriteFile(handle, utf8_text, lstrlenA(utf8_text), &written, NULL);
LocalFree(utf8_text);
}
}
return result;
}
#define WRITE_TEXT(...) do \
{ \
wchar_t* const _message = CONCAT(__VA_ARGS__); \
if (_message) \
{ \
write_text(hStdErr, _message); \
LocalFree(_message); \
} \
} \
while (0)
// --------------------------------------------------------------------------
// Condition variables
// --------------------------------------------------------------------------
static __forceinline void sleep_condvar_srw(const HANDLE hStdErr, const PCONDITION_VARIABLE condvar, const PSRWLOCK lock, const DWORD timeout, const BOOL sharedMode)
{
const ULONG flags = sharedMode ? CONDITION_VARIABLE_LOCKMODE_SHARED : 0U;
if (!SleepConditionVariableSRW(condvar, lock, timeout, flags))
{
if (GetLastError() != ERROR_TIMEOUT)
{
write_text(hStdErr, L"[tee] Operating system error: SleepConditionVariableSRW() has failed!\n");
TerminateProcess(GetCurrentProcess(), 1U);
}
}
}
// --------------------------------------------------------------------------
// Writer thread
// --------------------------------------------------------------------------
#define INCREMENT_INDEX(INDEX, FLAG) do \
{ \
if (++(INDEX) >= BUFFERS) \
{ \
(INDEX) = 0U; \
(FLAG) = (!(FLAG)); \
} \
} \
while (0)
typedef struct _thread
{
HANDLE hOutput, hError;
BOOL flush;
}
thread_t;
static BYTE g_buffer[BUFFERS][BUFFER_SIZE];
static DWORD g_bytesTotal[BUFFERS] = { 0U, 0U, 0U };
static volatile LONG g_pending[BUFFERS] = { 0L, 0L, 0L };
static SRWLOCK g_rwLocks[BUFFERS];
static CONDITION_VARIABLE g_condIsReady[BUFFERS], g_condAllDone[BUFFERS];
static DWORD WINAPI writer_thread_start_routine(const LPVOID lpThreadParameter)
{
DWORD bytesWritten = 0U, myIndex = 0U;
LONG pending = 0L;
BOOL myFlag = TRUE, writeErrors = FALSE;
PSRWLOCK rwLock = NULL;
const thread_t *const param = (const thread_t*)lpThreadParameter;
for (;;)
{
ASSERT(myIndex < BUFFERS, param->hError, L"Current buffer index is out of range!");
AcquireSRWLockShared(rwLock = &g_rwLocks[myIndex]);
pending = g_pending[myIndex];
while (!(myFlag ? (pending > 0L) : (pending < 0L)))
{
sleep_condvar_srw(param->hError, &g_condIsReady[myIndex], rwLock, INFINITE, TRUE);
pending = g_pending[myIndex];
}
const DWORD bytesTotal = g_bytesTotal[myIndex];
if (bytesTotal > BUFFER_SIZE)
{
ReleaseSRWLockShared(rwLock);
if (writeErrors)
{
write_text(param->hError, L"[tee] I/O error: Not all data could be written!\n");
}
return 0U;
}
for (DWORD offset = 0U; offset < bytesTotal; offset += bytesWritten)
{
const BOOL result = WriteFile(param->hOutput, g_buffer[myIndex] + offset, g_bytesTotal[myIndex] - offset, &bytesWritten, NULL);
if ((!result) || (!bytesWritten))
{
writeErrors = TRUE;
break;
}
}
ASSERT(g_pending > 0U, param->hError, L"Pending threads counter must be a positive value!");
pending = myFlag ? _InterlockedDecrement(&g_pending[myIndex]) : _InterlockedIncrement(&g_pending[myIndex]);
ReleaseSRWLockShared(rwLock);
if (!pending)
{
WakeConditionVariable(&g_condAllDone[myIndex]);
}
INCREMENT_INDEX(myIndex, myFlag);
if (param->flush)
{
FlushFileBuffers(param->hOutput);
}
}
}
// --------------------------------------------------------------------------
// Options
// --------------------------------------------------------------------------
typedef struct
{
BOOL append, buffer, delay, escape, flush, help, ignore, version;
}
options_t;
#define PARSE_OPTION(SHRT, NAME) do \
{ \
if ((lc == L##SHRT) || (name && (lstrcmpiW(name, L#NAME) == 0))) \
{ \
options->NAME = TRUE; \
return TRUE; \
} \
} \
while (0)
static BOOL parse_option(options_t *const options, const wchar_t c, const wchar_t *const name)
{
const wchar_t lc = to_lower(c);
PARSE_OPTION('a', append);
PARSE_OPTION('b', buffer);
PARSE_OPTION('d', delay);
PARSE_OPTION('e', escape);
PARSE_OPTION('f', flush);
PARSE_OPTION('h', help);
PARSE_OPTION('i', ignore);
PARSE_OPTION('v', version);
return FALSE;
}
static BOOL parse_argument(options_t *const options, const wchar_t *const argument)
{
if ((argument[0U] != L'-') || (argument[1U] == L'\0'))
{
return FALSE;
}
if (argument[1U] == L'-')
{
return (argument[2U] != L'\0') && parse_option(options, L'\0', argument + 2U);
}
else
{
for (const wchar_t* ptr = argument + 1U; *ptr != L'\0'; ++ptr)
{
if (!parse_option(options, *ptr, NULL))
{
return FALSE;
}
}
return TRUE;
}
}
// --------------------------------------------------------------------------
// Help screen
// --------------------------------------------------------------------------
static void print_helpscreen(const HANDLE hStdErr, const BOOL full)
{
wchar_t* const versionString = format_string(L"tee for Windows v%1!u!.%2!u!.%3!u! [%4!s!] [%5!s!]\n", APP_VERSION_MAJOR, APP_VERSION_MINOR, APP_VERSION_PATCH, PROCESSOR_ARCHITECTURE, TEXT(__DATE__));
write_text(hStdErr, versionString ? versionString : L"tee for Windows\n");
if (full)
{
write_text(hStdErr, L"\n"
L"Copy standard input to output file(s), and also to standard output.\n\n"
L"Usage:\n"
L" gizmo.exe [...] | tee.exe [options] <file_1> ... <file_n>\n\n"
L"Options:\n"
L" -a --append Append to the existing file, instead of truncating\n"
L" -b --buffer Enable write combining, i.e. buffer small chunks\n"
L" -e --escape Enable standard output ANSI escape code processing\n"
L" -f --flush Flush output file after each write operation\n"
L" -i --ignore Ignore the interrupt signal (SIGINT), e.g. CTRL+C\n"
L" -d --delay Add a small delay after each read operation\n\n");
}
if (versionString)
{
LocalFree(versionString);
}
}
// --------------------------------------------------------------------------
// MAIN
// --------------------------------------------------------------------------
int wmain(const int argc, const wchar_t *const argv[])
{
HANDLE hThreads[MAX_THREADS], hMyFiles[MAX_THREADS - 1U];
int exitCode = 1, argOff = 1;
BOOL myFlag = TRUE, readErrors = FALSE;
DWORD fileCount = 0U, threadCount = 0U, myIndex = 0U, bytesRead = 0U, totalBytes = 0U;
PSRWLOCK rwLock = NULL;
options_t options;
static thread_t threadData[MAX_THREADS];
/* Initialize local variables */
FILL_ARRAY(hMyFiles, INVALID_HANDLE_VALUE);
FILL_ARRAY(hThreads, NULL);
SecureZeroMemory(&options, sizeof(options));
SecureZeroMemory(&threadData, sizeof(threadData));
/* Initialize standard streams */
const HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE), hStdOut = GetStdHandle(STD_OUTPUT_HANDLE), hStdErr = GetStdHandle(STD_ERROR_HANDLE);
if (!(VALID_HANDLE(hStdIn) && VALID_HANDLE(hStdOut) && VALID_HANDLE(hStdErr)))
{
if (VALID_HANDLE(hStdErr))
{
write_text(hStdErr, L"[tee] Operating system error: GetStdHandle() has failed!\n");
}
return -1;
}
/* Initialize read/write locks and condition variables */
for (DWORD index = 0; index < BUFFERS; ++index)
{
InitializeSRWLock(&g_rwLocks[index]);
InitializeConditionVariable(&g_condIsReady[index]);
InitializeConditionVariable(&g_condAllDone[index]);
}
/* Set up CRTL+C handler */
SetConsoleCtrlHandler(console_handler, TRUE);
/* Parse command-line options */
while ((argOff < argc) && (argv[argOff][0U] == L'-') && (argv[argOff][1U] != L'\0'))
{
const wchar_t *const argValue= argv[argOff++];
if ((argValue[1U] == L'-') && (argValue[2U] == L'\0'))
{
break; /*stop!*/
}
else if (!parse_argument(&options, argValue))
{
WRITE_TEXT(L"[tee] Error: Invalid option \"", argValue, L"\" encountered!\n");
return 1;
}
}
/* Print manual page */
if (options.help || options.version)
{
print_helpscreen(hStdErr, options.help);
return 0;
}
/* Check output file name */
if (argOff >= argc)
{
write_text(hStdErr, L"[tee] Error: Output file name is missing. Type \"tee --help\" for details!\n");
return 1;
}
/* Determine input type */
const DWORD inputType = GetFileType(hStdIn);
if (inputType == FILE_TYPE_UNKNOWN)
{
if (GetLastError() != NO_ERROR)
{
write_text(hStdErr, L"[tee] Operating system error: GetFileType(hStdIn) has failed!\n");
return -1;
}
}
/* Enable ANSI escape code processing of stdout */
if (options.escape)
{
DWORD stdOutMode = 0U;
if (GetConsoleMode(hStdOut, &stdOutMode))
{
SetConsoleMode(hStdOut, stdOutMode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
}
/* Open output file(s) */
while ((argOff < argc) && (fileCount < ARRAYSIZE(hMyFiles)))
{
const wchar_t* const fileName = argv[argOff++];
if (!is_null_device(fileName))
{
const HANDLE hFile = CreateFileW(fileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, options.append ? OPEN_ALWAYS : CREATE_ALWAYS, 0U, NULL);
if ((hMyFiles[fileCount++] = hFile) == INVALID_HANDLE_VALUE)
{
WRITE_TEXT(L"[tee] Error: Failed to open the output file \"", fileName, L"\" for writing!\n");
goto cleanUp;
}
else if (options.append)
{
LARGE_INTEGER offset = { .QuadPart = 0LL };
if (!SetFilePointerEx(hFile, offset, NULL, FILE_END))
{
write_text(hStdErr, L"[tee] Error: Failed to move the file pointer to the end of the file!\n");
goto cleanUp;
}
}
}
}
/* Check output file name */
if (argOff < argc)
{
write_text(hStdErr, L"[tee] Warning: Too many input files, ignoring excess files!\n");
}
/* Determine number of outputs */
const DWORD outputCount = fileCount + 1U;
/* Start threads */
for (DWORD threadId = 0; threadId < outputCount; ++threadId)
{
threadData[threadId].hOutput = (threadId > 0U) ? hMyFiles[threadId - 1U] : hStdOut;
threadData[threadId].hError = hStdErr;
threadData[threadId].flush = options.flush && (!is_terminal(threadData[threadId].hOutput));
if (!(hThreads[threadCount++] = CreateThread(NULL, 0U, writer_thread_start_routine, (LPVOID)&threadData[threadId], 0U, NULL)))
{
write_text(hStdErr, L"[tee] Operating system error: CreateThread() has failed!\n");
goto cleanUp;
}
}
/* Determine minumum chunk size */
const DWORD minimumLength = options.buffer ? (BUFFER_SIZE / 8U) : 1U;
/* Process all input from STDIN stream */
do
{
ASSERT(myIndex < BUFFERS, hStdErr, L"Current buffer index is out of range!");
AcquireSRWLockExclusive(rwLock = &g_rwLocks[myIndex]);
while (g_pending[myIndex])
{
sleep_condvar_srw(hStdErr, &g_condAllDone[myIndex], rwLock, INFINITE, FALSE);
}
BYTE *const ptrBuffer = g_buffer[myIndex];
for (totalBytes = 0U; totalBytes < minimumLength; totalBytes += bytesRead)
{
if (!ReadFile(hStdIn, &ptrBuffer[totalBytes], BUFFER_SIZE - totalBytes, &bytesRead, NULL))
{
if (GetLastError() != ERROR_BROKEN_PIPE)
{
readErrors = TRUE;
}
break;
}
if ((!bytesRead) && (inputType != FILE_TYPE_PIPE))
{
break; /*pipes may return zero bytes, even when more data can become available later!*/
}
}
if (!totalBytes)
{
ReleaseSRWLockExclusive(rwLock);
break;
}
g_bytesTotal[myIndex] = totalBytes;
g_pending[myIndex] = myFlag ? ((LONG)threadCount) : (-((LONG)threadCount));
ReleaseSRWLockExclusive(&g_rwLocks[myIndex]);
WakeAllConditionVariable(&g_condIsReady[myIndex]);
INCREMENT_INDEX(myIndex, myFlag);
if (readErrors)
{
break; /*abort on previous read errors*/
}
if (options.delay)
{
Sleep(1U);
}
}
while ((!g_stop) || options.ignore);
/* Check for read errors */
if (readErrors)
{
write_text(hStdErr, L"[tee] I/O error: Failed to read input data!\n");
goto cleanUp;
}
exitCode = 0;
cleanUp:
/* Wait for the pending writes */
AcquireSRWLockExclusive(&g_rwLocks[myIndex]);
while (g_pending[myIndex])
{
sleep_condvar_srw(hStdErr, &g_condAllDone[myIndex], &g_rwLocks[myIndex], 25000U, FALSE);
}
/* Shut down the remaining worker threads */
g_bytesTotal[myIndex] = MAXDWORD;
g_pending[myIndex] = myFlag ? MAXLONG : MINLONG;
ReleaseSRWLockExclusive(&g_rwLocks[myIndex]);
WakeAllConditionVariable(&g_condIsReady[myIndex]);
/* Wait for worker threads to exit */
const DWORD pendingThreads = count_handles(hThreads, ARRAYSIZE(hThreads));
if (pendingThreads > 0U)
{
const DWORD result = WaitForMultipleObjects(pendingThreads, hThreads, TRUE, 10000U);
if (!((result >= WAIT_OBJECT_0) && (result < WAIT_OBJECT_0 + pendingThreads)))
{
for (DWORD threadId = 0U; threadId < pendingThreads; ++threadId)
{
if (WaitForSingleObject(hThreads[threadId], 125U) != WAIT_OBJECT_0)
{
write_text(hStdErr, L"[tee] Internal error: Worker thread did not exit cleanly!\n");
TerminateThread(hThreads[threadId], 1U);
}
}
}
}
/* Flush the output file */
if (options.flush)
{
for (size_t fileIndex = 0U; fileIndex < ARRAYSIZE(hMyFiles); ++fileIndex)
{
if (hMyFiles[fileIndex] != INVALID_HANDLE_VALUE)
{
FlushFileBuffers(hMyFiles[fileIndex]);
}
}
}
/* Close worker threads */
for (DWORD threadId = 0U; threadId < ARRAYSIZE(hThreads); ++threadId)
{
CLOSE_HANDLE(hThreads[threadId]);
}
/* Close the output file(s) */
for (size_t fileIndex = 0U; fileIndex < ARRAYSIZE(hMyFiles); ++fileIndex)
{
CLOSE_HANDLE(hMyFiles[fileIndex]);
}
/* Exit */
return exitCode;
}
// --------------------------------------------------------------------------
// CRT Startup
// --------------------------------------------------------------------------
#ifndef _DEBUG
#pragma warning(disable: 4702)
int _startup(void)
{
SetErrorMode(SEM_FAILCRITICALERRORS);
int nArgs;
LPWSTR *const szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (!szArglist)
{
OutputDebugStringA("[tee-win32] System error: Failed to initialize command-line arguments!\n");
ExitProcess((UINT)-1);
}
const int retval = wmain(nArgs, szArglist);
LocalFree(szArglist);
ExitProcess((UINT)retval);
return 0;
}
#endif