blob: ec4512765abff2f2bd5a387b630b4dea92b17351 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga34425382011-03-31 14:43:25 +020014//usage:#define head_trivial_usage
15//usage: "[OPTIONS] [FILE]..."
16//usage:#define head_full_usage "\n\n"
17//usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
18//usage: "With more than one FILE, precede each with a filename header.\n"
Pere Orga34425382011-03-31 14:43:25 +020019//usage: "\n -n N[kbm] Print first N lines"
20//usage: IF_FEATURE_FANCY_HEAD(
21//usage: "\n -c N[kbm] Print first N bytes"
22//usage: "\n -q Never print headers"
23//usage: "\n -v Always print headers"
24//usage: )
25//usage: "\n"
26//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
27//usage:
28//usage:#define head_example_usage
29//usage: "$ head -n 2 /etc/passwd\n"
30//usage: "root:x:0:0:root:/root:/bin/bash\n"
31//usage: "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
32
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
John Beppu3157b1f1999-12-10 07:42:50 +000034
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070035/* This is a NOEXEC applet. Be very careful! */
36
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000037static const char head_opts[] ALIGN1 =
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000039#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 "c:qv"
41#endif
42 ;
John Beppu3157b1f1999-12-10 07:42:50 +000043
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000044static const struct suffix_mult head_suffixes[] = {
45 { "b", 512 },
46 { "k", 1024 },
47 { "m", 1024*1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020048 { "", 0 }
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000049};
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000050
Denys Vlasenkoea8b2522010-06-02 12:57:26 +020051#define header_fmt_str "\n==> %s <==\n"
John Beppu3157b1f1999-12-10 07:42:50 +000052
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000053int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000054int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000055{
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 unsigned long count = 10;
57 unsigned long i;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000058#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 int count_bytes = 0;
60 int header_threshhold = 1;
61#endif
Matt Kraaic0321f92000-09-27 04:09:22 +000062 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 const char *fmt;
64 char *p;
65 int opt;
66 int c;
67 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000068
Denis Vlasenko08492072006-12-22 13:56:36 +000069#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 /* Allow legacy syntax of an initial numeric option without -n. */
Denis Vlasenko0d873672008-11-11 22:43:10 +000071 if (argv[1] && argv[1][0] == '-'
Denis Vlasenko459903b2006-11-27 14:44:18 +000072 && isdigit(argv[1][1])
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 ) {
74 --argc;
75 ++argv;
76 p = (*argv) + 1;
77 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000078 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +000079#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +000080
Denis Vlasenko67b23e62006-10-03 21:00:06 +000081 /* No size benefit in converting this to getopt32 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 while ((opt = getopt(argc, argv, head_opts)) > 0) {
Denis Vlasenkoc16bd212006-09-27 19:51:06 +000083 switch (opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000084#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko13858992006-10-08 12:49:22 +000085 case 'q':
86 header_threshhold = INT_MAX;
87 break;
88 case 'v':
89 header_threshhold = -1;
90 break;
91 case 'c':
92 count_bytes = 1;
93 /* fall through */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094#endif
Denis Vlasenko13858992006-10-08 12:49:22 +000095 case 'n':
96 p = optarg;
Denis Vlasenko08492072006-12-22 13:56:36 +000097#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
98 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000099#endif
Denis Vlasenko13858992006-10-08 12:49:22 +0000100 count = xatoul_sfx(p, head_suffixes);
Denis Vlasenko13858992006-10-08 12:49:22 +0000101 break;
102 default:
103 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 }
105 }
106
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000107 argc -= optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000109 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000110 *--argv = (char*)"-";
John Beppu3157b1f1999-12-10 07:42:50 +0000111
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112 fmt = header_fmt_str + 1;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000113#if ENABLE_FEATURE_FANCY_HEAD
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000114 if (argc <= header_threshhold) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 header_threshhold = 0;
116 }
117#else
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000118 if (argc <= 1) {
119 fmt += 11; /* "" */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 }
121 /* Now define some things here to avoid #ifdefs in the code below.
122 * These should optimize out of the if conditions below. */
123#define header_threshhold 1
124#define count_bytes 0
125#endif
126
127 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000128 fp = fopen_or_warn_stdin(*argv);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000129 if (fp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 if (fp == stdin) {
131 *argv = (char *) bb_msg_standard_input;
132 }
133 if (header_threshhold) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000134 printf(fmt, *argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 }
136 i = count;
137 while (i && ((c = getc(fp)) != EOF)) {
138 if (count_bytes || (c == '\n')) {
139 --i;
140 }
141 putchar(c);
142 }
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000143 if (fclose_if_not_stdin(fp)) {
Denis Vlasenko0d873672008-11-11 22:43:10 +0000144 bb_simple_perror_msg(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 retval = EXIT_FAILURE;
146 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000147 die_if_ferror_stdout();
Denis Vlasenko0d873672008-11-11 22:43:10 +0000148 } else {
149 retval = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 }
151 fmt = header_fmt_str;
152 } while (*++argv);
153
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000154 fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000155}