1 /* vi: set sw=4 ts=4: */
3 * Mini head implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
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.
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.
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
30 static int head(int len, FILE *fp)
35 for (i = 0; i < len; i++) {
36 if ((input = get_line_from_file(fp)) == NULL)
44 /* BusyBoxed head(1) */
45 int head_main(int argc, char **argv)
48 int need_headers, opt, len = 10, status = EXIT_SUCCESS;
50 if (( argc >= 2 ) && ( strlen ( argv [1] ) >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) {
51 len = atoi ( &argv [1][1] );
56 while ((opt = getopt(argc, argv, "n:")) > 0) {
68 /* get rest of argv[] or stdin if nothing's left */
69 if (argv[optind] == NULL) {
74 need_headers = optind != (argc - 1);
75 while (argv[optind]) {
76 if (strcmp(argv[optind], "-") == 0) {
78 argv[optind] = "standard input";
80 if ((fp = wfopen(argv[optind], "r")) == NULL)
81 status = EXIT_FAILURE;
85 printf("==> %s <==\n", argv[optind]);
89 perror_msg("%s", argv[optind]);
90 status = EXIT_FAILURE;
92 if (optind < argc - 1)