2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS conCATenation tool
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
17 #define O_BINARY 0x8000
18 #define setmode(fd, mode) // This function is useless in *nix world.
19 #define stricmp strcasecmp
22 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
28 "ReactOS File Concatenation Tool\n"
30 "Usage: cat [options] [file [...]]\n"
31 "options - Currently ignored\n");
34 int main(int argc
, char* argv
[])
38 unsigned char buff
[512];
43 if (stricmp(argv
[1], "-h" ) == 0 ||
44 stricmp(argv
[1], "--help") == 0 ||
45 stricmp(argv
[1], "/?" ) == 0 ||
46 stricmp(argv
[1], "/help" ) == 0)
53 /* Set STDOUT to binary */
54 setmode(fileno(stdout
), O_BINARY
);
56 /* Special case where we run 'cat' without any argument: we use STDIN */
61 /* Set STDIN to binary */
62 setmode(fileno(stdin
), O_BINARY
);
64 #if 0 // Version using feof()
72 while ((ch
= fgetc(stdin
)) != EOF
)
81 /* We have files: read them and output them to STDOUT */
82 for (i
= 1; i
< argc
; i
++)
84 /* Open the file in binary read mode */
85 in
= fopen(argv
[i
], "rb");
88 fprintf(stderr
, "Failed to open file '%s'\n", argv
[i
]);
92 /* Dump the file to STDOUT */
94 while (readcnt
== cnt
)
96 /* Read data from the input file */
97 cnt
= ARRAYSIZE(buff
);
98 readcnt
= fread(&buff
, sizeof(buff
[0]), cnt
, in
);
102 * The real number of read bytes differs from the number of bytes
103 * we wanted to read, so either a reading error occurred, or EOF
104 * was reached while reading. Bail out if it is a reading error.
108 fprintf(stderr
, "Error while reading file '%s'\n", argv
[i
]);
114 /* Nothing to be read anymore, so we can gracefully break */
115 if (readcnt
== 0) break;
117 /* Write data to STDOUT */
118 fwrite(&buff
, sizeof(buff
[0]), readcnt
, stdout
);
121 /* Finally close the file */