blob: 4169d9516e63e1d282cf57865d727d41b3d6b6e2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * cat implementation for busybox
Eric Andersencc8ed391999-10-05 16:24:54 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersencc8ed391999-10-05 16:24:54 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 */
Denys Vlasenkoe4070cb2010-06-04 19:59:49 +02009//config:config CAT
10//config: bool "cat"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020011//config: default y
Denys Vlasenkoe4070cb2010-06-04 19:59:49 +020012//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
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016//applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat))
17
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020018//kbuild:lib-$(CONFIG_CAT) += cat.o
19// For -n:
20//kbuild:lib-$(CONFIG_CAT) += nl.o
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010021
22/* BB_AUDIT SUSv3 compliant */
23/* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
24
Pere Orga34425382011-03-31 14:43:25 +020025//usage:#define cat_trivial_usage
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020026//usage: "[-n] [FILE]..."
Pere Orga34425382011-03-31 14:43:25 +020027//usage:#define cat_full_usage "\n\n"
28//usage: "Concatenate FILEs and print them to stdout"
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020029//usage: "\n -n Number output lines"
30/*
31 Longopts not implemented yet:
32 --number-nonblank number nonempty output lines, overrides -n
33 --number number all output lines
34 Not implemented yet:
35 -A, --show-all equivalent to -vET
36 -e equivalent to -vE
37 -E, --show-ends display $ at end of each line
38 -s, --squeeze-blank suppress repeated empty output lines
39 -t equivalent to -vT
40 -T, --show-tabs display TAB characters as ^I
41 -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
42*/
Pere Orga34425382011-03-31 14:43:25 +020043//usage:
44//usage:#define cat_example_usage
45//usage: "$ cat /proc/uptime\n"
46//usage: "110716.72 17.67"
47
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000048#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Denis Vlasenko99912ca2007-04-10 15:43:37 +000050/* This is a NOFORK applet. Be very careful! */
51
52
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000053int bb_cat(char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000054{
Denis Vlasenko50f7f442007-04-11 23:20:53 +000055 int fd;
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 int retval = EXIT_SUCCESS;
Matt Kraaie7c1af12000-09-27 03:01:40 +000057
Denis Vlasenko99912ca2007-04-10 15:43:37 +000058 if (!*argv)
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000059 argv = (char**) &bb_argv_dash;
Denis Vlasenkoe865e812006-12-21 13:24:58 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000062 fd = open_or_warn_stdin(*argv);
Denis Vlasenko50f7f442007-04-11 23:20:53 +000063 if (fd >= 0) {
Denis Vlasenko97bd0e02008-02-08 15:41:01 +000064 /* This is not a xfunc - never exits */
Denis Vlasenko50f7f442007-04-11 23:20:53 +000065 off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);
66 if (fd != STDIN_FILENO)
67 close(fd);
Denis Vlasenko714701c2006-12-22 00:21:07 +000068 if (r >= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 retval = EXIT_FAILURE;
72 } while (*++argv);
Erik Andersen029011b2000-03-04 21:19:32 +000073
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 return retval;
75}
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000076
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000077int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000078int cat_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000079{
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020080 struct number_state ns;
81 unsigned opt;
82
83 /* -u is ignored */
84 opt = getopt32(argv, "nbu");
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +000085 argv += optind;
Denys Vlasenkod88f94a2017-04-05 18:17:17 +020086 if (!(opt & 3)) /* no -n or -b */
87 return bb_cat(argv);
88
89 if (!*argv)
90 *--argv = (char*)"-";
91 ns.width = 6;
92 ns.start = 1;
93 ns.inc = 1;
94 ns.sep = "\t";
95 ns.empty_str = "\n";
96 ns.all = !(opt & 2); /* -n without -b */
97 ns.nonempty = (opt & 2); /* -b (with or without -n) */
98 do {
99 print_numbered_lines(&ns, *argv);
100 } while (*++argv);
101 fflush_stdout_and_exit(EXIT_SUCCESS);
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000102}