diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-14 10:47:18 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-14 10:47:18 +0200 |
commit | 75e90b15482184db83f03c67b53d4220888c6c9d (patch) | |
tree | 8307ba71f584715fcad8beb4373f9318e1f9ec13 | |
parent | cc86b2ad965bff071185edbb77b5a6ea45023e43 (diff) | |
download | busybox-w32-75e90b15482184db83f03c67b53d4220888c6c9d.tar.gz busybox-w32-75e90b15482184db83f03c67b53d4220888c6c9d.tar.bz2 busybox-w32-75e90b15482184db83f03c67b53d4220888c6c9d.zip |
cat: fix "cat -An" ignoring -n; make numbering go througn all files
function old new delta
cat_main 418 428 +10
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/cat.c | 44 | ||||
-rw-r--r-- | coreutils/nl.c | 20 | ||||
-rw-r--r-- | libbb/print_numbered_lines.c | 1 |
3 files changed, 30 insertions, 35 deletions
diff --git a/coreutils/cat.c b/coreutils/cat.c index 178f96b09..a9ba68d6b 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c | |||
@@ -101,27 +101,32 @@ | |||
101 | * to achieve "cat -v" effect. The actual effect would be "users pissed off | 101 | * to achieve "cat -v" effect. The actual effect would be "users pissed off |
102 | * by gratuitous incompatibility". | 102 | * by gratuitous incompatibility". |
103 | */ | 103 | */ |
104 | #define CATV_OPT_e (1<<0) | 104 | #define CAT_OPT_e (1<<0) |
105 | #define CATV_OPT_t (1<<1) | 105 | #define CAT_OPT_t (1<<1) |
106 | #define CATV_OPT_v (1<<2) | 106 | #define CAT_OPT_v (1<<2) |
107 | /* -A occupies bit (1<<3) */ | ||
108 | #define CAT_OPT_n ((1<<4) * ENABLE_FEATURE_CATN) | ||
109 | #define CAT_OPT_b ((1<<5) * ENABLE_FEATURE_CATN) | ||
107 | static int catv(unsigned opts, char **argv) | 110 | static int catv(unsigned opts, char **argv) |
108 | { | 111 | { |
109 | int retval = EXIT_SUCCESS; | 112 | int retval = EXIT_SUCCESS; |
110 | int fd; | 113 | int fd; |
114 | #if ENABLE_FEATURE_CATN | ||
115 | unsigned lineno = 0; | ||
116 | unsigned eol_char = (opts & (CAT_OPT_n|CAT_OPT_b)) ? '\n' : 0x100; | ||
117 | unsigned skip_num_on = (opts & CAT_OPT_b) ? '\n' : 0x100; | ||
118 | bool eol_seen = 1; | ||
119 | #endif | ||
111 | 120 | ||
112 | BUILD_BUG_ON(CATV_OPT_e != VISIBLE_ENDLINE); | 121 | BUILD_BUG_ON(CAT_OPT_e != VISIBLE_ENDLINE); |
113 | BUILD_BUG_ON(CATV_OPT_t != VISIBLE_SHOW_TABS); | 122 | BUILD_BUG_ON(CAT_OPT_t != VISIBLE_SHOW_TABS); |
114 | #if 0 /* These consts match, we can just pass "opts" to visible() */ | 123 | #if 0 /* These consts match, we can just pass "opts" to visible() */ |
115 | if (opts & CATV_OPT_e) | 124 | if (opts & CAT_OPT_e) |
116 | flags |= VISIBLE_ENDLINE; | 125 | flags |= VISIBLE_ENDLINE; |
117 | if (opts & CATV_OPT_t) | 126 | if (opts & CAT_OPT_t) |
118 | flags |= VISIBLE_SHOW_TABS; | 127 | flags |= VISIBLE_SHOW_TABS; |
119 | #endif | 128 | #endif |
120 | 129 | ||
121 | /* Read from stdin if there's nothing else to do. */ | ||
122 | if (!argv[0]) | ||
123 | *--argv = (char*)"-"; | ||
124 | |||
125 | #define read_buf bb_common_bufsiz1 | 130 | #define read_buf bb_common_bufsiz1 |
126 | setup_common_bufsiz(); | 131 | setup_common_bufsiz(); |
127 | do { | 132 | do { |
@@ -141,6 +146,11 @@ static int catv(unsigned opts, char **argv) | |||
141 | for (i = 0; i < res; i++) { | 146 | for (i = 0; i < res; i++) { |
142 | unsigned char c = read_buf[i]; | 147 | unsigned char c = read_buf[i]; |
143 | char buf[sizeof("M-^c")]; | 148 | char buf[sizeof("M-^c")]; |
149 | #if ENABLE_FEATURE_CATN | ||
150 | if (eol_seen && c != skip_num_on) | ||
151 | printf("%6u ", ++lineno); | ||
152 | eol_seen = (c == eol_char); | ||
153 | #endif | ||
144 | visible(c, buf, opts); | 154 | visible(c, buf, opts); |
145 | fputs(buf, stdout); | 155 | fputs(buf, stdout); |
146 | } | 156 | } |
@@ -151,12 +161,13 @@ static int catv(unsigned opts, char **argv) | |||
151 | 161 | ||
152 | fflush_stdout_and_exit(retval); | 162 | fflush_stdout_and_exit(retval); |
153 | } | 163 | } |
164 | #undef CAT_OPT_n | ||
165 | #undef CAT_OPT_b | ||
154 | #endif | 166 | #endif |
155 | 167 | ||
156 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 168 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
157 | int cat_main(int argc UNUSED_PARAM, char **argv) | 169 | int cat_main(int argc UNUSED_PARAM, char **argv) |
158 | { | 170 | { |
159 | struct number_state ns; | ||
160 | unsigned opts; | 171 | unsigned opts; |
161 | 172 | ||
162 | IF_FEATURE_CATV(opt_complementary = "Aetv"; /* -A == -vet */) | 173 | IF_FEATURE_CATV(opt_complementary = "Aetv"; /* -A == -vet */) |
@@ -164,10 +175,13 @@ int cat_main(int argc UNUSED_PARAM, char **argv) | |||
164 | opts = getopt32(argv, IF_FEATURE_CATV("etvA") IF_FEATURE_CATN("nb") "u"); | 175 | opts = getopt32(argv, IF_FEATURE_CATV("etvA") IF_FEATURE_CATN("nb") "u"); |
165 | argv += optind; | 176 | argv += optind; |
166 | 177 | ||
178 | /* Read from stdin if there's nothing else to do. */ | ||
179 | if (!argv[0]) | ||
180 | *--argv = (char*)"-"; | ||
181 | |||
167 | #if ENABLE_FEATURE_CATV | 182 | #if ENABLE_FEATURE_CATV |
168 | if (opts & 7) | 183 | if (opts & 7) |
169 | return catv(opts, argv); | 184 | return catv(opts, argv); |
170 | //BUG: -v,-e,-t,-A ignore -nb | ||
171 | opts >>= 4; | 185 | opts >>= 4; |
172 | #endif | 186 | #endif |
173 | 187 | ||
@@ -175,8 +189,8 @@ int cat_main(int argc UNUSED_PARAM, char **argv) | |||
175 | # define CAT_OPT_n (1<<0) | 189 | # define CAT_OPT_n (1<<0) |
176 | # define CAT_OPT_b (1<<1) | 190 | # define CAT_OPT_b (1<<1) |
177 | if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */ | 191 | if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */ |
178 | if (!*argv) | 192 | struct number_state ns; |
179 | *--argv = (char*)"-"; | 193 | |
180 | ns.width = 6; | 194 | ns.width = 6; |
181 | ns.start = 1; | 195 | ns.start = 1; |
182 | ns.inc = 1; | 196 | ns.inc = 1; |
diff --git a/coreutils/nl.c b/coreutils/nl.c index 5c64923bb..dc468a90b 100644 --- a/coreutils/nl.c +++ b/coreutils/nl.c | |||
@@ -35,26 +35,6 @@ | |||
35 | 35 | ||
36 | #include "libbb.h" | 36 | #include "libbb.h" |
37 | 37 | ||
38 | void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename) | ||
39 | { | ||
40 | FILE *fp = fopen_or_warn_stdin(filename); | ||
41 | unsigned N = ns->start; | ||
42 | char *line; | ||
43 | |||
44 | while ((line = xmalloc_fgetline(fp)) != NULL) { | ||
45 | if (ns->all | ||
46 | || (ns->nonempty && line[0]) | ||
47 | ) { | ||
48 | printf("%*u%s%s\n", ns->width, N, ns->sep, line); | ||
49 | N += ns->inc; | ||
50 | } else if (ns->empty_str) | ||
51 | fputs(ns->empty_str, stdout); | ||
52 | free(line); | ||
53 | } | ||
54 | |||
55 | fclose(fp); | ||
56 | } | ||
57 | |||
58 | int nl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 38 | int nl_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
59 | int nl_main(int argc UNUSED_PARAM, char **argv) | 39 | int nl_main(int argc UNUSED_PARAM, char **argv) |
60 | { | 40 | { |
diff --git a/libbb/print_numbered_lines.c b/libbb/print_numbered_lines.c index 702aed1ea..9a8a51440 100644 --- a/libbb/print_numbered_lines.c +++ b/libbb/print_numbered_lines.c | |||
@@ -24,6 +24,7 @@ void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filenam | |||
24 | fputs(ns->empty_str, stdout); | 24 | fputs(ns->empty_str, stdout); |
25 | free(line); | 25 | free(line); |
26 | } | 26 | } |
27 | ns->start = N; | ||
27 | 28 | ||
28 | fclose(fp); | 29 | fclose(fp); |
29 | } | 30 | } |