commit 7dc4efc1556ba34065a86035fdc3670732c977af parent 6dad92f0fb8e6e46d524c40ae11afd85e7f2108a Author: z3bra <willyatmailoodotorg> Date: Sun Apr 24 19:24:45 2016 cat.c: read from stdin for filename "-" Diffstat: cat.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/cat.c b/cat.c @@ -13,13 +13,23 @@ main(int argc, char **argv) char buf[LINE_MAX]; while (*(++argv) != NULL) { - fd = open(*argv, O_RDONLY); + + /* if filename is "-", read file from stdin */ + if ((*argv)[0] == '-' && (*argv)[1] == 0) { + fd = 0; + /* else, open the filename given as argument */ + } else { + fd = open(*argv, O_RDONLY); + } + if (fd < 0) { perror(*argv); } else { + /* read file in chunks, and write them to stdout */ while ((len = read(fd, buf, LINE_MAX)) > 0) { write(1, buf, len); } + /* gracefully close file descriptor */ close(fd); } }