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/nl.c | |
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/nl.c')
-rw-r--r-- | coreutils/nl.c | 100 |
1 files changed, 100 insertions, 0 deletions
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 | } | ||