blob: 7d5f219d2bf3c211d9d9e5caeda83fafd2b635ea [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 *
Rob Landleyf8fd4db2006-01-30 01:30:39 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
John Beppu3157b1f1999-12-10 07:42:50 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
13
Eric Andersencbe31da2001-02-20 06:14:08 +000014#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000015
Manuel Novoa III cad53642003-03-19 09:13:01 +000016static const char head_opts[] =
17 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000018#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000019 "c:qv"
20#endif
21 ;
John Beppu3157b1f1999-12-10 07:42:50 +000022
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000023#if ENABLE_FEATURE_FANCY_HEAD
24static const struct suffix_mult head_suffixes[] = {
25 { "b", 512 },
26 { "k", 1024 },
27 { "m", 1024*1024 },
28 { NULL, 0 }
29};
30#endif
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000031
Manuel Novoa III cad53642003-03-19 09:13:01 +000032static const char header_fmt_str[] = "\n==> %s <==\n";
John Beppu3157b1f1999-12-10 07:42:50 +000033
Erik Andersene49d5ec2000-02-08 19:58:47 +000034int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000035{
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 unsigned long count = 10;
37 unsigned long i;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000038#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 int count_bytes = 0;
40 int header_threshhold = 1;
41#endif
42
Matt Kraaic0321f92000-09-27 04:09:22 +000043 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 const char *fmt;
45 char *p;
46 int opt;
47 int c;
48 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000049
Rob Landleyf8fd4db2006-01-30 01:30:39 +000050#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 /* Allow legacy syntax of an initial numeric option without -n. */
52 if ((argc > 1) && (argv[1][0] == '-')
53 /* && (isdigit)(argv[1][1]) */
54 && (((unsigned int)(argv[1][1] - '0')) <= 9)
55 ) {
56 --argc;
57 ++argv;
58 p = (*argv) + 1;
59 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000060 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +000061#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +000062
Denis Vlasenko67b23e62006-10-03 21:00:06 +000063 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +000065 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000066#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 case 'q':
68 header_threshhold = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 case 'v':
71 header_threshhold = -1;
72 break;
73 case 'c':
74 count_bytes = 1;
75 /* fall through */
76#endif
77 case 'n':
78 p = optarg;
Rob Landleyf8fd4db2006-01-30 01:30:39 +000079#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000081#endif
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000082
83#if !ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 count = bb_xgetularg10(p);
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000085#else
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000086 count = bb_xgetularg_bnd_sfx(p, 10,
87 0, ULONG_MAX,
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000088 head_suffixes);
89#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 break;
91 default:
92 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
94 }
95
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 argv += optind;
97 if (!*argv) {
98 *--argv = "-";
John Beppu3157b1f1999-12-10 07:42:50 +000099 }
John Beppu3157b1f1999-12-10 07:42:50 +0000100
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 fmt = header_fmt_str + 1;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000102#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 if (argc - optind <= header_threshhold) {
104 header_threshhold = 0;
105 }
106#else
107 if (argc <= optind + 1) {
108 fmt += 11;
109 }
110 /* Now define some things here to avoid #ifdefs in the code below.
111 * These should optimize out of the if conditions below. */
112#define header_threshhold 1
113#define count_bytes 0
114#endif
115
116 do {
117 if ((fp = bb_wfopen_input(*argv)) != NULL) {
118 if (fp == stdin) {
119 *argv = (char *) bb_msg_standard_input;
120 }
121 if (header_threshhold) {
122 bb_printf(fmt, *argv);
123 }
124 i = count;
125 while (i && ((c = getc(fp)) != EOF)) {
126 if (count_bytes || (c == '\n')) {
127 --i;
128 }
129 putchar(c);
130 }
131 if (bb_fclose_nonstdin(fp)) {
132 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
133 retval = EXIT_FAILURE;
134 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000135 xferror_stdout();
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 }
137 fmt = header_fmt_str;
138 } while (*++argv);
139
140 bb_fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000141}