aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-04-04 20:29:15 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-04-04 20:29:15 +0000
commitd9c2d5fe4ffeedeadc26c1ee64247c72cdbd064a (patch)
treef259ff6ef1c22e2c2348b5bc87df8184b3d27a12
parent10aed96f14c3323106f98a23c654d96ba0cdbcb6 (diff)
downloadbusybox-w32-d9c2d5fe4ffeedeadc26c1ee64247c72cdbd064a.tar.gz
busybox-w32-d9c2d5fe4ffeedeadc26c1ee64247c72cdbd064a.tar.bz2
busybox-w32-d9c2d5fe4ffeedeadc26c1ee64247c72cdbd064a.zip
- minor shrinkage
text data bss dec hex filename 1431 0 4 1435 59b tail.o.orig 1396 0 0 1396 574 tail.o
-rw-r--r--coreutils/tail.c47
1 files changed, 21 insertions, 26 deletions
diff --git a/coreutils/tail.c b/coreutils/tail.c
index 52bacb7ed..67396ab1c 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -33,7 +33,10 @@ static const struct suffix_mult tail_suffixes[] = {
33 { NULL, 0 } 33 { NULL, 0 }
34}; 34};
35 35
36static int status; 36struct globals {
37 bool status;
38};
39#define G (*(struct globals*)&bb_common_bufsiz1)
37 40
38static void tail_xprint_header(const char *fmt, const char *filename) 41static void tail_xprint_header(const char *fmt, const char *filename)
39{ 42{
@@ -54,7 +57,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
54 r = safe_read(fd, buf, count); 57 r = safe_read(fd, buf, count);
55 if (r < 0) { 58 if (r < 0) {
56 bb_perror_msg(bb_msg_read_error); 59 bb_perror_msg(bb_msg_read_error);
57 status = EXIT_FAILURE; 60 G.status = EXIT_FAILURE;
58 } 61 }
59 62
60 return r; 63 return r;
@@ -64,7 +67,7 @@ static const char header_fmt[] = "\n==> %s <==\n";
64 67
65static unsigned eat_num(const char *p) { 68static unsigned eat_num(const char *p) {
66 if (*p == '-') p++; 69 if (*p == '-') p++;
67 else if (*p == '+') { p++; status = 1; } 70 else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
68 return xatou_sfx(p, tail_suffixes); 71 return xatou_sfx(p, tail_suffixes);
69} 72}
70 73
@@ -111,11 +114,12 @@ int tail_main(int argc, char **argv)
111#endif 114#endif
112 argc -= optind; 115 argc -= optind;
113 argv += optind; 116 argv += optind;
114 from_top = status; 117 from_top = G.status;
115 118
116 /* open all the files */ 119 /* open all the files */
117 fds = xmalloc(sizeof(int) * (argc + 1)); 120 fds = xmalloc(sizeof(int) * (argc + 1));
118 status = nfiles = i = 0; 121 nfiles = i = 0;
122 G.status = EXIT_SUCCESS;
119 if (argc == 0) { 123 if (argc == 0) {
120 struct stat statbuf; 124 struct stat statbuf;
121 125
@@ -123,23 +127,15 @@ int tail_main(int argc, char **argv)
123 opt &= ~1; /* clear FOLLOW */ 127 opt &= ~1; /* clear FOLLOW */
124 } 128 }
125 *argv = (char *) bb_msg_standard_input; 129 *argv = (char *) bb_msg_standard_input;
126 goto DO_STDIN;
127 } 130 }
128
129 do { 131 do {
130 if (NOT_LONE_DASH(argv[i])) { 132 FILE* fil = fopen_or_warn_stdin(argv[i]);
131 fds[nfiles] = open(argv[i], O_RDONLY); 133 if (!fil) {
132 if (fds[nfiles] < 0) { 134 G.status = EXIT_FAILURE;
133 bb_perror_msg("%s", argv[i]); 135 continue;
134 status = EXIT_FAILURE;
135 continue;
136 }
137 } else {
138 DO_STDIN: /* "-" */
139 fds[nfiles] = STDIN_FILENO;
140 } 136 }
141 argv[nfiles] = argv[i]; 137 fds[nfiles] = fileno(fil);
142 ++nfiles; 138 argv[nfiles++] = argv[i];
143 } while (++i < argc); 139 } while (++i < argc);
144 140
145 if (!nfiles) 141 if (!nfiles)
@@ -217,13 +213,11 @@ int tail_main(int argc, char **argv)
217 if (newline + nbuf < count) { 213 if (newline + nbuf < count) {
218 newline += nbuf; 214 newline += nbuf;
219 taillen += nread; 215 taillen += nread;
220
221 } else { 216 } else {
222 int extra = 0; 217 int extra = 0;
223 if (buf[nread-1] != '\n') {
224 extra = 1;
225 }
226 218
219 if (buf[nread-1] != '\n')
220 extra = 1;
227 k = newline + nbuf + extra - count; 221 k = newline + nbuf + extra - count;
228 s = tailbuf; 222 s = tailbuf;
229 while (k) { 223 while (k) {
@@ -232,7 +226,6 @@ int tail_main(int argc, char **argv)
232 } 226 }
233 ++s; 227 ++s;
234 } 228 }
235
236 taillen += nread - (s - tailbuf); 229 taillen += nread - (s - tailbuf);
237 memmove(tailbuf, s, taillen); 230 memmove(tailbuf, s, taillen);
238 newline = count - extra; 231 newline = count - extra;
@@ -273,6 +266,8 @@ int tail_main(int argc, char **argv)
273 } 266 }
274 } while (++i < nfiles); 267 } while (++i < nfiles);
275 } 268 }
276 269 if (ENABLE_FEATURE_CLEAN_UP) {
277 return status; 270 free(fds);
271 }
272 return G.status;
278} 273}