diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-23 15:58:11 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-23 15:58:11 +0000 |
commit | 9ac9e55e3c1dd11bf007623ea672f7569366c186 (patch) | |
tree | 7f3b46d7f2f01fed698b569b3217ab5b52413aac | |
parent | e13a537795751bee8e2b7fe2b88c75436acd9da0 (diff) | |
download | busybox-w32-9ac9e55e3c1dd11bf007623ea672f7569366c186.tar.gz busybox-w32-9ac9e55e3c1dd11bf007623ea672f7569366c186.tar.bz2 busybox-w32-9ac9e55e3c1dd11bf007623ea672f7569366c186.zip |
sum: 40% size reduction (-300 bytes)
find: one_char -> LONE_CHAR
-rw-r--r-- | coreutils/sum.c | 155 | ||||
-rw-r--r-- | findutils/find.c | 15 |
2 files changed, 53 insertions, 117 deletions
diff --git a/coreutils/sum.c b/coreutils/sum.c index 68a857816..df5804899 100644 --- a/coreutils/sum.c +++ b/coreutils/sum.c | |||
@@ -15,139 +15,80 @@ | |||
15 | 15 | ||
16 | #include "busybox.h" | 16 | #include "busybox.h" |
17 | 17 | ||
18 | /* 1 if any of the files read were the standard input */ | 18 | enum { sysv_sum, bsd_sum }; |
19 | static int have_read_stdin; | ||
20 | 19 | ||
21 | /* Calculate and print the rotated checksum and the size in 1K blocks | 20 | /* BSD: calculate and print the rotated checksum and the size in 1K blocks |
22 | of file FILE, or of the standard input if FILE is "-". | 21 | The checksum varies depending on sizeof (int). */ |
23 | If PRINT_NAME is >1, print FILE next to the checksum and size. | 22 | /* SYSV: calculate and print the checksum and the size in 512-byte blocks */ |
24 | The checksum varies depending on sizeof (int). | 23 | /* Return 1 if successful. */ |
25 | Return 1 if successful. */ | 24 | static int sum_file(const char *file, int type, int print_name) |
26 | static int bsd_sum_file(const char *file, int print_name) | ||
27 | { | 25 | { |
28 | FILE *fp; | 26 | #define buf bb_common_bufsiz1 |
29 | int checksum = 0; /* The checksum mod 2^16. */ | 27 | int r, fd; |
30 | uintmax_t total_bytes = 0; /* The number of bytes. */ | ||
31 | int ch; /* Each character read. */ | ||
32 | int ret = 0; | ||
33 | |||
34 | if (LONE_DASH(file)) { | ||
35 | fp = stdin; | ||
36 | have_read_stdin++; | ||
37 | } else { | ||
38 | fp = fopen_or_warn(file, "r"); | ||
39 | if (fp == NULL) | ||
40 | goto out; | ||
41 | } | ||
42 | |||
43 | while ((ch = getc(fp)) != EOF) { | ||
44 | ++total_bytes; | ||
45 | checksum = (checksum >> 1) + ((checksum & 1) << 15); | ||
46 | checksum += ch; | ||
47 | checksum &= 0xffff; /* Keep it within bounds. */ | ||
48 | } | ||
49 | |||
50 | if (ferror(fp)) { | ||
51 | bb_perror_msg(file); | ||
52 | fclose_if_not_stdin(fp); | ||
53 | goto out; | ||
54 | } | ||
55 | |||
56 | if (fclose_if_not_stdin(fp) == EOF) { | ||
57 | bb_perror_msg(file); | ||
58 | goto out; | ||
59 | } | ||
60 | ret++; | ||
61 | printf("%05d %5ju ", checksum, (total_bytes+1023)/1024); | ||
62 | if (print_name > 1) | ||
63 | puts(file); | ||
64 | else | ||
65 | puts(""); | ||
66 | out: | ||
67 | return ret; | ||
68 | } | ||
69 | |||
70 | /* Calculate and print the checksum and the size in 512-byte blocks | ||
71 | of file FILE, or of the standard input if FILE is "-". | ||
72 | If PRINT_NAME is >0, print FILE next to the checksum and size. | ||
73 | Return 1 if successful. */ | ||
74 | #define MY_BUF_SIZE 8192 | ||
75 | static int sysv_sum_file(const char *file, int print_name) | ||
76 | { | ||
77 | RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE); | ||
78 | int fd; | ||
79 | uintmax_t total_bytes = 0; | 28 | uintmax_t total_bytes = 0; |
80 | 29 | ||
81 | /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ | 30 | /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ |
82 | unsigned int s = 0; | 31 | unsigned s = 0; |
83 | 32 | ||
84 | if (LONE_DASH(file)) { | 33 | fd = 0; |
85 | fd = 0; | 34 | if (NOT_LONE_DASH(file)) { |
86 | have_read_stdin = 1; | ||
87 | } else { | ||
88 | fd = open(file, O_RDONLY); | 35 | fd = open(file, O_RDONLY); |
89 | if (fd == -1) | 36 | if (fd == -1) |
90 | goto release_and_ret; | 37 | goto ret_bad; |
91 | } | 38 | } |
92 | 39 | ||
93 | while (1) { | 40 | while (1) { |
94 | size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE); | 41 | size_t bytes_read = safe_read(fd, buf, BUFSIZ); |
95 | 42 | ||
96 | if (bytes_read == 0) | 43 | if ((ssize_t)bytes_read <= 0) { |
97 | break; | 44 | r = (fd && close(fd) != 0); |
98 | 45 | if (!bytes_read && !r) | |
99 | if (bytes_read == -1) { | 46 | /* no error */ |
100 | release_and_ret: | 47 | break; |
48 | ret_bad: | ||
101 | bb_perror_msg(file); | 49 | bb_perror_msg(file); |
102 | RELEASE_CONFIG_BUFFER(buf); | ||
103 | if (NOT_LONE_DASH(file)) | ||
104 | close(fd); | ||
105 | return 0; | 50 | return 0; |
106 | } | 51 | } |
107 | 52 | ||
108 | total_bytes += bytes_read; | 53 | total_bytes += bytes_read; |
109 | while (bytes_read--) | 54 | if (type == sysv_sum) { |
110 | s += buf[bytes_read]; | 55 | do s += buf[--bytes_read]; while (bytes_read); |
56 | } else { | ||
57 | r = 0; | ||
58 | do { | ||
59 | s = (s >> 1) + ((s & 1) << 15); | ||
60 | s += buf[r++]; | ||
61 | s &= 0xffff; /* Keep it within bounds. */ | ||
62 | } while (--bytes_read); | ||
63 | } | ||
111 | } | 64 | } |
112 | 65 | ||
113 | if (NOT_LONE_DASH(file) && close(fd) == -1) | 66 | if (!print_name) file = ""; |
114 | goto release_and_ret; | 67 | if (type == sysv_sum) { |
115 | else | 68 | r = (s & 0xffff) + ((s & 0xffffffff) >> 16); |
116 | RELEASE_CONFIG_BUFFER(buf); | ||
117 | |||
118 | { | ||
119 | int r = (s & 0xffff) + ((s & 0xffffffff) >> 16); | ||
120 | s = (r & 0xffff) + (r >> 16); | 69 | s = (r & 0xffff) + (r >> 16); |
121 | 70 | printf("%d %ju %s\n", s, (total_bytes+511)/512, file); | |
122 | printf("%d %ju ", s, (total_bytes+511)/512); | 71 | } else |
123 | } | 72 | printf("%05d %5ju %s\n", s, (total_bytes+1023)/1024, file); |
124 | puts(print_name ? file : ""); | ||
125 | |||
126 | return 1; | 73 | return 1; |
74 | #undef buf | ||
127 | } | 75 | } |
128 | 76 | ||
129 | int sum_main(int argc, char **argv) | 77 | int sum_main(int argc, char **argv) |
130 | { | 78 | { |
131 | int flags; | 79 | int n; |
132 | int ok; | 80 | int type = bsd_sum; |
133 | int (*sum_func)(const char *, int) = bsd_sum_file; | ||
134 | 81 | ||
135 | /* give the bsd func priority over sysv func */ | 82 | n = getopt32(argc, argv, "sr"); |
136 | flags = getopt32(argc, argv, "sr"); | 83 | if (n & 1) type = sysv_sum; |
137 | if (flags & 1) | 84 | /* give the bsd priority over sysv func */ |
138 | sum_func = sysv_sum_file; | 85 | if (n & 2) type = bsd_sum; |
139 | if (flags & 2) | ||
140 | sum_func = bsd_sum_file; | ||
141 | 86 | ||
142 | have_read_stdin = 0; | 87 | if (argc == optind) |
143 | if ((argc - optind) == 0) | 88 | n = sum_file("-", type, 0); |
144 | ok = sum_func("-", 0); | ||
145 | else | 89 | else |
146 | for (ok = 1; optind < argc; optind++) | 90 | for (n = 1; optind < argc; optind++) |
147 | ok &= sum_func(argv[optind], 1); | 91 | n &= sum_file(argv[optind], type, 1); |
148 | |||
149 | if (have_read_stdin && fclose(stdin) == EOF) | ||
150 | bb_perror_msg_and_die("-"); | ||
151 | 92 | ||
152 | exit(ok ? EXIT_SUCCESS : EXIT_FAILURE); | 93 | return !n; |
153 | } | 94 | } |
diff --git a/findutils/find.c b/findutils/find.c index 38bbfbec9..fffa3cd5c 100644 --- a/findutils/find.c +++ b/findutils/find.c | |||
@@ -75,11 +75,6 @@ USE_DESKTOP( ACTS(prune)) | |||
75 | static action ***actions; | 75 | static action ***actions; |
76 | static int need_print = 1; | 76 | static int need_print = 1; |
77 | 77 | ||
78 | static inline int one_char(const char* str, char c) | ||
79 | { | ||
80 | return (str[0] == c && str[1] == '\0'); | ||
81 | } | ||
82 | |||
83 | 78 | ||
84 | static int count_subst(const char *str) | 79 | static int count_subst(const char *str) |
85 | { | 80 | { |
@@ -455,7 +450,7 @@ action*** parse_params(char **argv) | |||
455 | while (1) { | 450 | while (1) { |
456 | if (!*argv) /* did not see ';' till end */ | 451 | if (!*argv) /* did not see ';' till end */ |
457 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | 452 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
458 | if (one_char(argv[0], ';')) | 453 | if (LONE_CHAR(argv[0], ';')) |
459 | break; | 454 | break; |
460 | argv++; | 455 | argv++; |
461 | ap->exec_argc++; | 456 | ap->exec_argc++; |
@@ -469,7 +464,7 @@ action*** parse_params(char **argv) | |||
469 | } | 464 | } |
470 | #endif | 465 | #endif |
471 | #if ENABLE_DESKTOP | 466 | #if ENABLE_DESKTOP |
472 | else if (one_char(arg, '(')) { | 467 | else if (LONE_CHAR(arg, '(')) { |
473 | action_paren *ap; | 468 | action_paren *ap; |
474 | char **endarg; | 469 | char **endarg; |
475 | int nested = 1; | 470 | int nested = 1; |
@@ -478,9 +473,9 @@ action*** parse_params(char **argv) | |||
478 | while (1) { | 473 | while (1) { |
479 | if (!*++endarg) | 474 | if (!*++endarg) |
480 | bb_error_msg_and_die("unpaired '('"); | 475 | bb_error_msg_and_die("unpaired '('"); |
481 | if (one_char(*endarg, '(')) | 476 | if (LONE_CHAR(*endarg, '(')) |
482 | nested++; | 477 | nested++; |
483 | else if (one_char(*endarg, ')') && !--nested) { | 478 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
484 | *endarg = NULL; | 479 | *endarg = NULL; |
485 | break; | 480 | break; |
486 | } | 481 | } |
@@ -522,7 +517,7 @@ int find_main(int argc, char **argv) | |||
522 | if (argv[firstopt][0] == '-') | 517 | if (argv[firstopt][0] == '-') |
523 | break; | 518 | break; |
524 | #if ENABLE_DESKTOP | 519 | #if ENABLE_DESKTOP |
525 | if (one_char(argv[firstopt], '(')) | 520 | if (LONE_CHAR(argv[firstopt], '(')) |
526 | break; | 521 | break; |
527 | #endif | 522 | #endif |
528 | } | 523 | } |