A few minor updates. ;-)
[oweals/busybox.git] / coreutils / head.c
1 /*
2  * Mini head implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by John Beppu <beppu@lineo.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <errno.h>
26 #include <stdio.h>
27
28 const char head_usage[] =
29 "head [OPTION] [FILE]...\n\n"
30 "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 "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
36 int
37 head(int len, FILE *src)
38 {
39     int     i;
40     char    buffer[1024];
41
42     for (i = 0; i < len; i++) {
43         fgets(buffer, 1024, src);
44         if (feof(src)) { break; }
45         fputs(buffer, stdout);
46     }
47     return 0;
48 }
49
50 /* BusyBoxed head(1) */
51 int
52 head_main(int argc, char **argv)
53 {
54     char    opt;
55     int     len = 10, tmplen, i;
56
57     /* parse argv[] */
58     for (i = 1; i < argc; i++) {
59         if (argv[i][0] == '-') {
60             opt = argv[i][1];
61             switch (opt) {
62                 case 'n':
63                     tmplen = 0;
64                     if (++i < argc)
65                         tmplen = atoi(argv[i]);
66                     if (tmplen < 1)
67                         usage(head_usage);
68                     len = tmplen;
69                     break;
70                 case '-':
71                 case 'h':
72                     usage(head_usage);
73                 default:
74                     fprintf(stderr, "head: invalid option -- %c\n", opt);
75                     usage(head_usage);
76             }
77         } else {
78             break;
79         }
80     }
81
82     /* get rest of argv[] or stdin if nothing's left */
83     if (i >= argc) {
84         head(len, stdin);
85
86     } else {
87         int need_headers = ((argc - i) > 1);
88         for ( ; i < argc; i++) {
89             FILE *src;
90             src = fopen(argv[i], "r");
91             if (!src) {
92                 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
93             } else {
94                 /* emulating GNU behaviour */
95                 if (need_headers) { 
96                     fprintf(stdout, "==> %s <==\n", argv[i]);
97                 }
98                 head(len, src);
99                 if (i < argc - 1) {
100                     fprintf(stdout, "\n");
101                 }
102             }
103         }
104     }
105     exit(0);
106 }
107
108 /* $Id: head.c,v 1.7 2000/02/07 05:29:42 erik Exp $ */