blob: 17d3ef890166aabd36dcaa4deb66a06e1eb68e5f [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
Manuel Novoa III cad53642003-03-19 09:13:01 +000028static const char header_fmt_str[] = "\n==> %s <==\n";
John Beppu3157b1f1999-12-10 07:42:50 +000029
Erik Andersene49d5ec2000-02-08 19:58:47 +000030int head_main(int argc, char **argv)
John Beppu3157b1f1999-12-10 07:42:50 +000031{
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 unsigned long count = 10;
33 unsigned long i;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000034#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 int count_bytes = 0;
36 int header_threshhold = 1;
37#endif
38
Matt Kraaic0321f92000-09-27 04:09:22 +000039 FILE *fp;
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 const char *fmt;
41 char *p;
42 int opt;
43 int c;
44 int retval = EXIT_SUCCESS;
John Beppu3157b1f1999-12-10 07:42:50 +000045
Rob Landleyf8fd4db2006-01-30 01:30:39 +000046#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 /* Allow legacy syntax of an initial numeric option without -n. */
48 if ((argc > 1) && (argv[1][0] == '-')
49 /* && (isdigit)(argv[1][1]) */
50 && (((unsigned int)(argv[1][1] - '0')) <= 9)
51 ) {
52 --argc;
53 ++argv;
54 p = (*argv) + 1;
55 goto GET_COUNT;
Robert Griebl13c26fc2002-05-17 22:18:04 +000056 }
Glenn L McGrath0bd02572005-12-11 03:09:05 +000057#endif
Robert Griebl13c26fc2002-05-17 22:18:04 +000058
Glenn L McGrath0bd02572005-12-11 03:09:05 +000059 /* No size benefit in converting this to bb_getopt_ulflags */
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 while ((opt = getopt(argc, argv, head_opts)) > 0) {
61 switch(opt) {
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000062#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 case 'q':
64 header_threshhold = INT_MAX;
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 break;
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 case 'v':
67 header_threshhold = -1;
68 break;
69 case 'c':
70 count_bytes = 1;
71 /* fall through */
72#endif
73 case 'n':
74 p = optarg;
Rob Landleyf8fd4db2006-01-30 01:30:39 +000075#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 GET_COUNT:
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000077#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 count = bb_xgetularg10(p);
79 break;
80 default:
81 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 }
83 }
84
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 argv += optind;
86 if (!*argv) {
87 *--argv = "-";
John Beppu3157b1f1999-12-10 07:42:50 +000088 }
John Beppu3157b1f1999-12-10 07:42:50 +000089
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 fmt = header_fmt_str + 1;
Bernhard Reutner-Fischer5816ccb2005-12-13 10:48:45 +000091#if ENABLE_FEATURE_FANCY_HEAD
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 if (argc - optind <= header_threshhold) {
93 header_threshhold = 0;
94 }
95#else
96 if (argc <= optind + 1) {
97 fmt += 11;
98 }
99 /* Now define some things here to avoid #ifdefs in the code below.
100 * These should optimize out of the if conditions below. */
101#define header_threshhold 1
102#define count_bytes 0
103#endif
104
105 do {
106 if ((fp = bb_wfopen_input(*argv)) != NULL) {
107 if (fp == stdin) {
108 *argv = (char *) bb_msg_standard_input;
109 }
110 if (header_threshhold) {
111 bb_printf(fmt, *argv);
112 }
113 i = count;
114 while (i && ((c = getc(fp)) != EOF)) {
115 if (count_bytes || (c == '\n')) {
116 --i;
117 }
118 putchar(c);
119 }
120 if (bb_fclose_nonstdin(fp)) {
121 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
122 retval = EXIT_FAILURE;
123 }
124 bb_xferror_stdout();
125 }
126 fmt = header_fmt_str;
127 } while (*++argv);
128
129 bb_fflush_stdout_and_exit(retval);
Matt Kraaic0321f92000-09-27 04:09:22 +0000130}