2 * cat.c - concatenate file(s) to standard output
5 * Build: c89 -o cat cat.c
6 * Source: <http://pdcore.sourceforge.net/>
7 * Spec: <http://www.opengroup.org/onlinepubs/9699919799/utilities/cat.html>
9 * This is free and unencumbered software released into the public domain,
10 * provided "as is", without warranty of any kind, express or implied. See the
11 * file UNLICENSE and the website <http://unlicense.org> for further details.
22 #include <sys/types.h>
25 #define USAGE "usage: cat [-u] [file ...]\n"
28 static void catfile(int fd, char *fn);
29 static void error(char *s);
31 static int exitstatus;
35 int main(int argc, char **argv)
37 extern int opterr, optind;
41 setlocale(LC_ALL, "");
44 while ((c = getopt(argc, argv, "u")) != -1)
51 fprintf(stderr, USAGE);
56 catfile(STDIN_FILENO, "stdin");
62 if (strcmp(fn, "-") == 0)
63 catfile(STDIN_FILENO, "stdin");
65 if ((fd = open(fn, O_RDONLY)) == -1)
79 void catfile(int fd, char *fn)
81 unsigned char buf[BUFSIZE];
84 while ((n = read(fd, buf, (optu ? 1 : BUFSIZE))) > 0)
85 if (write(STDOUT_FILENO, buf, (size_t)n) != n)
98 fprintf(stderr, "cat: %s: %s\n", s, strerror(errno));