diff options
author | Aki <please@ignore.pl> | 2022-06-16 17:37:19 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-06-16 17:37:19 +0200 |
commit | 05ba7e71cbbbf49db8ed95b2841f64ce3e4b3139 (patch) | |
tree | 2b8dc9636cd311505bf3116be0f452fd14f388c9 | |
parent | 59506e56f3c1a1d95e0d465cb43d466c5e7ba49e (diff) | |
download | coreutils-05ba7e71cbbbf49db8ed95b2841f64ce3e4b3139.zip coreutils-05ba7e71cbbbf49db8ed95b2841f64ce3e4b3139.tar.gz coreutils-05ba7e71cbbbf49db8ed95b2841f64ce3e4b3139.tar.bz2 |
Head now handles multiple arguments
-rw-r--r-- | head.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -1,4 +1,5 @@ #include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -65,5 +66,28 @@ int main(int argc, char * argv[]) return 1; } } - (void) head(0, lines, "<stdin>"); + 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; } |