fix errors found with make_single_applets.sh
[oweals/busybox.git] / coreutils / cat.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * cat implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config CAT
10 //config:       bool "cat"
11 //config:       default y
12 //config:       help
13 //config:         cat is used to concatenate files and print them to the standard
14 //config:         output. Enable this option if you wish to enable the 'cat' utility.
15
16 //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat))
17
18 //kbuild:lib-$(CONFIG_CAT) += cat.o
19
20 /* BB_AUDIT SUSv3 compliant */
21 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
22
23 //usage:#define cat_trivial_usage
24 //usage:       "[-n] [FILE]..."
25 //usage:#define cat_full_usage "\n\n"
26 //usage:       "Concatenate FILEs and print them to stdout"
27 //usage:     "\n        -n      Number output lines"
28 /*
29   Longopts not implemented yet:
30      --number-nonblank    number nonempty output lines, overrides -n
31      --number             number all output lines
32   Not implemented yet:
33   -A, --show-all           equivalent to -vET
34   -e                       equivalent to -vE
35   -E, --show-ends          display $ at end of each line
36   -s, --squeeze-blank      suppress repeated empty output lines
37   -t                       equivalent to -vT
38   -T, --show-tabs          display TAB characters as ^I
39   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
40 */
41 //usage:
42 //usage:#define cat_example_usage
43 //usage:       "$ cat /proc/uptime\n"
44 //usage:       "110716.72 17.67"
45
46 #include "libbb.h"
47
48 /* This is a NOFORK applet. Be very careful! */
49
50 int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
51 int cat_main(int argc UNUSED_PARAM, char **argv)
52 {
53         struct number_state ns;
54         unsigned opt;
55
56         /* -u is ignored */
57         opt = getopt32(argv, "nbu");
58         argv += optind;
59         if (!(opt & 3)) /* no -n or -b */
60                 return bb_cat(argv);
61
62         if (!*argv)
63                 *--argv = (char*)"-";
64         ns.width = 6;
65         ns.start = 1;
66         ns.inc = 1;
67         ns.sep = "\t";
68         ns.empty_str = "\n";
69         ns.all = !(opt & 2); /* -n without -b */
70         ns.nonempty = (opt & 2); /* -b (with or without -n) */
71         do {
72                 print_numbered_lines(&ns, *argv);
73         } while (*++argv);
74         fflush_stdout_and_exit(EXIT_SUCCESS);
75 }