aboutsummaryrefslogtreecommitdiff
path: root/coreutils/head.c
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2000-09-27 04:09:22 +0000
committerMatt Kraai <kraai@debian.org>2000-09-27 04:09:22 +0000
commitc0321f9bc67e0a90dbd12f4d7b39d6991c9899cd (patch)
tree119d96c575fe50db7026ee96d476006823dd0f4b /coreutils/head.c
parente7c1af1e0dc1440483d7f195f15eb0df5cf70a04 (diff)
downloadbusybox-w32-c0321f9bc67e0a90dbd12f4d7b39d6991c9899cd.tar.gz
busybox-w32-c0321f9bc67e0a90dbd12f4d7b39d6991c9899cd.tar.bz2
busybox-w32-c0321f9bc67e0a90dbd12f4d7b39d6991c9899cd.zip
Rewrote head to perservere when it can't open a file, and share code
with cat.
Diffstat (limited to 'coreutils/head.c')
-rw-r--r--coreutils/head.c93
1 files changed, 43 insertions, 50 deletions
diff --git a/coreutils/head.c b/coreutils/head.c
index c232e1153..92b43bae2 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -26,17 +26,16 @@
26#include <errno.h> 26#include <errno.h>
27#include <stdio.h> 27#include <stdio.h>
28 28
29int head(int len, FILE * src) 29int head(int len, FILE *fp)
30{ 30{
31 int i; 31 int i;
32 char buffer[1024]; 32 char *input;
33 33
34 for (i = 0; i < len; i++) { 34 for (i = 0; i < len; i++) {
35 fgets(buffer, 1024, src); 35 if ((input = get_line_from_file(fp)) == NULL)
36 if (feof(src)) {
37 break; 36 break;
38 } 37 fputs(input, stdout);
39 fputs(buffer, stdout); 38 free(input);
40 } 39 }
41 return 0; 40 return 0;
42} 41}
@@ -44,60 +43,54 @@ int head(int len, FILE * src)
44/* BusyBoxed head(1) */ 43/* BusyBoxed head(1) */
45int head_main(int argc, char **argv) 44int head_main(int argc, char **argv)
46{ 45{
47 char opt; 46 FILE *fp;
48 int len = 10, tmplen, i; 47 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
49 48
50 /* parse argv[] */ 49 /* parse argv[] */
51 for (i = 1; i < argc; i++) { 50 while ((opt = getopt(argc, argv, "n:")) > 0) {
52 if (argv[i][0] == '-') { 51 switch (opt) {
53 opt = argv[i][1]; 52 case 'n':
54 switch (opt) { 53 len = atoi(optarg);
55 case 'n': 54 if (len >= 1)
56 tmplen = 0;
57 if (++i < argc)
58 tmplen = atoi(argv[i]);
59 if (tmplen < 1)
60 usage(head_usage);
61 len = tmplen;
62 break; 55 break;
63 case '-': 56 /* fallthrough */
64 case 'h': 57 default:
65 usage(head_usage); 58 usage(head_usage);
66 default:
67 errorMsg("invalid option -- %c\n", opt);
68 usage(head_usage);
69 }
70 } else {
71 break;
72 } 59 }
73 } 60 }
74 61
75 /* get rest of argv[] or stdin if nothing's left */ 62 /* get rest of argv[] or stdin if nothing's left */
76 if (i >= argc) { 63 if (argv[optind] == NULL) {
77 head(len, stdin); 64 head(len, stdin);
65 return status;
66 }
78 67
79 } else { 68 need_headers = optind != (argc - 1);
80 int need_headers = ((argc - i) > 1); 69 while (argv[optind]) {
81 70 if (strcmp(argv[optind], "-") == 0) {
82 for (; i < argc; i++) { 71 fp = stdin;
83 FILE *src; 72 argv[optind] = "standard input";
84 73 } else {
85 src = fopen(argv[i], "r"); 74 if ((fp = wfopen(argv[optind], "r")) == NULL)
86 if (!src) { 75 status = EXIT_FAILURE;
87 errorMsg("%s: %s\n", argv[i], strerror(errno)); 76 }
88 } else { 77 if (fp) {
89 /* emulating GNU behaviour */ 78 if (need_headers) {
90 if (need_headers) { 79 fprintf(stdout, "==> %s <==\n", argv[optind]);
91 fprintf(stdout, "==> %s <==\n", argv[i]); 80 }
92 } 81 head(len, fp);
93 head(len, src); 82 if (errno) {
94 if (i < argc - 1) { 83 errorMsg("%s: %s\n", argv[optind], strerror(errno));
95 fprintf(stdout, "\n"); 84 status = EXIT_FAILURE;
96 } 85 errno = 0;
97 } 86 }
87 if (optind < argc - 1)
88 fprintf(stdout, "\n");
89 if (fp != stdin)
90 fclose(fp);
98 } 91 }
92 optind++;
99 } 93 }
100 return(0);
101}
102 94
103/* $Id: head.c,v 1.14 2000/09/25 21:45:57 andersen Exp $ */ 95 return status;
96}