summaryrefslogtreecommitdiff
path: root/head.c
diff options
context:
space:
mode:
Diffstat (limited to 'head.c')
-rw-r--r--head.c26
1 files changed, 25 insertions, 1 deletions
diff --git a/head.c b/head.c
index b372a30..d5165e4 100644
--- a/head.c
+++ b/head.c
@@ -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;
}