use CONFIG_BUFFER macros like cow suggested and shrink code size some more by using less variables and more gotos ;)
This commit is contained in:
parent
98ea849623
commit
db289b258d
@ -81,13 +81,12 @@ static int bsd_sum_file(const char *file, int print_name)
|
|||||||
of file FILE, or of the standard input if FILE is "-".
|
of file FILE, or of the standard input if FILE is "-".
|
||||||
If PRINT_NAME is >0, print FILE next to the checksum and size.
|
If PRINT_NAME is >0, print FILE next to the checksum and size.
|
||||||
Return 1 if successful. */
|
Return 1 if successful. */
|
||||||
|
#define MY_BUF_SIZE 8192
|
||||||
static int sysv_sum_file(const char *file, int print_name)
|
static int sysv_sum_file(const char *file, int print_name)
|
||||||
{
|
{
|
||||||
#define MY_BUF_SIZE 8192
|
RESERVE_CONFIG_UBUFFER(buf, MY_BUF_SIZE);
|
||||||
int fd;
|
int fd;
|
||||||
unsigned char buf[MY_BUF_SIZE];
|
|
||||||
uintmax_t total_bytes = 0;
|
uintmax_t total_bytes = 0;
|
||||||
int checksum;
|
|
||||||
|
|
||||||
/* The sum of all the input bytes, modulo (UINT_MAX + 1). */
|
/* The sum of all the input bytes, modulo (UINT_MAX + 1). */
|
||||||
unsigned int s = 0;
|
unsigned int s = 0;
|
||||||
@ -97,43 +96,42 @@ static int sysv_sum_file(const char *file, int print_name)
|
|||||||
have_read_stdin = 1;
|
have_read_stdin = 1;
|
||||||
} else {
|
} else {
|
||||||
fd = open(file, O_RDONLY);
|
fd = open(file, O_RDONLY);
|
||||||
if (fd == -1) {
|
if (fd == -1)
|
||||||
bb_perror_msg(file);
|
goto release_and_ret;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
size_t i;
|
|
||||||
size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
|
size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
|
||||||
|
|
||||||
if (bytes_read == 0)
|
if (bytes_read == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (bytes_read == -1) {
|
if (bytes_read == -1) {
|
||||||
|
release_and_ret:
|
||||||
bb_perror_msg(file);
|
bb_perror_msg(file);
|
||||||
|
RELEASE_CONFIG_BUFFER(buf);
|
||||||
if (!IS_STDIN(file))
|
if (!IS_STDIN(file))
|
||||||
close(fd);
|
close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < bytes_read; i++)
|
|
||||||
s += buf[i];
|
|
||||||
total_bytes += bytes_read;
|
total_bytes += bytes_read;
|
||||||
|
while (bytes_read--)
|
||||||
|
s += buf[bytes_read];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IS_STDIN(file) && close(fd) == -1) {
|
if (!IS_STDIN(file) && close(fd) == -1)
|
||||||
bb_perror_msg(file);
|
goto release_and_ret;
|
||||||
return 0;
|
else
|
||||||
}
|
RELEASE_CONFIG_BUFFER(buf);
|
||||||
|
|
||||||
{
|
{
|
||||||
int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
|
int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
|
||||||
checksum = (r & 0xffff) + (r >> 16);
|
s = (r & 0xffff) + (r >> 16);
|
||||||
}
|
|
||||||
|
|
||||||
printf("%d %s ", checksum,
|
printf("%d %s ", s,
|
||||||
make_human_readable_str(total_bytes, 1, 512));
|
make_human_readable_str(total_bytes, 1, 512));
|
||||||
|
}
|
||||||
puts(print_name ? file : "");
|
puts(print_name ? file : "");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -143,7 +141,6 @@ int sum_main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int flags;
|
int flags;
|
||||||
int ok;
|
int ok;
|
||||||
int files_given;
|
|
||||||
int (*sum_func)(const char *, int) = bsd_sum_file;
|
int (*sum_func)(const char *, int) = bsd_sum_file;
|
||||||
|
|
||||||
/* give the bsd func priority over sysv func */
|
/* give the bsd func priority over sysv func */
|
||||||
@ -154,12 +151,11 @@ int sum_main(int argc, char **argv)
|
|||||||
sum_func = bsd_sum_file;
|
sum_func = bsd_sum_file;
|
||||||
|
|
||||||
have_read_stdin = 0;
|
have_read_stdin = 0;
|
||||||
files_given = argc - optind;
|
if ((argc - optind) == 0)
|
||||||
if (files_given <= 0)
|
ok = sum_func("-", 0);
|
||||||
ok = sum_func("-", files_given);
|
|
||||||
else
|
else
|
||||||
for (ok = 1; optind < argc; optind++)
|
for (ok = 1; optind < argc; optind++)
|
||||||
ok &= sum_func(argv[optind], files_given);
|
ok &= sum_func(argv[optind], 1);
|
||||||
|
|
||||||
if (have_read_stdin && fclose(stdin) == EOF)
|
if (have_read_stdin && fclose(stdin) == EOF)
|
||||||
bb_perror_msg_and_die("-");
|
bb_perror_msg_and_die("-");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user