aboutsummaryrefslogtreecommitdiff
path: root/coreutils/sum.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:07:36 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:07:36 +0000
commit62a90cdd7435f09f4bb8673e8b7b213067f9d5cc (patch)
treeac7f86c4de0ce0c095b59820c7ba238369c65f71 /coreutils/sum.c
parentd02db892440499a072ea8c83c2370e9d9c103098 (diff)
downloadbusybox-w32-62a90cdd7435f09f4bb8673e8b7b213067f9d5cc.tar.gz
busybox-w32-62a90cdd7435f09f4bb8673e8b7b213067f9d5cc.tar.bz2
busybox-w32-62a90cdd7435f09f4bb8673e8b7b213067f9d5cc.zip
*: shrink by using [f]open_or_warn_stdin where appropriate
function old new delta lsattr_main 62 143 +81 open_or_warn_stdin - 36 +36 fclose_if_not_stdin 20 47 +27 xfopen_stdin - 20 +20 tac_main 336 356 +20 cksum_main 249 259 +10 bb_argv_dash - 8 +8 su_main 448 455 +7 cmp_main 630 633 +3 passwd_main 1072 1074 +2 uudecode_main 317 315 -2 text_yank 110 108 -2 handle_incoming_and_exit 2653 2651 -2 flags 5 1 -4 write_leases 235 230 -5 fopen_or_warn_stdin 48 42 -6 fold_main 648 642 -6 static.argv_dash 8 - -8 sum_main 142 128 -14 tail_main 1237 1221 -16 sed_main 711 695 -16 cmp_xfopen_input 17 - -17 bb_cat 113 96 -17 catv_main 328 306 -22 strings_main 457 434 -23 hash_file 298 274 -24 sum_file 353 325 -28 sort_main 904 859 -45 expand_main 736 686 -50 cut_main 1116 1065 -51 md5_sha1_sum_main 549 493 -56 lsattr_args 90 - -90 read_stduu 408 255 -153 ------------------------------------------------------------------------------ (add/remove: 3/3 grow/shrink: 7/20 up/down: 214/-657) Total: -443 bytes text data bss dec hex filename 797417 658 7428 805503 c4a7f busybox_old 796973 658 7428 805059 c48c3 busybox_unstripped
Diffstat (limited to 'coreutils/sum.c')
-rw-r--r--coreutils/sum.c27
1 files changed, 13 insertions, 14 deletions
diff --git a/coreutils/sum.c b/coreutils/sum.c
index 65478b0a1..e6cfbfd80 100644
--- a/coreutils/sum.c
+++ b/coreutils/sum.c
@@ -21,20 +21,17 @@ enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
21 The checksum varies depending on sizeof (int). */ 21 The checksum varies depending on sizeof (int). */
22/* SYSV: calculate and print the checksum and the size in 512-byte blocks */ 22/* SYSV: calculate and print the checksum and the size in 512-byte blocks */
23/* Return 1 if successful. */ 23/* Return 1 if successful. */
24static unsigned sum_file(const char *file, const unsigned type) 24static unsigned sum_file(const char *file, unsigned type)
25{ 25{
26#define buf bb_common_bufsiz1 26#define buf bb_common_bufsiz1
27 unsigned long long total_bytes = 0; 27 unsigned long long total_bytes = 0;
28 int fd = 0, r; 28 int fd, r;
29
30 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ 29 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
31 unsigned s = 0; 30 unsigned s = 0;
32 31
33 if (NOT_LONE_DASH(file)) { 32 fd = open_or_warn_stdin(file);
34 fd = open(file, O_RDONLY); 33 if (fd == -1)
35 if (fd == -1) 34 return 0;
36 goto ret_bad;
37 }
38 35
39 while (1) { 36 while (1) {
40 size_t bytes_read = safe_read(fd, buf, BUFSIZ); 37 size_t bytes_read = safe_read(fd, buf, BUFSIZ);
@@ -44,7 +41,6 @@ static unsigned sum_file(const char *file, const unsigned type)
44 if (!bytes_read && !r) 41 if (!bytes_read && !r)
45 /* no error */ 42 /* no error */
46 break; 43 break;
47 ret_bad:
48 bb_perror_msg(file); 44 bb_perror_msg(file);
49 return 0; 45 return 0;
50 } 46 }
@@ -75,26 +71,29 @@ static unsigned sum_file(const char *file, const unsigned type)
75} 71}
76 72
77int sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 73int sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78int sum_main(int argc, char **argv) 74int sum_main(int argc ATTRIBUTE_UNUSED, char **argv)
79{ 75{
80 unsigned n; 76 unsigned n;
81 unsigned type = SUM_BSD; 77 unsigned type = SUM_BSD;
82 78
83 n = getopt32(argv, "sr"); 79 n = getopt32(argv, "sr");
80 argv += optind;
84 if (n & 1) type = SUM_SYSV; 81 if (n & 1) type = SUM_SYSV;
85 /* give the bsd priority over sysv func */ 82 /* give the bsd priority over sysv func */
86 if (n & 2) type = SUM_BSD; 83 if (n & 2) type = SUM_BSD;
87 84
88 if (argc == optind) { 85 if (!argv[0]) {
89 /* Do not print the name */ 86 /* Do not print the name */
90 n = sum_file("-", type); 87 n = sum_file("-", type);
91 } else { 88 } else {
92 /* Need to print the name if either 89 /* Need to print the name if either
93 - more than one file given 90 - more than one file given
94 - doing sysv */ 91 - doing sysv */
95 type += argc - 1 > optind || type == SUM_SYSV; 92 type += (argv[1] || type == SUM_SYSV);
96 for (n = 1; optind < argc; optind++) 93 n = 1;
97 n &= sum_file(argv[optind], type); 94 do {
95 n &= sum_file(*argv, type);
96 } while (*++argv);
98 } 97 }
99 return !n; 98 return !n;
100} 99}