blob: bc7f35429d155d9483ba5155dd8a840900529d29 [file] [log] [blame]
John Beppu3157b1f1999-12-10 07:42:50 +00001/*
2 * Mini head implementation for busybox
3 *
4 *
5 * Copyright (C) 1999 by Lineo, inc.
Eric Andersencf536871999-12-10 08:29:20 +00006 * Written by John Beppu <beppu@lineo.com>
John Beppu3157b1f1999-12-10 07:42:50 +00007 *
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 "internal.h"
25#include <errno.h>
26#include <stdio.h>
27
28const char head_usage[] =
Erik Andersen5509af72000-01-23 18:19:02 +000029"head [OPTION] [FILE]...\n\n"
John Beppu3157b1f1999-12-10 07:42:50 +000030"Print first 10 lines of each FILE to standard output.\n"
Eric Andersencf536871999-12-10 08:29:20 +000031"With more than one FILE, precede each with a header giving the\n"
Erik Andersen5509af72000-01-23 18:19:02 +000032"file name. With no FILE, or when FILE is -, read standard input.\n\n"
33"Options:\n"
34"\t-n NUM\t\tPrint first NUM lines instead of first 10\n";
John Beppu3157b1f1999-12-10 07:42:50 +000035
36int
37head(int len, FILE *src)
38{
39 int i;
40 char buffer[1024];
41
42 for (i = 0; i < len; i++) {
43 fgets(buffer, 1024, src);
44 if (feof(src)) { break; }
45 fputs(buffer, stdout);
46 }
47 return 0;
48}
49
50/* BusyBoxed head(1) */
51int
52head_main(int argc, char **argv)
53{
John Beppu3157b1f1999-12-10 07:42:50 +000054 char opt;
Erik Andersen5509af72000-01-23 18:19:02 +000055 int len = 10, tmplen, i;
John Beppu3157b1f1999-12-10 07:42:50 +000056
57 /* parse argv[] */
Erik Andersen5509af72000-01-23 18:19:02 +000058 for (i = 1; i < argc; i++) {
John Beppu3157b1f1999-12-10 07:42:50 +000059 if (argv[i][0] == '-') {
60 opt = argv[i][1];
61 switch (opt) {
Erik Andersen5509af72000-01-23 18:19:02 +000062 case 'n':
63 tmplen = 0;
Erik Andersen3fe39dc2000-01-25 18:13:53 +000064 if (++i < argc)
Erik Andersen5509af72000-01-23 18:19:02 +000065 tmplen = atoi(argv[i]);
66 if (tmplen < 1)
67 usage(head_usage);
68 len = tmplen;
69 break;
Eric Andersencf536871999-12-10 08:29:20 +000070 case '-':
John Beppu3157b1f1999-12-10 07:42:50 +000071 case 'h':
72 usage(head_usage);
73 default:
74 fprintf(stderr, "head: invalid option -- %c\n", opt);
75 usage(head_usage);
76 }
77 } else {
78 break;
79 }
80 }
81
82 /* get rest of argv[] or stdin if nothing's left */
83 if (i >= argc) {
84 head(len, stdin);
85
86 } else {
87 int need_headers = ((argc - i) > 1);
88 for ( ; i < argc; i++) {
89 FILE *src;
90 src = fopen(argv[i], "r");
91 if (!src) {
92 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
93 } else {
94 /* emulating GNU behaviour */
95 if (need_headers) {
96 fprintf(stdout, "==> %s <==\n", argv[i]);
97 }
98 head(len, src);
99 if (i < argc - 1) {
100 fprintf(stdout, "\n");
101 }
102 }
103 }
104 }
105 exit(0);
106}
107
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000108/* $Id: head.c,v 1.6 2000/01/25 18:13:53 erik Exp $ */