aboutsummaryrefslogtreecommitdiff
path: root/head.c
diff options
context:
space:
mode:
Diffstat (limited to 'head.c')
-rw-r--r--head.c97
1 files changed, 0 insertions, 97 deletions
diff --git a/head.c b/head.c
deleted file mode 100644
index 688c250b1..000000000
--- a/head.c
+++ /dev/null
@@ -1,97 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini head implementation for busybox
4 *
5 *
6 * Copyright (C) 1999,2000,2001 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 <stdio.h>
26#include <getopt.h>
27#include <stdlib.h>
28#include <string.h>
29#include "busybox.h"
30
31static 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) */
46int 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 show_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 (ferror(fp)) {
85 perror_msg("%s", argv[optind]);
86 status = EXIT_FAILURE;
87 }
88 if (optind < argc - 1)
89 putchar('\n');
90 if (fp != stdin)
91 fclose(fp);
92 }
93 optind++;
94 }
95
96 return status;
97}