Blame


1 e46639b3 2024-02-20 benni #include <string.h>
2 e46639b3 2024-02-20 benni #include <stdio.h>
3 e46639b3 2024-02-20 benni #include <err.h>
4 e46639b3 2024-02-20 benni
5 e46639b3 2024-02-20 benni static void cat (FILE* file) {
6 e46639b3 2024-02-20 benni int ch;
7 e46639b3 2024-02-20 benni while ((ch = fgetc(file)) != EOF)
8 e46639b3 2024-02-20 benni putchar(ch);
9 e46639b3 2024-02-20 benni }
10 e46639b3 2024-02-20 benni
11 e46639b3 2024-02-20 benni int main (int argc, char* argv[]) {
12 e46639b3 2024-02-20 benni FILE *file;
13 e46639b3 2024-02-20 benni int ec = 0;
14 e46639b3 2024-02-20 benni int ch;
15 e46639b3 2024-02-20 benni
16 e46639b3 2024-02-20 benni if (argc == 1) {
17 e46639b3 2024-02-20 benni cat (stdin);
18 e46639b3 2024-02-20 benni return 0;
19 e46639b3 2024-02-20 benni }
20 e46639b3 2024-02-20 benni
21 e46639b3 2024-02-20 benni for (int i = 1; i < argc; ++i) {
22 e46639b3 2024-02-20 benni if (strcmp (argv[i], "-") == 0) {
23 e46639b3 2024-02-20 benni file = stdin;
24 e46639b3 2024-02-20 benni } else {
25 e46639b3 2024-02-20 benni file = fopen (argv[i], "r");
26 e46639b3 2024-02-20 benni }
27 e46639b3 2024-02-20 benni
28 e46639b3 2024-02-20 benni if (file == NULL) {
29 e46639b3 2024-02-20 benni warn ("%s", argv[i]);
30 e46639b3 2024-02-20 benni ec = 1;
31 e46639b3 2024-02-20 benni continue;
32 e46639b3 2024-02-20 benni }
33 e46639b3 2024-02-20 benni
34 e46639b3 2024-02-20 benni cat (file);
35 e46639b3 2024-02-20 benni
36 cc17802d 2024-06-10 benni if (file != stdin)
37 cc17802d 2024-06-10 benni fclose (file);
38 e46639b3 2024-02-20 benni }
39 e46639b3 2024-02-20 benni return ec;
40 e46639b3 2024-02-20 benni }
41 e46639b3 2024-02-20 benni