aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-01-27 22:11:28 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2007-01-27 22:11:28 +0000
commitcd75a96f0f9d446028cad7e4b9b9224e009752e1 (patch)
tree3d85b95f511d68e6e465a84f4465ecccb2226b7e
parent14813c5943ef6d3e07a8ccab1d29d9cb704cebd7 (diff)
downloadbusybox-w32-cd75a96f0f9d446028cad7e4b9b9224e009752e1.tar.gz
busybox-w32-cd75a96f0f9d446028cad7e4b9b9224e009752e1.tar.bz2
busybox-w32-cd75a96f0f9d446028cad7e4b9b9224e009752e1.zip
- sum -r TODO should not print the filename as oposed to -s
Unfortunately, without rewriting sum, this bugfix adds 19 (!) bytes.
-rw-r--r--coreutils/sum.c37
1 files changed, 21 insertions, 16 deletions
diff --git a/coreutils/sum.c b/coreutils/sum.c
index df5804899..d62f2cfde 100644
--- a/coreutils/sum.c
+++ b/coreutils/sum.c
@@ -15,22 +15,21 @@
15 15
16#include "busybox.h" 16#include "busybox.h"
17 17
18enum { sysv_sum, bsd_sum }; 18enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
19 19
20/* BSD: 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
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 int sum_file(const char *file, int type, int print_name) 24static unsigned sum_file(const char *file, const unsigned type)
25{ 25{
26#define buf bb_common_bufsiz1 26#define buf bb_common_bufsiz1
27 int r, fd;
28 uintmax_t total_bytes = 0; 27 uintmax_t total_bytes = 0;
28 int fd = 0, r;
29 29
30 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ 30 /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
31 unsigned s = 0; 31 unsigned s = 0;
32 32
33 fd = 0;
34 if (NOT_LONE_DASH(file)) { 33 if (NOT_LONE_DASH(file)) {
35 fd = open(file, O_RDONLY); 34 fd = open(file, O_RDONLY);
36 if (fd == -1) 35 if (fd == -1)
@@ -51,7 +50,7 @@ static int sum_file(const char *file, int type, int print_name)
51 } 50 }
52 51
53 total_bytes += bytes_read; 52 total_bytes += bytes_read;
54 if (type == sysv_sum) { 53 if (type >= SUM_SYSV) {
55 do s += buf[--bytes_read]; while (bytes_read); 54 do s += buf[--bytes_read]; while (bytes_read);
56 } else { 55 } else {
57 r = 0; 56 r = 0;
@@ -63,8 +62,9 @@ static int sum_file(const char *file, int type, int print_name)
63 } 62 }
64 } 63 }
65 64
66 if (!print_name) file = ""; 65 if (type < PRINT_NAME)
67 if (type == sysv_sum) { 66 file = "";
67 if (type >= SUM_SYSV) {
68 r = (s & 0xffff) + ((s & 0xffffffff) >> 16); 68 r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
69 s = (r & 0xffff) + (r >> 16); 69 s = (r & 0xffff) + (r >> 16);
70 printf("%d %ju %s\n", s, (total_bytes+511)/512, file); 70 printf("%d %ju %s\n", s, (total_bytes+511)/512, file);
@@ -76,19 +76,24 @@ static int sum_file(const char *file, int type, int print_name)
76 76
77int sum_main(int argc, char **argv) 77int sum_main(int argc, char **argv)
78{ 78{
79 int n; 79 unsigned n;
80 int type = bsd_sum; 80 unsigned type = SUM_BSD;
81 81
82 n = getopt32(argc, argv, "sr"); 82 n = getopt32(argc, argv, "sr");
83 if (n & 1) type = sysv_sum; 83 if (n & 1) type = SUM_SYSV;
84 /* give the bsd priority over sysv func */ 84 /* give the bsd priority over sysv func */
85 if (n & 2) type = bsd_sum; 85 if (n & 2) type = SUM_BSD;
86 86
87 if (argc == optind) 87 if (argc == optind) {
88 n = sum_file("-", type, 0); 88 /* Do not print the name */
89 else 89 n = sum_file("-", type);
90 } else {
91 /* Need to print the name if either
92 - more than one file given
93 - doing sysv */
94 type += argc - 1 > optind || type == SUM_SYSV;
90 for (n = 1; optind < argc; optind++) 95 for (n = 1; optind < argc; optind++)
91 n &= sum_file(argv[optind], type, 1); 96 n &= sum_file(argv[optind], type);
92 97 }
93 return !n; 98 return !n;
94} 99}