aboutsummaryrefslogtreecommitdiff
path: root/coreutils/head.c
diff options
context:
space:
mode:
authorJohn Beppu <beppu@lbox.org>1999-12-10 07:42:50 +0000
committerJohn Beppu <beppu@lbox.org>1999-12-10 07:42:50 +0000
commit3157b1fba9ee8a558b04e9025da9f77e4256c0ea (patch)
tree27ea10d69773a06c8618132a2b76e7583db9da9c /coreutils/head.c
parenta3e0d7928b0042dfe1be94da62b750664306b404 (diff)
downloadbusybox-w32-3157b1fba9ee8a558b04e9025da9f77e4256c0ea.tar.gz
busybox-w32-3157b1fba9ee8a558b04e9025da9f77e4256c0ea.tar.bz2
busybox-w32-3157b1fba9ee8a558b04e9025da9f77e4256c0ea.zip
Implemented head(1).
tried to mimic GNU behaviour.
Diffstat (limited to 'coreutils/head.c')
-rw-r--r--coreutils/head.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/coreutils/head.c b/coreutils/head.c
new file mode 100644
index 000000000..930839de3
--- /dev/null
+++ b/coreutils/head.c
@@ -0,0 +1,106 @@
1/*
2 * Mini head implementation for busybox
3 *
4 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by John Beppu <beppu@line.com>
7 *
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[] =
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";
34
35int
36head(int len, FILE *src)
37{
38 int i;
39 char buffer[1024];
40
41 for (i = 0; i < len; i++) {
42 fgets(buffer, 1024, src);
43 if (feof(src)) { break; }
44 fputs(buffer, stdout);
45 }
46 return 0;
47}
48
49/* BusyBoxed head(1) */
50int
51head_main(int argc, char **argv)
52{
53 int i = 1;
54 char opt;
55 int len = 10;
56
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; }
61 i = 2;
62 }
63
64 /* parse argv[] */
65 for ( ; i < argc; i++) {
66 if (argv[i][0] == '-') {
67 opt = argv[i][1];
68 switch (opt) {
69 case 'h':
70 usage(head_usage);
71 default:
72 fprintf(stderr, "head: invalid option -- %c\n", opt);
73 usage(head_usage);
74 }
75 } else {
76 break;
77 }
78 }
79
80 /* get rest of argv[] or stdin if nothing's left */
81 if (i >= argc) {
82 head(len, stdin);
83
84 } else {
85 int need_headers = ((argc - i) > 1);
86 for ( ; i < argc; i++) {
87 FILE *src;
88 src = fopen(argv[i], "r");
89 if (!src) {
90 fprintf(stderr,"head: %s: %s\n", argv[i], strerror(errno));
91 } else {
92 /* emulating GNU behaviour */
93 if (need_headers) {
94 fprintf(stdout, "==> %s <==\n", argv[i]);
95 }
96 head(len, src);
97 if (i < argc - 1) {
98 fprintf(stdout, "\n");
99 }
100 }
101 }
102 }
103 exit(0);
104}
105
106/* $Id: head.c,v 1.1 1999/12/10 07:42:50 beppu Exp $ */