Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * cat implementation for busybox |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 9 | //config:config CAT |
| 10 | //config: bool "cat" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 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 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 16 | //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat)) |
| 17 | |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame^] | 18 | //kbuild:lib-$(CONFIG_CAT) += cat.o |
| 19 | // For -n: |
| 20 | //kbuild:lib-$(CONFIG_CAT) += nl.o |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 21 | |
| 22 | /* BB_AUDIT SUSv3 compliant */ |
| 23 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */ |
| 24 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 25 | //usage:#define cat_trivial_usage |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame^] | 26 | //usage: "[-n] [FILE]..." |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 27 | //usage:#define cat_full_usage "\n\n" |
| 28 | //usage: "Concatenate FILEs and print them to stdout" |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame^] | 29 | //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 Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 43 | //usage: |
| 44 | //usage:#define cat_example_usage |
| 45 | //usage: "$ cat /proc/uptime\n" |
| 46 | //usage: "110716.72 17.67" |
| 47 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 48 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 50 | /* This is a NOFORK applet. Be very careful! */ |
| 51 | |
| 52 | |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 53 | int bb_cat(char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | { |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 55 | int fd; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | int retval = EXIT_SUCCESS; |
Matt Kraai | e7c1af1 | 2000-09-27 03:01:40 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 58 | if (!*argv) |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 59 | argv = (char**) &bb_argv_dash; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 60 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 62 | fd = open_or_warn_stdin(*argv); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 63 | if (fd >= 0) { |
Denis Vlasenko | 97bd0e0 | 2008-02-08 15:41:01 +0000 | [diff] [blame] | 64 | /* This is not a xfunc - never exits */ |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 65 | off_t r = bb_copyfd_eof(fd, STDOUT_FILENO); |
| 66 | if (fd != STDIN_FILENO) |
| 67 | close(fd); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 68 | if (r >= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | retval = EXIT_FAILURE; |
| 72 | } while (*++argv); |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 73 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | return retval; |
| 75 | } |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 77 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 78 | int cat_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 79 | { |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame^] | 80 | struct number_state ns; |
| 81 | unsigned opt; |
| 82 | |
| 83 | /* -u is ignored */ |
| 84 | opt = getopt32(argv, "nbu"); |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 85 | argv += optind; |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame^] | 86 | 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 Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 102 | } |