silly switch style fix
[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 tarball for details.
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 #include "busybox.h"
15
16 static const char head_opts[] =
17         "n:"
18 #if ENABLE_FEATURE_FANCY_HEAD
19         "c:qv"
20 #endif
21         ;
22
23 #if ENABLE_FEATURE_FANCY_HEAD
24 static const struct suffix_mult head_suffixes[] = {
25         { "b", 512 },
26         { "k", 1024 },
27         { "m", 1024*1024 },
28         { NULL, 0 }
29 };
30 #endif
31
32 static const char header_fmt_str[] = "\n==> %s <==\n";
33
34 int head_main(int argc, char **argv)
35 {
36         unsigned long count = 10;
37         unsigned long i;
38 #if ENABLE_FEATURE_FANCY_HEAD
39         int count_bytes = 0;
40         int header_threshhold = 1;
41 #endif
42
43         FILE *fp;
44         const char *fmt;
45         char *p;
46         int opt;
47         int c;
48         int retval = EXIT_SUCCESS;
49
50 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
51         /* Allow legacy syntax of an initial numeric option without -n. */
52         if ((argc > 1) && (argv[1][0] == '-')
53                 /* && (isdigit)(argv[1][1]) */
54                 && (((unsigned int)(argv[1][1] - '0')) <= 9)
55         ) {
56                 --argc;
57                 ++argv;
58                 p = (*argv) + 1;
59                 goto GET_COUNT;
60         }
61 #endif
62
63         /* No size benefit in converting this to bb_getopt_ulflags */
64         while ((opt = getopt(argc, argv, head_opts)) > 0) {
65                 switch (opt) {
66 #if ENABLE_FEATURE_FANCY_HEAD
67                         case 'q':
68                                 header_threshhold = INT_MAX;
69                                 break;
70                         case 'v':
71                                 header_threshhold = -1;
72                                 break;
73                         case 'c':
74                                 count_bytes = 1;
75                                 /* fall through */
76 #endif
77                         case 'n':
78                                 p = optarg;
79 #if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_HEAD
80                         GET_COUNT:
81 #endif
82
83 #if !ENABLE_FEATURE_FANCY_HEAD
84                                 count = bb_xgetularg10(p);
85 #else
86                                 count = bb_xgetularg_bnd_sfx(p, 10,
87                                                                 0, ULONG_MAX,
88                                                                 head_suffixes);
89 #endif
90                                 break;
91                         default:
92                                 bb_show_usage();
93                 }
94         }
95
96         argv += optind;
97         if (!*argv) {
98                 *--argv = "-";
99         }
100
101         fmt = header_fmt_str + 1;
102 #if ENABLE_FEATURE_FANCY_HEAD
103         if (argc - optind <= header_threshhold) {
104                 header_threshhold = 0;
105         }
106 #else
107         if (argc <= optind + 1) {
108                 fmt += 11;
109         }
110         /* Now define some things here to avoid #ifdefs in the code below.
111          * These should optimize out of the if conditions below. */
112 #define header_threshhold   1
113 #define count_bytes         0
114 #endif
115
116         do {
117                 if ((fp = bb_wfopen_input(*argv)) != NULL) {
118                         if (fp == stdin) {
119                                 *argv = (char *) bb_msg_standard_input;
120                         }
121                         if (header_threshhold) {
122                                 bb_printf(fmt, *argv);
123                         }
124                         i = count;
125                         while (i && ((c = getc(fp)) != EOF)) {
126                                 if (count_bytes || (c == '\n')) {
127                                         --i;
128                                 }
129                                 putchar(c);
130                         }
131                         if (bb_fclose_nonstdin(fp)) {
132                                 bb_perror_msg("%s", *argv);     /* Avoid multibyte problems. */
133                                 retval = EXIT_FAILURE;
134                         }
135                         xferror_stdout();
136                 }
137                 fmt = header_fmt_str;
138         } while (*++argv);
139
140         bb_fflush_stdout_and_exit(retval);
141 }