-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.c
96 lines (83 loc) · 3.49 KB
/
type.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
#include <windows.h>
#define OUTPUT(out, wbuf, cbuf, pbcount) \
if (!WriteConsoleW((out), (wbuf), wcslen((wbuf)), (pbcount), NULL)) { \
WideCharToMultiByte(CP_OEMCP, 0, (wbuf), -1, (cbuf), sizeof (cbuf), NULL, NULL); \
WriteFile((out), (cbuf), strlen((cbuf)), (pbcount), NULL); \
} \
FlushFileBuffers((out))
int main() {
INT i, argcw;
CHAR cbuffer[8192];
WCHAR wbuffer[8192];
DWORD dwByteCount;
HANDLE hFile;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
LPWSTR *argvw = CommandLineToArgvW(GetCommandLineW(), &argcw);
/* No arguments */
if (argcw < 2) {
OUTPUT(hStdErr, L"The syntax of the command is incorrect.\r\n", cbuffer, &dwByteCount);
return 1;
}
/* /? switch */
for (i = 1; i < argcw; i++) {
if (argvw[i][0] == '/' && argvw[i][1] == '?') {
OUTPUT(hStdOut, L"Displays the contents of a text file or files.\r\n\r\n"
L"TYPE [drive:][path]filename\r\n", cbuffer, &dwByteCount);
return 1;
}
}
/* Bogus switches */
for (i = 1; i < argcw; i++) {
if (argvw[i][0] == '/') {
OUTPUT(hStdErr, L"The syntax of the command is incorrect.\r\n", cbuffer, &dwByteCount);
return 1;
}
}
/* Process files */
for (i = 1; i < argcw; i++) {
/* Attempt to open (wide-character name) file */
if ((hFile = CreateFileW(argvw[i],
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)
) == INVALID_HANDLE_VALUE) {
OUTPUT(hStdErr, L"The system cannot find the file specified.\r\n", cbuffer, &dwByteCount);
/* Specify file when multiple passed in */
if (argcw > 2) {
OUTPUT(hStdErr, L"Error occurred while processing: ", cbuffer, &dwByteCount);
OUTPUT(hStdErr, argvw[i], cbuffer, &dwByteCount);
OUTPUT(hStdErr, L".\r\n", cbuffer, &dwByteCount);
}
continue;
}
/* Display filename, and blank lines, when there's multiple files */
if (argcw > 2) {
OUTPUT(hStdErr, L"\r\n", cbuffer, &dwByteCount);
OUTPUT(hStdErr, argvw[i], cbuffer, &dwByteCount);
OUTPUT(hStdErr, L"\r\n\r\n\r\n", cbuffer, &dwByteCount);
}
/* Check byte-order mark for UTF-16 [little-endian] encoding */
ReadFile(hFile, wbuffer, sizeof (wchar_t), &dwByteCount, NULL);
/* UTF16le encoding ? */
if (wbuffer[0] == 0xFEFF) {
while ((ReadFile(hFile, wbuffer, sizeof wbuffer/2, &dwByteCount, NULL), dwByteCount)) {
OUTPUT(hStdOut, wbuffer, cbuffer, &dwByteCount);
}
/* ASCII encoding ? */
} else {
/* Rewind file pointer */
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
while((ReadFile(hFile, cbuffer, sizeof cbuffer, &dwByteCount, NULL)), dwByteCount) {
WriteFile(hStdOut, cbuffer, dwByteCount, NULL, NULL);
FlushFileBuffers(hStdOut);
}
}
/* Close file */
CloseHandle(hFile);
}
return 0;
}