blob: 59b4d552c44102d4535ce1397c9849706eee3217 [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
Denis Vlasenko08492072006-12-22 13:56:36 +000050#if ENABLE_INCLUDE_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. */
Denis Vlasenko459903b2006-11-27 14:44:18 +000052 if (argc > 1 && argv[1][0] == '-'
53 && isdigit(argv[1][1])
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 ) {
55 --argc;
56 ++argv;
57 p = (*argv) + 1;
58 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000059 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +000060#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +000061
Denis Vlasenko67b23e62006-10-03 21:00:06 +000062 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +000064 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000065#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +000066 case 'q':
67 header_threshhold = INT_MAX;
68 break;
69 case 'v':
70 header_threshhold = -1;
71 break;
72 case 'c':
73 count_bytes = 1;
74 /* fall through */
Manuel Novoa III cad53642003-03-19 09:13:01 +000075#endif
Denis Vlasenko13858992006-10-08 12:49:22 +000076 case 'n':
77 p = optarg;
Denis Vlasenko08492072006-12-22 13:56:36 +000078#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
79 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000080#endif
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000081
82#if !ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +000083 count = xatoul(p);
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000084#else
Denis Vlasenko13858992006-10-08 12:49:22 +000085 count = xatoul_sfx(p, head_suffixes);
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000086#endif
Denis Vlasenko13858992006-10-08 12:49:22 +000087 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) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000095 *--argv = (char*)"-";
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;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000099#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 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 {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000114 fp = fopen_or_warn_stdin(*argv);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000115 if (fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 if (fp == stdin) {
117 *argv = (char *) bb_msg_standard_input;
118 }
119 if (header_threshhold) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000120 printf(fmt, *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 }
122 i = count;
123 while (i && ((c = getc(fp)) != EOF)) {
124 if (count_bytes || (c == '\n')) {
125 --i;
126 }
127 putchar(c);
128 }
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000129 if (fclose_if_not_stdin(fp)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
131 retval = EXIT_FAILURE;
132 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000133 die_if_ferror_stdout();
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 }
135 fmt = header_fmt_str;
136 } while (*++argv);
137
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000138 fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000139}