Use an int to hold the result of fgetc (bug noted by David Kimdon).
[oweals/busybox.git] / coreutils / head.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini head implementation for busybox
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
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 <stdio.h>
25 #include <getopt.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "busybox.h"
29
30 static int head(int len, FILE *fp)
31 {
32         int i;
33         char *input;
34
35         for (i = 0; i < len; i++) {
36                 if ((input = get_line_from_file(fp)) == NULL)
37                         break;
38                 fputs(input, stdout);
39                 free(input);
40         }
41         return 0;
42 }
43
44 /* BusyBoxed head(1) */
45 int head_main(int argc, char **argv)
46 {
47         FILE *fp;
48         int need_headers, opt, len = 10, status = EXIT_SUCCESS;
49
50         /* parse argv[] */
51         while ((opt = getopt(argc, argv, "n:")) > 0) {
52                 switch (opt) {
53                 case 'n':
54                         len = atoi(optarg);
55                         if (len >= 1)
56                                 break;
57                         /* fallthrough */
58                 default:
59                         show_usage();
60                 }
61         }
62
63         /* get rest of argv[] or stdin if nothing's left */
64         if (argv[optind] == NULL) {
65                 head(len, stdin);
66                 return status;
67         } 
68
69         need_headers = optind != (argc - 1);
70         while (argv[optind]) {
71                 if (strcmp(argv[optind], "-") == 0) {
72                         fp = stdin;
73                         argv[optind] = "standard input";
74                 } else {
75                         if ((fp = wfopen(argv[optind], "r")) == NULL)
76                                 status = EXIT_FAILURE;
77                 }
78                 if (fp) {
79                         if (need_headers) {
80                                 printf("==> %s <==\n", argv[optind]);
81                         }
82                         head(len, fp);
83                         if (ferror(fp)) {
84                                 perror_msg("%s", argv[optind]);
85                                 status = EXIT_FAILURE;
86                         }
87                         if (optind < argc - 1)
88                                 putchar('\n');
89                         if (fp != stdin)
90                                 fclose(fp);
91                 }
92                 optind++;
93         }
94
95         return status;
96 }