From 05ba7e71cbbbf49db8ed95b2841f64ce3e4b3139 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 16 Jun 2022 17:37:19 +0200 Subject: Head now handles multiple arguments --- head.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/head.c b/head.c index b372a30..d5165e4 100644 --- a/head.c +++ b/head.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -65,5 +66,28 @@ int main(int argc, char * argv[]) return 1; } } - (void) head(0, lines, ""); + const unsigned int file_count = argc - optind; + int res = 0; + if (optind >= argc) + res |= head(0, lines, "-"); + else for (int i = optind; i < argc; ++i) { + if (1 < file_count) + printf("==> %s <==\n", argv[i]); + const unsigned int is_stdin = '-' == argv[i][0] && 0 == argv[i][1]; + const int fd = is_stdin ? 0 : open(argv[i], O_RDONLY); + if (-1 == fd) { + perror(argv[i]); + res = 1; + continue; + } + res |= head(fd, lines, argv[i]); + if (!is_stdin) { + const int clr = close(fd); + if (-1 == clr) { + perror(argv[i]); + res = 1; + } + } + } + return res; } -- cgit v1.1