aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvapier <vapier@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-09-10 04:10:18 +0000
committervapier <vapier@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-09-10 04:10:18 +0000
commit72f79f2e43279d1d731ec47a18187452420d66a5 (patch)
treefaf9cf49cd4f2cbb23634345ec9d2ec257915c9e
parentb21bd89772fef40cd9fc311e5a7a60bbcc73fca4 (diff)
downloadbusybox-w32-72f79f2e43279d1d731ec47a18187452420d66a5.tar.gz
busybox-w32-72f79f2e43279d1d731ec47a18187452420d66a5.tar.bz2
busybox-w32-72f79f2e43279d1d731ec47a18187452420d66a5.zip
use CONFIG_BUFFER macros like cow suggested and shrink code size some more by using less variables and more gotos ;)
git-svn-id: svn://busybox.net/trunk/busybox@11421 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/sum.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/coreutils/sum.c b/coreutils/sum.c
index 70b9b3a0a..3b4941d28 100644
--- a/coreutils/sum.c
+++ b/coreutils/sum.c
@@ -81,13 +81,12 @@ static int bsd_sum_file(const char *file, int print_name)
81 of file FILE, or of the standard input if FILE is "-". 81 of file FILE, or of the standard input if FILE is "-".
82 If PRINT_NAME is >0, print FILE next to the checksum and size. 82 If PRINT_NAME is >0, print FILE next to the checksum and size.
83 Return 1 if successful. */ 83 Return 1 if successful. */
84#define MY_BUF_SIZE 8192
84static int sysv_sum_file(const char *file, int print_name) 85static int sysv_sum_file(const char *file, int print_name)
85{ 86{
86#define MY_BUF_SIZE 8192 87 RESERVE_CONFIG_UBUFFER(buf, MY_BUF_SIZE);
87 int fd; 88 int fd;
88 unsigned char buf[MY_BUF_SIZE];
89 uintmax_t total_bytes = 0; 89 uintmax_t total_bytes = 0;
90 int checksum;
91 90
92 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ 91 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
93 unsigned int s = 0; 92 unsigned int s = 0;
@@ -97,43 +96,42 @@ static int sysv_sum_file(const char *file, int print_name)
97 have_read_stdin = 1; 96 have_read_stdin = 1;
98 } else { 97 } else {
99 fd = open(file, O_RDONLY); 98 fd = open(file, O_RDONLY);
100 if (fd == -1) { 99 if (fd == -1)
101 bb_perror_msg(file); 100 goto release_and_ret;
102 return 0;
103 }
104 } 101 }
105 102
106 while (1) { 103 while (1) {
107 size_t i;
108 size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE); 104 size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
109 105
110 if (bytes_read == 0) 106 if (bytes_read == 0)
111 break; 107 break;
112 108
113 if (bytes_read == -1) { 109 if (bytes_read == -1) {
110release_and_ret:
114 bb_perror_msg(file); 111 bb_perror_msg(file);
112 RELEASE_CONFIG_BUFFER(buf);
115 if (!IS_STDIN(file)) 113 if (!IS_STDIN(file))
116 close(fd); 114 close(fd);
117 return 0; 115 return 0;
118 } 116 }
119 117
120 for (i = 0; i < bytes_read; i++)
121 s += buf[i];
122 total_bytes += bytes_read; 118 total_bytes += bytes_read;
119 while (bytes_read--)
120 s += buf[bytes_read];
123 } 121 }
124 122
125 if (!IS_STDIN(file) && close(fd) == -1) { 123 if (!IS_STDIN(file) && close(fd) == -1)
126 bb_perror_msg(file); 124 goto release_and_ret;
127 return 0; 125 else
128 } 126 RELEASE_CONFIG_BUFFER(buf);
129 127
130 { 128 {
131 int r = (s & 0xffff) + ((s & 0xffffffff) >> 16); 129 int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
132 checksum = (r & 0xffff) + (r >> 16); 130 s = (r & 0xffff) + (r >> 16);
133 }
134 131
135 printf("%d %s ", checksum, 132 printf("%d %s ", s,
136 make_human_readable_str(total_bytes, 1, 512)); 133 make_human_readable_str(total_bytes, 1, 512));
134 }
137 puts(print_name ? file : ""); 135 puts(print_name ? file : "");
138 136
139 return 1; 137 return 1;
@@ -143,7 +141,6 @@ int sum_main(int argc, char **argv)
143{ 141{
144 int flags; 142 int flags;
145 int ok; 143 int ok;
146 int files_given;
147 int (*sum_func)(const char *, int) = bsd_sum_file; 144 int (*sum_func)(const char *, int) = bsd_sum_file;
148 145
149 /* give the bsd func priority over sysv func */ 146 /* give the bsd func priority over sysv func */
@@ -154,12 +151,11 @@ int sum_main(int argc, char **argv)
154 sum_func = bsd_sum_file; 151 sum_func = bsd_sum_file;
155 152
156 have_read_stdin = 0; 153 have_read_stdin = 0;
157 files_given = argc - optind; 154 if ((argc - optind) == 0)
158 if (files_given <= 0) 155 ok = sum_func("-", 0);
159 ok = sum_func("-", files_given);
160 else 156 else
161 for (ok = 1; optind < argc; optind++) 157 for (ok = 1; optind < argc; optind++)
162 ok &= sum_func(argv[optind], files_given); 158 ok &= sum_func(argv[optind], 1);
163 159
164 if (have_read_stdin && fclose(stdin) == EOF) 160 if (have_read_stdin && fclose(stdin) == EOF)
165 bb_perror_msg_and_die("-"); 161 bb_perror_msg_and_die("-");