1 /* `cat.c` - con*cat*enate and print files
2 Copyright (c) 2022, Alan Potteiger
3 See `LICENSE` for copyright and license details */
5 #define _POSIX_C_SOURCE 200809L
13 /* usage: cat [-u] [file...] */
16 main(int argc
, char *argv
[])
29 if (argv
[0][0] == '-' && argv
[0][1] == 'u') {
31 /* disable buffering for stdout, if the request cannot be
32 honored we'll print a brief message to stderr and continue
34 if (setvbuf(stdout
, NULL
, _IONBF
, 0) == EOF
)
35 fputs("cat: -u: unbuffer request failed\n", stderr
);
40 /* interpret remaining arguments as files to be read and printed */
41 for (; argc
!= 0; argc
-- && argv
++) {
45 in
= fopen(argv
[0], "r");
48 fprintf(stderr
, "cat: %s: %s\n", argv
[0],
53 /* disable buffering for our input stream, if the request
54 cannot be honored we'll print a brief message to stderr and
55 continue regular execution */
57 if (setvbuf(in
, NULL
, _IONBF
, 0) == EOF
)
58 fputs("cat: -u: unbuffer request failed\n",
62 while ((ch
= fgetc(in
)) != EOF
)
72 while ((ch
= getchar()) != EOF
)