Set the close-on-exec flag, just to be saf
[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 <ctype.h>
29 #include "busybox.h"
30
31 static 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         if (( argc >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) {
52                 len = atoi ( &argv [1][1] );
53                 optind = 2;
54         }
55
56         /* parse argv[] */
57         while ((opt = getopt(argc, argv, "n:")) > 0) {
58                 switch (opt) {
59                 case 'n':
60                         len = atoi(optarg);
61                         if (len >= 0)
62                                 break;
63                         /* fallthrough */
64                 default:
65                         show_usage();
66                 }
67         }
68
69         /* get rest of argv[] or stdin if nothing's left */
70         if (argv[optind] == NULL) {
71                 head(len, stdin);
72                 return status;
73         } 
74
75         need_headers = optind != (argc - 1);
76         while (argv[optind]) {
77                 if (strcmp(argv[optind], "-") == 0) {
78                         fp = stdin;
79                         argv[optind] = "standard input";
80                 } else {
81                         if ((fp = wfopen(argv[optind], "r")) == NULL)
82                                 status = EXIT_FAILURE;
83                 }
84                 if (fp) {
85                         if (need_headers) {
86                                 printf("==> %s <==\n", argv[optind]);
87                         }
88                         head(len, fp);
89                         if (ferror(fp)) {
90                                 perror_msg("%s", argv[optind]);
91                                 status = EXIT_FAILURE;
92                         }
93                         if (optind < argc - 1)
94                                 putchar('\n');
95                         if (fp != stdin)
96                                 fclose(fp);
97                 }
98                 optind++;
99         }
100
101         return status;
102 }