aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-08-30 19:35:06 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-08-30 19:35:06 +0200
commit1249dbb2c77af8543e58a4bb217196e1b97aff9b (patch)
treeff931d97afb9397458fd0b172b8c7753c9f0cf9e
parenta9c9bf5055956b9578911c1d773acf6a16397b84 (diff)
downloadbusybox-w32-1249dbb2c77af8543e58a4bb217196e1b97aff9b.tar.gz
busybox-w32-1249dbb2c77af8543e58a4bb217196e1b97aff9b.tar.bz2
busybox-w32-1249dbb2c77af8543e58a4bb217196e1b97aff9b.zip
uniq: code shrink
function old new delta xgetoptfile_uniq_s 44 51 +7 uniq_main 389 368 -21 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 7/-21) Total: -14 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/uniq.c34
1 files changed, 17 insertions, 17 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index be53b312e..910f46e15 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -12,23 +12,23 @@
12 12
13#include "libbb.h" 13#include "libbb.h"
14 14
15static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) 15static void xgetoptfile_uniq_s(const char *n, int fd)
16{ 16{
17 const char *n; 17 if (n == NULL)
18 18 return;
19 n = *argv; 19 if ((n[0] == '-') && !n[1])
20 if (n != NULL) { 20 return;
21 if ((n[0] != '-') || n[1]) { 21 /* close(fd); - optimization */
22 return xfopen(n, "r\0w" + read0write2); 22 xmove_fd(
23 } 23 xopen3(n,
24 } 24 (fd == STDIN_FILENO) ? O_RDONLY : (O_WRONLY | O_CREAT | O_TRUNC),
25 return (read0write2) ? stdout : stdin; 25 0666),
26 fd);
26} 27}
27 28
28int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 29int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
29int uniq_main(int argc UNUSED_PARAM, char **argv) 30int uniq_main(int argc UNUSED_PARAM, char **argv)
30{ 31{
31 FILE *in, *out;
32 const char *input_filename; 32 const char *input_filename;
33 unsigned skip_fields, skip_chars, max_chars; 33 unsigned skip_fields, skip_chars, max_chars;
34 unsigned opt; 34 unsigned opt;
@@ -53,11 +53,11 @@ int uniq_main(int argc UNUSED_PARAM, char **argv)
53 53
54 input_filename = *argv; 54 input_filename = *argv;
55 55
56 in = xgetoptfile_uniq_s(argv, 0); 56 xgetoptfile_uniq_s(*argv, STDIN_FILENO);
57 if (*argv) { 57 if (*argv) {
58 ++argv; 58 ++argv;
59 } 59 }
60 out = xgetoptfile_uniq_s(argv, 2); 60 xgetoptfile_uniq_s(*argv, STDOUT_FILENO);
61 if (*argv && argv[1]) { 61 if (*argv && argv[1]) {
62 bb_show_usage(); 62 bb_show_usage();
63 } 63 }
@@ -75,7 +75,7 @@ int uniq_main(int argc UNUSED_PARAM, char **argv)
75 dups = 0; 75 dups = 0;
76 76
77 /* gnu uniq ignores newlines */ 77 /* gnu uniq ignores newlines */
78 while ((cur_line = xmalloc_fgetline(in)) != NULL) { 78 while ((cur_line = xmalloc_fgetline(stdin)) != NULL) {
79 cur_compare = cur_line; 79 cur_compare = cur_line;
80 for (i = skip_fields; i; i--) { 80 for (i = skip_fields; i; i--) {
81 cur_compare = skip_whitespace(cur_compare); 81 cur_compare = skip_whitespace(cur_compare);
@@ -95,14 +95,14 @@ int uniq_main(int argc UNUSED_PARAM, char **argv)
95 95
96 if (old_line) { 96 if (old_line) {
97 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */ 97 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_u) */
98 fprintf(out, "\0%lu " + (opt & 1), dups + 1); /* 1 == OPT_c */ 98 printf("\0%lu " + (opt & 1), dups + 1); /* 1 == OPT_c */
99 fprintf(out, "%s\n", old_line); 99 printf("%s\n", old_line);
100 } 100 }
101 free(old_line); 101 free(old_line);
102 } 102 }
103 } while (cur_line); 103 } while (cur_line);
104 104
105 die_if_ferror(in, input_filename); 105 die_if_ferror(stdin, input_filename);
106 106
107 fflush_stdout_and_exit(EXIT_SUCCESS); 107 fflush_stdout_and_exit(EXIT_SUCCESS);
108} 108}