diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-02-25 01:26:09 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-02-25 01:26:09 +0100 |
commit | 40c6da433f0900d451e46da86422d89f3878d40c (patch) | |
tree | 7e659456dff909d00371f4393b79f0bcd0b18c69 | |
parent | af0255f49681bcc78830a56af885e891eca210f4 (diff) | |
download | busybox-w32-40c6da433f0900d451e46da86422d89f3878d40c.tar.gz busybox-w32-40c6da433f0900d451e46da86422d89f3878d40c.tar.bz2 busybox-w32-40c6da433f0900d451e46da86422d89f3878d40c.zip |
head: support -n -NUM and -c -NUM
function old new delta
head_main 406 832 +426
packed_usage 29234 29252 +18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 444/0) Total: 444 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/head.c | 150 |
1 files changed, 128 insertions, 22 deletions
diff --git a/coreutils/head.c b/coreutils/head.c index 598fccb64..291e1ce37 100644 --- a/coreutils/head.c +++ b/coreutils/head.c | |||
@@ -21,7 +21,8 @@ | |||
21 | //usage: "With more than one FILE, precede each with a filename header.\n" | 21 | //usage: "With more than one FILE, precede each with a filename header.\n" |
22 | //usage: "\n -n N[kbm] Print first N lines" | 22 | //usage: "\n -n N[kbm] Print first N lines" |
23 | //usage: IF_FEATURE_FANCY_HEAD( | 23 | //usage: IF_FEATURE_FANCY_HEAD( |
24 | //usage: "\n -c N[kbm] Print first N bytes" | 24 | //usage: "\n -n -N[kbm] Print all except N last lines" |
25 | //usage: "\n -c [-]N[kbm] Print first N bytes" | ||
25 | //usage: "\n -q Never print headers" | 26 | //usage: "\n -q Never print headers" |
26 | //usage: "\n -v Always print headers" | 27 | //usage: "\n -v Always print headers" |
27 | //usage: ) | 28 | //usage: ) |
@@ -38,6 +39,110 @@ | |||
38 | 39 | ||
39 | /* This is a NOEXEC applet. Be very careful! */ | 40 | /* This is a NOEXEC applet. Be very careful! */ |
40 | 41 | ||
42 | #if !ENABLE_FEATURE_FANCY_HEAD | ||
43 | # define print_first_N(fp,count,bytes) print_first_N(fp,count) | ||
44 | #endif | ||
45 | static void | ||
46 | print_first_N(FILE *fp, unsigned long count, bool count_bytes) | ||
47 | { | ||
48 | #if !ENABLE_FEATURE_FANCY_HEAD | ||
49 | const int count_bytes = 0; | ||
50 | #endif | ||
51 | while (count) { | ||
52 | int c = getc(fp); | ||
53 | if (c == EOF) | ||
54 | break; | ||
55 | if (count_bytes || (c == '\n')) | ||
56 | --count; | ||
57 | putchar(c); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | #if ENABLE_FEATURE_FANCY_HEAD | ||
62 | static void | ||
63 | print_except_N_last_bytes(FILE *fp, unsigned count) | ||
64 | { | ||
65 | unsigned char *circle = xmalloc(++count); | ||
66 | unsigned head = 0; | ||
67 | for(;;) { | ||
68 | int c; | ||
69 | c = getc(fp); | ||
70 | if (c == EOF) | ||
71 | goto ret; | ||
72 | circle[head++] = c; | ||
73 | if (head == count) | ||
74 | break; | ||
75 | } | ||
76 | for (;;) { | ||
77 | int c; | ||
78 | if (head == count) | ||
79 | head = 0; | ||
80 | putchar(circle[head]); | ||
81 | c = getc(fp); | ||
82 | if (c == EOF) | ||
83 | goto ret; | ||
84 | circle[head] = c; | ||
85 | head++; | ||
86 | } | ||
87 | ret: | ||
88 | free(circle); | ||
89 | } | ||
90 | |||
91 | static void | ||
92 | print_except_N_last_lines(FILE *fp, unsigned count) | ||
93 | { | ||
94 | char **circle = xzalloc((++count) * sizeof(circle[0])); | ||
95 | unsigned head = 0; | ||
96 | for(;;) { | ||
97 | char *c; | ||
98 | c = xmalloc_fgets(fp); | ||
99 | if (!c) | ||
100 | goto ret; | ||
101 | circle[head++] = c; | ||
102 | if (head == count) | ||
103 | break; | ||
104 | } | ||
105 | for (;;) { | ||
106 | char *c; | ||
107 | if (head == count) | ||
108 | head = 0; | ||
109 | fputs(circle[head], stdout); | ||
110 | c = xmalloc_fgets(fp); | ||
111 | if (!c) | ||
112 | goto ret; | ||
113 | free(circle[head]); | ||
114 | circle[head++] = c; | ||
115 | } | ||
116 | ret: | ||
117 | head = 0; | ||
118 | for(;;) { | ||
119 | free(circle[head++]); | ||
120 | if (head == count) | ||
121 | break; | ||
122 | } | ||
123 | free(circle); | ||
124 | } | ||
125 | #else | ||
126 | /* Must never be called */ | ||
127 | void print_except_N_last_bytes(FILE *fp, unsigned count); | ||
128 | void print_except_N_last_lines(FILE *fp, unsigned count); | ||
129 | #endif | ||
130 | |||
131 | #if !ENABLE_FEATURE_FANCY_HEAD | ||
132 | # define eat_num(negative_N,p) eat_num(p) | ||
133 | #endif | ||
134 | static unsigned long | ||
135 | eat_num(bool *negative_N, const char *p) | ||
136 | { | ||
137 | #if ENABLE_FEATURE_FANCY_HEAD | ||
138 | if (*p == '-') { | ||
139 | *negative_N = 1; | ||
140 | p++; | ||
141 | } | ||
142 | #endif | ||
143 | return xatoul_sfx(p, head_tail_suffixes); | ||
144 | } | ||
145 | |||
41 | static const char head_opts[] ALIGN1 = | 146 | static const char head_opts[] ALIGN1 = |
42 | "n:" | 147 | "n:" |
43 | #if ENABLE_FEATURE_FANCY_HEAD | 148 | #if ENABLE_FEATURE_FANCY_HEAD |
@@ -52,8 +157,13 @@ int head_main(int argc, char **argv) | |||
52 | { | 157 | { |
53 | unsigned long count = 10; | 158 | unsigned long count = 10; |
54 | #if ENABLE_FEATURE_FANCY_HEAD | 159 | #if ENABLE_FEATURE_FANCY_HEAD |
55 | int count_bytes = 0; | ||
56 | int header_threshhold = 1; | 160 | int header_threshhold = 1; |
161 | bool count_bytes = 0; | ||
162 | bool negative_N = 0; | ||
163 | #else | ||
164 | # define header_threshhold 1 | ||
165 | # define count_bytes 0 | ||
166 | # define negative_N 0 | ||
57 | #endif | 167 | #endif |
58 | FILE *fp; | 168 | FILE *fp; |
59 | const char *fmt; | 169 | const char *fmt; |
@@ -68,7 +178,7 @@ int head_main(int argc, char **argv) | |||
68 | ) { | 178 | ) { |
69 | --argc; | 179 | --argc; |
70 | ++argv; | 180 | ++argv; |
71 | p = (*argv) + 1; | 181 | p = argv[0] + 1; |
72 | goto GET_COUNT; | 182 | goto GET_COUNT; |
73 | } | 183 | } |
74 | #endif | 184 | #endif |
@@ -92,7 +202,7 @@ int head_main(int argc, char **argv) | |||
92 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD | 202 | #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_HEAD |
93 | GET_COUNT: | 203 | GET_COUNT: |
94 | #endif | 204 | #endif |
95 | count = xatoul_sfx(p, head_tail_suffixes); | 205 | count = eat_num(&negative_N, p); |
96 | break; | 206 | break; |
97 | default: | 207 | default: |
98 | bb_show_usage(); | 208 | bb_show_usage(); |
@@ -105,39 +215,35 @@ int head_main(int argc, char **argv) | |||
105 | *--argv = (char*)"-"; | 215 | *--argv = (char*)"-"; |
106 | 216 | ||
107 | fmt = header_fmt_str + 1; | 217 | fmt = header_fmt_str + 1; |
108 | #if ENABLE_FEATURE_FANCY_HEAD | ||
109 | if (argc <= header_threshhold) { | 218 | if (argc <= header_threshhold) { |
219 | #if ENABLE_FEATURE_FANCY_HEAD | ||
110 | header_threshhold = 0; | 220 | header_threshhold = 0; |
111 | } | ||
112 | #else | 221 | #else |
113 | if (argc <= 1) { | ||
114 | fmt += 11; /* "" */ | 222 | fmt += 11; /* "" */ |
115 | } | ||
116 | /* Now define some things here to avoid #ifdefs in the code below. | ||
117 | * These should optimize out of the if conditions below. */ | ||
118 | #define header_threshhold 1 | ||
119 | #define count_bytes 0 | ||
120 | #endif | 223 | #endif |
224 | } | ||
225 | if (negative_N) { | ||
226 | if (count >= INT_MAX / sizeof(char*)) | ||
227 | bb_error_msg("count is too big: %lu", count); | ||
228 | } | ||
121 | 229 | ||
122 | do { | 230 | do { |
123 | fp = fopen_or_warn_stdin(*argv); | 231 | fp = fopen_or_warn_stdin(*argv); |
124 | if (fp) { | 232 | if (fp) { |
125 | unsigned long i; | ||
126 | |||
127 | if (fp == stdin) { | 233 | if (fp == stdin) { |
128 | *argv = (char *) bb_msg_standard_input; | 234 | *argv = (char *) bb_msg_standard_input; |
129 | } | 235 | } |
130 | if (header_threshhold) { | 236 | if (header_threshhold) { |
131 | printf(fmt, *argv); | 237 | printf(fmt, *argv); |
132 | } | 238 | } |
133 | i = count; | 239 | if (negative_N) { |
134 | while (i) { | 240 | if (count_bytes) { |
135 | int c = getc(fp); | 241 | print_except_N_last_bytes(fp, count); |
136 | if (c == EOF) | 242 | } else { |
137 | break; | 243 | print_except_N_last_lines(fp, count); |
138 | if (count_bytes || (c == '\n')) | 244 | } |
139 | --i; | 245 | } else { |
140 | putchar(c); | 246 | print_first_N(fp, count, count_bytes); |
141 | } | 247 | } |
142 | die_if_ferror_stdout(); | 248 | die_if_ferror_stdout(); |
143 | if (fclose_if_not_stdin(fp)) { | 249 | if (fclose_if_not_stdin(fp)) { |