2 * Mini head implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@line.com>
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
28 const char head_usage[] =
29 "Usage: head [OPTION]... [FILE]...\n"
30 "Print first 10 lines of each FILE to standard output.\n"
31 "With more than one FILE, precede each with a header giving the file name.\n"
32 "With no FILE, or when FILE is -, read standard input.\n\n"
33 " -h display this help and exit\n";
36 head(int len, FILE *src)
41 for (i = 0; i < len; i++) {
42 fgets(buffer, 1024, src);
43 if (feof(src)) { break; }
44 fputs(buffer, stdout);
49 /* BusyBoxed head(1) */
51 head_main(int argc, char **argv)
57 /* 1st option is potentially special */
58 if ((argv[1][0] == '-') && isDecimal(argv[1][1])) {
59 int tmplen = atoi(&argv[1][1]);
60 if (tmplen) { len = tmplen; }
65 for ( ; i < argc; i++) {
66 if (argv[i][0] == '-') {
72 fprintf(stderr, "head: invalid option -- %c\n", opt);
80 /* get rest of argv[] or stdin if nothing's left */
85 int need_headers = ((argc - i) > 1);
86 for ( ; i < argc; i++) {
88 src = fopen(argv[i], "r");
90 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
92 /* emulating GNU behaviour */
94 fprintf(stdout, "==> %s <==\n", argv[i]);
98 fprintf(stdout, "\n");
106 /* $Id: head.c,v 1.1 1999/12/10 07:42:50 beppu Exp $ */