-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcattee.c
131 lines (113 loc) · 2.95 KB
/
cattee.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
#include "arg.inl"
#define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
#define FILE_ERR ((FILE*) -1)
FILE* argfile(int i, FILE* def, const TCHAR* mode, const TCHAR* descvalue, const TCHAR* desc)
{
const TCHAR* name = argnumdesc(i, NULL, descvalue, desc);
if (name == NULL)
return NULL;
else if (strcmp(name, "-") == 0)
return def;
else
{
FILE* f = NULL;
errno_t e = _tfopen_s(&f, name, mode);
if (e != 0)
{
TCHAR err[1024];
strerror_s(err, ARRAYSIZE(err), e);
_ftprintf(stderr, _T("Error: %d (%s)\n"), e, err);
return FILE_ERR;
}
else
return f;
}
}
typedef struct
{
FILE** files;
int capacity;
int size;
} FileList;
void appendFileList(FileList* fl, FILE* nf)
{
if (fl->size == fl->capacity)
{
fl->capacity *= 2;
fl->files = realloc(fl->files, fl->capacity * sizeof(FILE*));
}
fl->files[fl->size++] = nf;
}
FileList argfilelist(int i, FILE* def, const TCHAR* mode, const TCHAR* descvalue, const TCHAR* desc)
{
FileList fl = {
malloc(1 * sizeof(FILE*)),
1,
0
};
FILE* nf = NULL;
while ((nf = argfile(i++, def, mode, descvalue, desc)) != NULL)
{
appendFileList(&fl, nf);
}
return fl;
}
BOOL fileListErr(const FileList* fl)
{
for (int i = 0; i < fl->size; ++i)
{
if (fl->files[i] == FILE_ERR)
return TRUE;
}
return FALSE;
}
int _tmain(int argc, const TCHAR* const argv[])
{
arginit(argc, argv, _T("Read and output a file to stdout"));
BOOL unicode = argswitchdesc(_T("/U"), _T("Use unicode mode"));
FILE* i = argfile(1, stdin, _T("rb"), _T("input"), _T("The file to read ('-' for stdin, 'CONIN$' for console)")); // TODO Should be a mandatory option
argoptional();
FileList o = argfilelist(2, stdout, _T("wb"), _T("output..."), _T("The file(s) to write ('-' for stdout, 'CONOUT$' for console)")); // TODO Should be an optional option (with many)
if (!argcleanup())
return EXIT_FAILURE;
if (argusage(i == NULL))
return EXIT_SUCCESS;
if (i == FILE_ERR || fileListErr(&o))
return EXIT_FAILURE;
if (o.size == 0)
appendFileList(&o, stdout);
if (unicode)
{
wint_t c;
while ((c = fgetwc(i)) != EOF)
{
for (int j = 0; j < o.size; ++j)
fputwc(c, o.files[j]);
}
}
else
{
int c;
while ((c = fgetc(i)) != EOF)
{
for (int j = 0; j < o.size; ++j)
fputc(c, o.files[j]);
}
}
if (i != stdin)
fclose(i);
for (int j = 0; j < o.size; ++j)
{
if (o.files[j] != stdout)
fclose(o.files[j]);
}
free(o.files);
return EXIT_SUCCESS;
}