aboutsummaryrefslogtreecommitdiff
path: root/coreutils/sum.c
diff options
context:
space:
mode:
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}