blob: dab4de11b16555514da7eff3bcc194bb86fd29d5 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
John Beppu3157b1f1999-12-10 07:42:50 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * head implementation for busybox
John Beppu3157b1f1999-12-10 07:42:50 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
John Beppu3157b1f1999-12-10 07:42:50 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Manuel Novoa III cad53642003-03-19 09:13:01 +000023/* BB_AUDIT SUSv3 compliant */
24/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
26
John Beppu3157b1f1999-12-10 07:42:50 +000027#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000028#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000029#include <limits.h>
Robert Griebl53146cc2002-05-27 22:24:53 +000030#include <ctype.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000031#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000033
Manuel Novoa III cad53642003-03-19 09:13:01 +000034static const char head_opts[] =
35 "n:"
36#ifdef CONFIG_FEATURE_FANCY_HEAD
37 "c:qv"
38#endif
39 ;
John Beppu3157b1f1999-12-10 07:42:50 +000040
Manuel Novoa III cad53642003-03-19 09:13:01 +000041static const char header_fmt_str[] = "\n==> %s <==\n";
John Beppu3157b1f1999-12-10 07:42:50 +000042
Erik Andersene49d5ec2000-02-08 19:58:47 +000043int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000044{
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 unsigned long count = 10;
46 unsigned long i;
47#ifdef CONFIG_FEATURE_FANCY_HEAD
48 int count_bytes = 0;
49 int header_threshhold = 1;
50#endif
51
Matt Kraaic0321f92000-09-27 04:09:22 +000052 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 const char *fmt;
54 char *p;
55 int opt;
56 int c;
57 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000058
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 /* Allow legacy syntax of an initial numeric option without -n. */
60 if ((argc > 1) && (argv[1][0] == '-')
61 /* && (isdigit)(argv[1][1]) */
62 && (((unsigned int)(argv[1][1] - '0')) <= 9)
63 ) {
64 --argc;
65 ++argv;
66 p = (*argv) + 1;
67 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000068 }
69
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 while ((opt = getopt(argc, argv, head_opts)) > 0) {
71 switch(opt) {
72#ifdef CONFIG_FEATURE_FANCY_HEAD
73 case 'q':
74 header_threshhold = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 case 'v':
77 header_threshhold = -1;
78 break;
79 case 'c':
80 count_bytes = 1;
81 /* fall through */
82#endif
83 case 'n':
84 p = optarg;
85 GET_COUNT:
86 count = bb_xgetularg10(p);
87 break;
88 default:
89 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 }
91 }
92
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 argv += optind;
94 if (!*argv) {
95 *--argv = "-";
John Beppu3157b1f1999-12-10 07:42:50 +000096 }
John Beppu3157b1f1999-12-10 07:42:50 +000097
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 fmt = header_fmt_str + 1;
99#ifdef CONFIG_FEATURE_FANCY_HEAD
100 if (argc - optind <= header_threshhold) {
101 header_threshhold = 0;
102 }
103#else
104 if (argc <= optind + 1) {
105 fmt += 11;
106 }
107 /* Now define some things here to avoid #ifdefs in the code below.
108 * These should optimize out of the if conditions below. */
109#define header_threshhold 1
110#define count_bytes 0
111#endif
112
113 do {
114 if ((fp = bb_wfopen_input(*argv)) != NULL) {
115 if (fp == stdin) {
116 *argv = (char *) bb_msg_standard_input;
117 }
118 if (header_threshhold) {
119 bb_printf(fmt, *argv);
120 }
121 i = count;
122 while (i && ((c = getc(fp)) != EOF)) {
123 if (count_bytes || (c == '\n')) {
124 --i;
125 }
126 putchar(c);
127 }
128 if (bb_fclose_nonstdin(fp)) {
129 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
130 retval = EXIT_FAILURE;
131 }
132 bb_xferror_stdout();
133 }
134 fmt = header_fmt_str;
135 } while (*++argv);
136
137 bb_fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000138}