f49320ef4caa570f21aed177f8cd3f820d3f08bf
[oweals/busybox.git] / coreutils / head.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * head implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
10 /* 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
14 //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"
19 //usage:     "\nOptions:"
20 //usage:     "\n        -n N[kbm]       Print first N lines"
21 //usage:        IF_FEATURE_FANCY_HEAD(
22 //usage:     "\n        -c N[kbm]       Print first N bytes"
23 //usage:     "\n        -q              Never print headers"
24 //usage:     "\n        -v              Always print headers"
25 //usage:        )
26 //usage:     "\n"
27 //usage:     "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
28 //usage:
29 //usage:#define head_example_usage
30 //usage:       "$ head -n 2 /etc/passwd\n"
31 //usage:       "root:x:0:0:root:/root:/bin/bash\n"
32 //usage:       "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n"
33
34 #include "libbb.h"
35
36 /* This is a NOEXEC applet. Be very careful! */
37
38 static const char head_opts[] ALIGN1 =
39         "n:"
40 #if ENABLE_FEATURE_FANCY_HEAD
41         "c:qv"
42 #endif
43         ;
44
45 static const struct suffix_mult head_suffixes[] = {
46         { "b", 512 },
47         { "k", 1024 },
48         { "m", 1024*1024 },
49         { "", 0 }
50 };
51
52 #define header_fmt_str "\n==> %s <==\n"
53
54 int head_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
55 int head_main(int argc, char **argv)
56 {
57         unsigned long count = 10;
58         unsigned long i;
59 #if ENABLE_FEATURE_FANCY_HEAD
60         int count_bytes = 0;
61         int header_threshhold = 1;
62 #endif
63         FILE *fp;
64         const char *fmt;
65         char *p;
66         int opt;
67         int c;
68         int retval = EXIT_SUCCESS;
69
70 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
71         /* Allow legacy syntax of an initial numeric option without -n. */
72         if (argv[1] && argv[1][0] == '-'
73          && isdigit(argv[1][1])
74         ) {
75                 --argc;
76                 ++argv;
77                 p = (*argv) + 1;
78                 goto GET_COUNT;
79         }
80 #endif
81
82         /* No size benefit in converting this to getopt32 */
83         while ((opt = getopt(argc, argv, head_opts)) > 0) {
84                 switch (opt) {
85 #if ENABLE_FEATURE_FANCY_HEAD
86                 case 'q':
87                         header_threshhold = INT_MAX;
88                         break;
89                 case 'v':
90                         header_threshhold = -1;
91                         break;
92                 case 'c':
93                         count_bytes = 1;
94                         /* fall through */
95 #endif
96                 case 'n':
97                         p = optarg;
98 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
99  GET_COUNT:
100 #endif
101                         count = xatoul_sfx(p, head_suffixes);
102                         break;
103                 default:
104                         bb_show_usage();
105                 }
106         }
107
108         argc -= optind;
109         argv += optind;
110         if (!*argv)
111                 *--argv = (char*)"-";
112
113         fmt = header_fmt_str + 1;
114 #if ENABLE_FEATURE_FANCY_HEAD
115         if (argc <= header_threshhold) {
116                 header_threshhold = 0;
117         }
118 #else
119         if (argc <= 1) {
120                 fmt += 11; /* "" */
121         }
122         /* Now define some things here to avoid #ifdefs in the code below.
123          * These should optimize out of the if conditions below. */
124 #define header_threshhold   1
125 #define count_bytes         0
126 #endif
127
128         do {
129                 fp = fopen_or_warn_stdin(*argv);
130                 if (fp) {
131                         if (fp == stdin) {
132                                 *argv = (char *) bb_msg_standard_input;
133                         }
134                         if (header_threshhold) {
135                                 printf(fmt, *argv);
136                         }
137                         i = count;
138                         while (i && ((c = getc(fp)) != EOF)) {
139                                 if (count_bytes || (c == '\n')) {
140                                         --i;
141                                 }
142                                 putchar(c);
143                         }
144                         if (fclose_if_not_stdin(fp)) {
145                                 bb_simple_perror_msg(*argv);
146                                 retval = EXIT_FAILURE;
147                         }
148                         die_if_ferror_stdout();
149                 } else {
150                         retval = EXIT_FAILURE;
151                 }
152                 fmt = header_fmt_str;
153         } while (*++argv);
154
155         fflush_stdout_and_exit(retval);
156 }