1 /* vi: set sw=4 ts=4: */
3 * cat implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
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.
16 //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat))
18 //kbuild:lib-$(CONFIG_CAT) += cat.o
20 //kbuild:lib-$(CONFIG_CAT) += nl.o
22 /* BB_AUDIT SUSv3 compliant */
23 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
25 //usage:#define cat_trivial_usage
26 //usage: "[-n] [FILE]..."
27 //usage:#define cat_full_usage "\n\n"
28 //usage: "Concatenate FILEs and print them to stdout"
29 //usage: "\n -n Number output lines"
31 Longopts not implemented yet:
32 --number-nonblank number nonempty output lines, overrides -n
33 --number number all output lines
35 -A, --show-all equivalent to -vET
37 -E, --show-ends display $ at end of each line
38 -s, --squeeze-blank suppress repeated empty output lines
40 -T, --show-tabs display TAB characters as ^I
41 -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
44 //usage:#define cat_example_usage
45 //usage: "$ cat /proc/uptime\n"
46 //usage: "110716.72 17.67"
50 /* This is a NOFORK applet. Be very careful! */
53 int bb_cat(char **argv)
56 int retval = EXIT_SUCCESS;
59 argv = (char**) &bb_argv_dash;
62 fd = open_or_warn_stdin(*argv);
64 /* This is not a xfunc - never exits */
65 off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
66 if (fd != STDIN_FILENO)
71 retval = EXIT_FAILURE;
77 int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int cat_main(int argc UNUSED_PARAM, char **argv)
80 struct number_state ns;
84 opt = getopt32(argv, "nbu");
86 if (!(opt & 3)) /* no -n or -b */
96 ns.all = !(opt & 2); /* -n without -b */
97 ns.nonempty = (opt & 2); /* -b (with or without -n) */
99 print_numbered_lines(&ns, *argv);
101 fflush_stdout_and_exit(EXIT_SUCCESS);