aboutsummaryrefslogtreecommitdiff
path: root/coreutils/head.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
committerErik Andersen <andersen@codepoet.org>2000-02-08 19:58:47 +0000
commite49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /coreutils/head.c
parentc0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff)
downloadbusybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2
busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'coreutils/head.c')
-rw-r--r--coreutils/head.c134
1 files changed, 69 insertions, 65 deletions
diff --git a/coreutils/head.c b/coreutils/head.c
index b80d06580..82a73de2a 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini head implementation for busybox 3 * Mini head implementation for busybox
3 * 4 *
@@ -26,83 +27,86 @@
26#include <stdio.h> 27#include <stdio.h>
27 28
28const char head_usage[] = 29const char head_usage[] =
29"head [OPTION] [FILE]...\n\n" 30 "head [OPTION] [FILE]...\n\n"
30"Print first 10 lines of each FILE to standard output.\n" 31 "Print first 10 lines of each FILE to standard output.\n"
31"With more than one FILE, precede each with a header giving the\n" 32 "With more than one FILE, precede each with a header giving the\n"
32"file name. With no FILE, or when FILE is -, read standard input.\n\n" 33 "file name. With no FILE, or when FILE is -, read standard input.\n\n"
33"Options:\n"
34"\t-n NUM\t\tPrint first NUM lines instead of first 10\n";
35 34
36int 35 "Options:\n" "\t-n NUM\t\tPrint first NUM lines instead of first 10\n";
37head(int len, FILE *src) 36
37int head(int len, FILE * src)
38{ 38{
39 int i; 39 int i;
40 char buffer[1024]; 40 char buffer[1024];
41 41
42 for (i = 0; i < len; i++) { 42 for (i = 0; i < len; i++) {
43 fgets(buffer, 1024, src); 43 fgets(buffer, 1024, src);
44 if (feof(src)) { break; } 44 if (feof(src)) {
45 fputs(buffer, stdout); 45 break;
46 } 46 }
47 return 0; 47 fputs(buffer, stdout);
48 }
49 return 0;
48} 50}
49 51
50/* BusyBoxed head(1) */ 52/* BusyBoxed head(1) */
51int 53int head_main(int argc, char **argv)
52head_main(int argc, char **argv)
53{ 54{
54 char opt; 55 char opt;
55 int len = 10, tmplen, i; 56 int len = 10, tmplen, i;
56 57
57 /* parse argv[] */ 58 /* parse argv[] */
58 for (i = 1; i < argc; i++) { 59 for (i = 1; i < argc; i++) {
59 if (argv[i][0] == '-') { 60 if (argv[i][0] == '-') {
60 opt = argv[i][1]; 61 opt = argv[i][1];
61 switch (opt) { 62 switch (opt) {
62 case 'n': 63 case 'n':
63 tmplen = 0; 64 tmplen = 0;
64 if (++i < argc) 65 if (++i < argc)
65 tmplen = atoi(argv[i]); 66 tmplen = atoi(argv[i]);
66 if (tmplen < 1) 67 if (tmplen < 1)
67 usage(head_usage); 68 usage(head_usage);
68 len = tmplen; 69 len = tmplen;
69 break; 70 break;
70 case '-': 71 case '-':
71 case 'h': 72 case 'h':
72 usage(head_usage); 73 usage(head_usage);
73 default: 74 default:
74 fprintf(stderr, "head: invalid option -- %c\n", opt); 75 fprintf(stderr, "head: invalid option -- %c\n", opt);
75 usage(head_usage); 76 usage(head_usage);
76 } 77 }
77 } else { 78 } else {
78 break; 79 break;
80 }
79 } 81 }
80 }
81 82
82 /* get rest of argv[] or stdin if nothing's left */ 83 /* get rest of argv[] or stdin if nothing's left */
83 if (i >= argc) { 84 if (i >= argc) {
84 head(len, stdin); 85 head(len, stdin);
85 86
86 } else { 87 } else {
87 int need_headers = ((argc - i) > 1); 88 int need_headers = ((argc - i) > 1);
88 for ( ; i < argc; i++) { 89
89 FILE *src; 90 for (; i < argc; i++) {
90 src = fopen(argv[i], "r"); 91 FILE *src;
91 if (!src) { 92
92 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno)); 93 src = fopen(argv[i], "r");
93 } else { 94 if (!src) {
94 /* emulating GNU behaviour */ 95 fprintf(stderr, "head: %s: %s\n", argv[i],
95 if (need_headers) { 96 strerror(errno));
96 fprintf(stdout, "==> %s <==\n", argv[i]); 97 } else {
97 } 98 /* emulating GNU behaviour */
98 head(len, src); 99 if (need_headers) {
99 if (i < argc - 1) { 100 fprintf(stdout, "==> %s <==\n", argv[i]);
100 fprintf(stdout, "\n"); 101 }
102 head(len, src);
103 if (i < argc - 1) {
104 fprintf(stdout, "\n");
105 }
106 }
101 } 107 }
102 }
103 } 108 }
104 } 109 exit(0);
105 exit(0);
106} 110}
107 111
108/* $Id: head.c,v 1.7 2000/02/07 05:29:42 erik Exp $ */ 112/* $Id: head.c,v 1.8 2000/02/08 19:58:47 erik Exp $ */