-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathcat.c
More file actions
127 lines (112 loc) · 2.56 KB
/
cat.c
File metadata and controls
127 lines (112 loc) · 2.56 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
/*
* Output contents of a file.
*
* Compile this file with Visual Studio and run the produced command in
* console with a file name argument. For example, command
*
* cat include\dirent.h
*
* will output the dirent.h to screen.
*
* Copyright (C) 1998-2019 Toni Ronkko
* This file is part of dirent. Dirent may be freely distributed
* under the MIT license. For all details and documentation, see
* https://github.com/tronkko/dirent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <locale.h>
static void output_file(const char *fn);
static int _main(int argc, char *argv[]);
static int
_main(int argc, char *argv[])
{
/* Require at least one file */
if (argc == 1) {
fprintf(stderr, "Usage: cat filename(s)\n");
return EXIT_FAILURE;
}
/* For each file name argument in command line */
int i = 1;
while (i < argc) {
output_file(argv[i]);
i++;
}
return EXIT_SUCCESS;
}
/*
* Output file to screen
*/
static void
output_file(const char *fn)
{
/* Open file */
FILE *fp = fopen(fn, "r");
if (!fp) {
/* Could not open directory */
fprintf(stderr, "Cannot open %s (%s)\n", fn, strerror(errno));
exit(EXIT_FAILURE);
}
/* Output file to screen */
size_t n;
do {
/* Read some bytes from file */
char buffer[4096];
n = fread(buffer, 1, 4096, fp);
/* Output bytes to screen */
fwrite(buffer, 1, n, stdout);
} while (n != 0);
/* Close file */
fclose(fp);
}
/* Convert arguments to UTF-8 */
#ifdef _MSC_VER
int
wmain(int argc, wchar_t *argv[])
{
/* Select UTF-8 locale */
setlocale(LC_ALL, ".utf8");
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
/* Allocate memory for multi-byte argv table */
char **mbargv;
mbargv = (char**) malloc(argc * sizeof(char*));
if (!mbargv) {
puts("Out of memory");
exit(3);
}
/* Convert each argument to UTF-8 */
for (int i = 0; i < argc; i++) {
/* Compute the size of corresponding UTF-8 string */
size_t n;
wcstombs_s(&n, NULL, 0, argv[i], 0);
/* Allocate room for UTF-8 string */
mbargv[i] = (char*) malloc(n + 1);
if (!mbargv[i]) {
puts("Out of memory");
exit(3);
}
/* Convert ith argument to UTF-8 */
wcstombs_s(NULL, mbargv[i], n + 1, argv[i], n);
}
/* Pass UTF-8 arguments to the real main program */
int errorcode = _main(argc, mbargv);
/* Release UTF-8 arguments */
for (int i = 0; i < argc; i++) {
free(mbargv[i]);
}
/* Release the multi-byte argv table */
free(mbargv);
return errorcode;
}
#else
int
main(int argc, char *argv[])
{
return _main(argc, argv);
}
#endif