Fix header file usage -- there were many unnecessary header files included in
[oweals/busybox.git] / coreutils / head.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini head implementation for busybox
4  *
5  *
6  * Copyright (C) 1999,2000 by Lineo, inc.
7  * Written by John Beppu <beppu@lineo.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include "busybox.h"
26 #include <errno.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <stdlib.h>
30
31 int head(int len, FILE *fp)
32 {
33         int i;
34         char *input;
35
36         for (i = 0; i < len; i++) {
37                 if ((input = get_line_from_file(fp)) == NULL)
38                         break;
39                 fputs(input, stdout);
40                 free(input);
41         }
42         return 0;
43 }
44
45 /* BusyBoxed head(1) */
46 int head_main(int argc, char **argv)
47 {
48         FILE *fp;
49         int need_headers, opt, len = 10, status = EXIT_SUCCESS;
50
51         /* parse argv[] */
52         while ((opt = getopt(argc, argv, "n:")) > 0) {
53                 switch (opt) {
54                 case 'n':
55                         len = atoi(optarg);
56                         if (len >= 1)
57                                 break;
58                         /* fallthrough */
59                 default:
60                         usage(head_usage);
61                 }
62         }
63
64         /* get rest of argv[] or stdin if nothing's left */
65         if (argv[optind] == NULL) {
66                 head(len, stdin);
67                 return status;
68         } 
69
70         need_headers = optind != (argc - 1);
71         while (argv[optind]) {
72                 if (strcmp(argv[optind], "-") == 0) {
73                         fp = stdin;
74                         argv[optind] = "standard input";
75                 } else {
76                         if ((fp = wfopen(argv[optind], "r")) == NULL)
77                                 status = EXIT_FAILURE;
78                 }
79                 if (fp) {
80                         if (need_headers) {
81                                 printf("==> %s <==\n", argv[optind]);
82                         }
83                         head(len, fp);
84                         if (errno) {
85                                 perror_msg("%s", argv[optind]);
86                                 status = EXIT_FAILURE;
87                                 errno = 0;
88                         }
89                         if (optind < argc - 1)
90                                 putchar('\n');
91                         if (fp != stdin)
92                                 fclose(fp);
93                 }
94                 optind++;
95         }
96
97         return status;
98 }