blob: 184e8161cef2d2d748758242d991fbd1c846ba3b [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
John Beppu3157b1f1999-12-10 07:42:50 +000014#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000015#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000016#include <limits.h>
Robert Griebl53146cc2002-05-27 22:24:53 +000017#include <ctype.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000018#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000019#include "busybox.h"
John Beppu3157b1f1999-12-10 07:42:50 +000020
Manuel Novoa III cad53642003-03-19 09:13:01 +000021static const char head_opts[] =
22 "n:"
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000023#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 "c:qv"
25#endif
26 ;
John Beppu3157b1f1999-12-10 07:42:50 +000027
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000028#if ENABLE_FEATURE_FANCY_HEAD
29static const struct suffix_mult head_suffixes[] = {
30 { "b", 512 },
31 { "k", 1024 },
32 { "m", 1024*1024 },
33 { NULL, 0 }
34};
35#endif
36
Manuel Novoa III cad53642003-03-19 09:13:01 +000037static const char header_fmt_str[] = "\n==> %s <==\n";
John Beppu3157b1f1999-12-10 07:42:50 +000038
Erik Andersene49d5ec2000-02-08 19:58:47 +000039int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000040{
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 unsigned long count = 10;
42 unsigned long i;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000043#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 int count_bytes = 0;
45 int header_threshhold = 1;
46#endif
47
Matt Kraaic0321f92000-09-27 04:09:22 +000048 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 const char *fmt;
50 char *p;
51 int opt;
52 int c;
53 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000054
Rob Landleyf8fd4db2006-01-30 01:30:39 +000055#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 /* Allow legacy syntax of an initial numeric option without -n. */
57 if ((argc > 1) && (argv[1][0] == '-')
58 /* && (isdigit)(argv[1][1]) */
59 && (((unsigned int)(argv[1][1] - '0')) <= 9)
60 ) {
61 --argc;
62 ++argv;
63 p = (*argv) + 1;
64 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000065 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +000066#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +000067
Glenn L McGrath0bd02572005-12-11 03:09:05 +000068 /* No size benefit in converting this to bb_getopt_ulflags */
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 while ((opt = getopt(argc, argv, head_opts)) > 0) {
70 switch(opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000071#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 case 'q':
73 header_threshhold = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 case 'v':
76 header_threshhold = -1;
77 break;
78 case 'c':
79 count_bytes = 1;
80 /* fall through */
81#endif
82 case 'n':
83 p = optarg;
Rob Landleyf8fd4db2006-01-30 01:30:39 +000084#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000086#endif
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000087
88#if !ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 count = bb_xgetularg10(p);
"Vladimir N. Oleynik"e75b41d2006-01-30 11:15:11 +000090#else
91 count = bb_xgetularg_bnd_sfx(p, 10,
92 0, ULONG_MAX,
93 head_suffixes);
94#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 break;
96 default:
97 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
99 }
100
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 argv += optind;
102 if (!*argv) {
103 *--argv = "-";
John Beppu3157b1f1999-12-10 07:42:50 +0000104 }
John Beppu3157b1f1999-12-10 07:42:50 +0000105
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 fmt = header_fmt_str + 1;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +0000107#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 if (argc - optind <= header_threshhold) {
109 header_threshhold = 0;
110 }
111#else
112 if (argc <= optind + 1) {
113 fmt += 11;
114 }
115 /* Now define some things here to avoid #ifdefs in the code below.
116 * These should optimize out of the if conditions below. */
117#define header_threshhold 1
118#define count_bytes 0
119#endif
120
121 do {
122 if ((fp = bb_wfopen_input(*argv)) != NULL) {
123 if (fp == stdin) {
124 *argv = (char *) bb_msg_standard_input;
125 }
126 if (header_threshhold) {
127 bb_printf(fmt, *argv);
128 }
129 i = count;
130 while (i && ((c = getc(fp)) != EOF)) {
131 if (count_bytes || (c == '\n')) {
132 --i;
133 }
134 putchar(c);
135 }
136 if (bb_fclose_nonstdin(fp)) {
137 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
138 retval = EXIT_FAILURE;
139 }
140 bb_xferror_stdout();
141 }
142 fmt = header_fmt_str;
143 } while (*++argv);
144
145 bb_fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000146}