2 * Mini head implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@lineo.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 "head [OPTION] [FILE]...\n\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\n"
32 "file name. With no FILE, or when FILE is -, read standard input.\n\n"
34 "\t-n NUM\t\tPrint first NUM lines instead of first 10\n";
37 head(int len, FILE *src)
42 for (i = 0; i < len; i++) {
43 fgets(buffer, 1024, src);
44 if (feof(src)) { break; }
45 fputs(buffer, stdout);
50 /* BusyBoxed head(1) */
52 head_main(int argc, char **argv)
55 int len = 10, tmplen, i;
58 for (i = 1; i < argc; i++) {
59 if (argv[i][0] == '-') {
65 tmplen = atoi(argv[i]);
74 fprintf(stderr, "head: invalid option -- %c\n", opt);
82 /* get rest of argv[] or stdin if nothing's left */
87 int need_headers = ((argc - i) > 1);
88 for ( ; i < argc; i++) {
90 src = fopen(argv[i], "r");
92 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
94 /* emulating GNU behaviour */
96 fprintf(stdout, "==> %s <==\n", argv[i]);
100 fprintf(stdout, "\n");
108 /* $Id: head.c,v 1.6 2000/01/25 18:13:53 erik Exp $ */