2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS conCATenation tool
4 * FILE: cmdutils/cat/cat.c
5 * PURPOSE: Concatenates STDIN or an arbitrary number of files to STDOUT
6 * PROGRAMMERS: David Welch
7 * Semyon Novikov (tappak)
8 * Hermès Bélusca - Maïto
14 #include <string.h> // Required for _stricmp()
15 #include <fcntl.h> // Required for _setmode flags
16 #include <io.h> // Required for _setmode()
18 #include <strings.h> // Required for strcasecmp()
20 #define O_BINARY 0x8000
21 #define _setmode(fd, mode) // This function is useless in *nix world
22 #define _stricmp strcasecmp
25 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
31 "ReactOS File Concatenation Tool\n"
33 "Usage: cat [options] [file [...]]\n"
34 "options - Currently ignored\n");
37 int main(int argc
, char* argv
[])
41 unsigned char buff
[512];
46 if (_stricmp(argv
[1], "-h" ) == 0 ||
47 _stricmp(argv
[1], "--help") == 0 ||
48 _stricmp(argv
[1], "/?" ) == 0 ||
49 _stricmp(argv
[1], "/help" ) == 0)
56 /* Set STDOUT to binary */
57 _setmode(_fileno(stdout
), O_BINARY
);
59 /* Special case where we run 'cat' without any argument: we use STDIN */
64 /* Set STDIN to binary */
65 _setmode(_fileno(stdin
), O_BINARY
);
67 #if 0 // Version using feof()
75 while ((ch
= fgetc(stdin
)) != EOF
)
84 /* We have files: read them and output them to STDOUT */
85 for (i
= 1; i
< argc
; i
++)
87 /* Open the file in binary read mode */
88 in
= fopen(argv
[i
], "rb");
91 fprintf(stderr
, "Failed to open file '%s'\n", argv
[i
]);
95 /* Dump the file to STDOUT */
97 while (readcnt
== cnt
)
99 /* Read data from the input file */
100 cnt
= ARRAYSIZE(buff
);
101 readcnt
= fread(&buff
, sizeof(buff
[0]), cnt
, in
);
105 * The real number of read bytes differs from the number of bytes
106 * we wanted to read, so either a reading error occurred, or EOF
107 * was reached while reading. Bail out if it is a reading error.
111 fprintf(stderr
, "Error while reading file '%s'\n", argv
[i
]);
117 /* Nothing to be read anymore, so we can gracefully break */
118 if (readcnt
== 0) break;
120 /* Write data to STDOUT */
121 fwrite(&buff
, sizeof(buff
[0]), readcnt
, stdout
);
124 /* Finally close the file */