blob: a9d024e6216858c4282276c013d57d36bba5c270 [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[] =
Eric Andersencf536871999-12-10 08:29:20 +000029"Usage: head [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"
32"file name. With no FILE, or when FILE is -, read standard input.\n";
John Beppu3157b1f1999-12-10 07:42:50 +000033
34int
35head(int len, FILE *src)
36{
37 int i;
38 char buffer[1024];
39
40 for (i = 0; i < len; i++) {
41 fgets(buffer, 1024, src);
42 if (feof(src)) { break; }
43 fputs(buffer, stdout);
44 }
45 return 0;
46}
47
48/* BusyBoxed head(1) */
49int
50head_main(int argc, char **argv)
51{
52 int i = 1;
53 char opt;
54 int len = 10;
55
56 /* 1st option is potentially special */
John Beppu25ab6531999-12-15 19:29:09 +000057 if ((argc > 1) && (argv[1][0] == '-') && isDecimal(argv[1][1])) {
John Beppu3157b1f1999-12-10 07:42:50 +000058 int tmplen = atoi(&argv[1][1]);
59 if (tmplen) { len = tmplen; }
60 i = 2;
61 }
62
63 /* parse argv[] */
64 for ( ; i < argc; i++) {
65 if (argv[i][0] == '-') {
66 opt = argv[i][1];
67 switch (opt) {
Eric Andersencf536871999-12-10 08:29:20 +000068 case '-':
John Beppu25ab6531999-12-15 19:29:09 +000069 break;
John Beppu3157b1f1999-12-10 07:42:50 +000070 case 'h':
71 usage(head_usage);
72 default:
73 fprintf(stderr, "head: invalid option -- %c\n", opt);
74 usage(head_usage);
75 }
76 } else {
77 break;
78 }
79 }
80
81 /* get rest of argv[] or stdin if nothing's left */
82 if (i >= argc) {
83 head(len, stdin);
84
85 } else {
86 int need_headers = ((argc - i) > 1);
87 for ( ; i < argc; i++) {
88 FILE *src;
89 src = fopen(argv[i], "r");
90 if (!src) {
91 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
92 } else {
93 /* emulating GNU behaviour */
94 if (need_headers) {
95 fprintf(stdout, "==> %s <==\n", argv[i]);
96 }
97 head(len, src);
98 if (i < argc - 1) {
99 fprintf(stdout, "\n");
100 }
101 }
102 }
103 }
104 exit(0);
105}
106
John Beppu25ab6531999-12-15 19:29:09 +0000107/* $Id: head.c,v 1.3 1999/12/15 19:29:09 beppu Exp $ */