diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-05 18:17:17 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-04-05 18:17:17 +0200 |
commit | d88f94a5df3a2edb8ba56fab5c13674b452f87ab (patch) | |
tree | abfed9c655cf1c436dac9e4089d7838c36e6e221 /coreutils | |
parent | 5d561ef6349b4b6e1d12ad6638acb46abf1eeca6 (diff) | |
download | busybox-w32-d88f94a5df3a2edb8ba56fab5c13674b452f87ab.tar.gz busybox-w32-d88f94a5df3a2edb8ba56fab5c13674b452f87ab.tar.bz2 busybox-w32-d88f94a5df3a2edb8ba56fab5c13674b452f87ab.zip |
nl: new applet; also implement cat -nb (similar functionality to nl)
function old new delta
nl_main - 201 +201
print_numbered_lines - 115 +115
cat_main 36 149 +113
static.nl_longopts - 106 +106
packed_usage 31081 31182 +101
applet_main 1488 1492 +4
applet_names 2575 2578 +3
applet_suid 93 94 +1
applet_install_loc 186 187 +1
------------------------------------------------------------------------------
(add/remove: 4/0 grow/shrink: 6/0 up/down: 645/0) Total: 645 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/cat.c | 43 | ||||
-rw-r--r-- | coreutils/nl.c | 100 |
2 files changed, 139 insertions, 4 deletions
diff --git a/coreutils/cat.c b/coreutils/cat.c index 65978887e..4169d9516 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c | |||
@@ -15,15 +15,31 @@ | |||
15 | 15 | ||
16 | //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat)) | 16 | //applet:IF_CAT(APPLET_NOFORK(cat, cat, BB_DIR_BIN, BB_SUID_DROP, cat)) |
17 | 17 | ||
18 | //kbuild:lib-$(CONFIG_CAT) += cat.o | 18 | //kbuild:lib-$(CONFIG_CAT) += cat.o |
19 | // For -n: | ||
20 | //kbuild:lib-$(CONFIG_CAT) += nl.o | ||
19 | 21 | ||
20 | /* BB_AUDIT SUSv3 compliant */ | 22 | /* BB_AUDIT SUSv3 compliant */ |
21 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */ | 23 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */ |
22 | 24 | ||
23 | //usage:#define cat_trivial_usage | 25 | //usage:#define cat_trivial_usage |
24 | //usage: "[FILE]..." | 26 | //usage: "[-n] [FILE]..." |
25 | //usage:#define cat_full_usage "\n\n" | 27 | //usage:#define cat_full_usage "\n\n" |
26 | //usage: "Concatenate FILEs and print them to stdout" | 28 | //usage: "Concatenate FILEs and print them to stdout" |
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 | */ | ||
27 | //usage: | 43 | //usage: |
28 | //usage:#define cat_example_usage | 44 | //usage:#define cat_example_usage |
29 | //usage: "$ cat /proc/uptime\n" | 45 | //usage: "$ cat /proc/uptime\n" |
@@ -61,7 +77,26 @@ int bb_cat(char **argv) | |||
61 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 77 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
62 | int cat_main(int argc UNUSED_PARAM, char **argv) | 78 | int cat_main(int argc UNUSED_PARAM, char **argv) |
63 | { | 79 | { |
64 | getopt32(argv, "u"); | 80 | struct number_state ns; |
81 | unsigned opt; | ||
82 | |||
83 | /* -u is ignored */ | ||
84 | opt = getopt32(argv, "nbu"); | ||
65 | argv += optind; | 85 | argv += optind; |
66 | return bb_cat(argv); | 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); | ||
67 | } | 102 | } |
diff --git a/coreutils/nl.c b/coreutils/nl.c new file mode 100644 index 000000000..97931f334 --- /dev/null +++ b/coreutils/nl.c | |||
@@ -0,0 +1,100 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
4 | */ | ||
5 | //config:config NL | ||
6 | //config: bool "nl" | ||
7 | //config: default y | ||
8 | //config: help | ||
9 | //config: nl is used to number lines of files. | ||
10 | |||
11 | //applet:IF_UNIQ(APPLET(nl, BB_DIR_USR_BIN, BB_SUID_DROP)) | ||
12 | |||
13 | //kbuild:lib-$(CONFIG_UNIQ) += nl.o | ||
14 | |||
15 | //usage:#define nl_trivial_usage | ||
16 | //usage: "[OPTIONS] [FILE]..." | ||
17 | //usage:#define nl_full_usage "\n\n" | ||
18 | //usage: "Write FILEs to standard output with line numbers added\n" | ||
19 | //usage: "\n -b STYLE Which lines to number - a: all, t: nonempty, n: none" | ||
20 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TODO: support "pBRE": number only lines thatmatch regexp BRE" | ||
21 | ////usage: "\n -f STYLE footer lines" | ||
22 | ////usage: "\n -h STYLE header lines" | ||
23 | ////usage: "\n -d CC use CC for separating logical pages" | ||
24 | //usage: "\n -i N Line number increment" | ||
25 | ////usage: "\n -l NUMBER group of NUMBER empty lines counted as one" | ||
26 | ////usage: "\n -n FORMAT lneft justified, no leading zeros; rn or rz" | ||
27 | ////usage: "\n -p do not reset line numbers at logical pages (huh?)" | ||
28 | //usage: "\n -s STRING Use STRING as line number separator" | ||
29 | //usage: "\n -v N Start from N" | ||
30 | //usage: "\n -w N Width of line numbers" | ||
31 | |||
32 | /* By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn */ | ||
33 | |||
34 | #include "libbb.h" | ||
35 | |||
36 | void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename) | ||
37 | { | ||
38 | FILE *fp = fopen_or_warn_stdin(filename); | ||
39 | unsigned N = ns->start; | ||
40 | char *line; | ||
41 | |||
42 | while ((line = xmalloc_fgetline(fp)) != NULL) { | ||
43 | if (ns->all | ||
44 | || (ns->nonempty && line[0]) | ||
45 | ) { | ||
46 | printf("%*u%s%s\n", ns->width, N, ns->sep, line); | ||
47 | N += ns->inc; | ||
48 | } else if (ns->empty_str) | ||
49 | fputs(ns->empty_str, stdout); | ||
50 | free(line); | ||
51 | } | ||
52 | |||
53 | fclose(fp); | ||
54 | } | ||
55 | |||
56 | int nl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
57 | int nl_main(int argc UNUSED_PARAM, char **argv) | ||
58 | { | ||
59 | unsigned opt; | ||
60 | struct number_state ns; | ||
61 | const char *opt_b = "t"; | ||
62 | enum { | ||
63 | OPT_p = (1 << 0), | ||
64 | }; | ||
65 | #if ENABLE_LONG_OPTS | ||
66 | static const char nl_longopts[] ALIGN1 = | ||
67 | "body-numbering\0" Required_argument "b" | ||
68 | // "footer-numbering\0" Required_argument "f" - not implemented yet | ||
69 | // "header-numbering\0" Required_argument "h" - not implemented yet | ||
70 | // "section-delimiter\0" Required_argument "d" - not implemented yet | ||
71 | "line-increment\0" Required_argument "i" | ||
72 | // "join-blank-lines\0" Required_argument "l" - not implemented yet | ||
73 | // "number-format\0" Required_argument "n" - not implemented yet | ||
74 | "no-renumber\0" No_argument "p" // no-op so far | ||
75 | "number-separator\0" Required_argument "s" | ||
76 | "starting-line-number\0"Required_argument "v" | ||
77 | "number-width\0" Required_argument "w" | ||
78 | ; | ||
79 | |||
80 | applet_long_options = nl_longopts; | ||
81 | #endif | ||
82 | ns.width = 6; | ||
83 | ns.start = 1; | ||
84 | ns.inc = 1; | ||
85 | ns.sep = "\t"; | ||
86 | opt = getopt32(argv, "pw:+s:v:+i:+b:", &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b); | ||
87 | ns.all = (opt_b[0] == 'a'); | ||
88 | ns.nonempty = (opt_b[0] == 't'); | ||
89 | ns.empty_str = xasprintf("%*s\n", ns.width + strlen(ns.sep), ""); | ||
90 | |||
91 | argv += optind; | ||
92 | if (!*argv) | ||
93 | *--argv = (char*)"-"; | ||
94 | |||
95 | do { | ||
96 | print_numbered_lines(&ns, *argv); | ||
97 | } while (*++argv); | ||
98 | |||
99 | fflush_stdout_and_exit(EXIT_SUCCESS); | ||
100 | } | ||