1/* See LICENSE file for copyright and license details. */
2
3#include <fcntl.h>
4#include <string.h>
5#include <unistd.h>
6
7#include "util.h"
8
9static void
10usage(void)
11{
12 eprintf("usage: %s [-u] [file ...]\n", argv0);
13}
14
15// ?man cat: concatenate files and print to standard output
16// ?man arguments: file ...
17// ?man cat reads each file in sequence and writes it to standard output
18// ?man if no file is given, or a file is -, standard input is read
19int
20main(int argc, char *argv[])
21{
22 int fd, ret = 0;
23
24 ARGBEGIN
25 {
26 // ?man -u: specify u option
27 case 'u':
28 // ?man -u: ignored; accepted for posix compatibility; output is
29 // always unbuffered
30 break;
31 default:
32 usage();
33 }
34 ARGEND
35
36 if (!argc) {
37 if (concat(0, "<stdin>", 1, "<stdout>") < 0)
38 ret = 1;
39 } else {
40 for (; *argv; argc--, argv++) {
41 if (!strcmp(*argv, "-")) {
42 *argv = "<stdin>";
43 fd = 0;
44 } else if ((fd = open(*argv, O_RDONLY)) < 0) {
45 weprintf("open %s:", *argv);
46 ret = 1;
47 continue;
48 }
49 switch (concat(fd, *argv, 1, "<stdout>")) {
50 case -1:
51 ret = 1;
52 break;
53 case -2:
54 return 1; /* exit on write error */
55 }
56 if (fd != 0)
57 close(fd);
58 }
59 }
60
61 // ?man
62 // ?man ## Exit status
63 // ?man
64 // ?man cat exits 0 on success, and >0 if an error occurs reading any
65 // file ?man or writing to standard output ?man ?man ## See also ?man
66 // ?man cp(1), dd(1)
67 // ?man
68
69 return ret;
70}