use bb_xbind/bb_xlisten
[oweals/busybox.git] / coreutils / sum.c
index d68043cf3322988c8ca1294b14b4e17e8b6a3ecf..2edd92036d48a1f8284bceec95b81951d5b5ecd7 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /*
  * sum -- checksum and count the blocks in a file
  *     Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
@@ -9,20 +10,7 @@
  * Written by Kayvan Aghaiepour and David MacKenzie
  * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under the GPL v2, see the file LICENSE in this tarball.
  */
 
 #include <stdio.h>
@@ -30,9 +18,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <getopt.h>
 
-#include "libbb.h"
+#include "busybox.h"
 
 /* 1 if any of the files read were the standard input */
 static int have_read_stdin;
@@ -47,10 +34,10 @@ static int have_read_stdin;
    Return 1 if successful.  */
 static int bsd_sum_file(const char *file, int print_name)
 {
-       register FILE *fp;
-       register int checksum = 0;          /* The checksum mod 2^16. */
-       register uintmax_t total_bytes = 0; /* The number of bytes. */
-       register int ch;                    /* Each character read. */
+       FILE *fp;
+       int checksum = 0;          /* The checksum mod 2^16. */
+       uintmax_t total_bytes = 0; /* The number of bytes. */
+       int ch;                    /* Each character read. */
 
        if (IS_STDIN(file)) {
                fp = stdin;
@@ -79,8 +66,7 @@ static int bsd_sum_file(const char *file, int print_name)
                return 0;
        }
 
-       printf("%05d %5s ", checksum,
-              make_human_readable_str(total_bytes, 1, 1024));
+       printf("%05d %5ju ", checksum, (total_bytes+1023)/1024);
        if (print_name > 1)
                puts(file);
        else
@@ -93,13 +79,12 @@ static int bsd_sum_file(const char *file, int print_name)
    of file FILE, or of the standard input if FILE is "-".
    If PRINT_NAME is >0, print FILE next to the checksum and size.
    Return 1 if successful.  */
+#define MY_BUF_SIZE 8192
 static int sysv_sum_file(const char *file, int print_name)
 {
+       RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE);
        int fd;
-       unsigned char buf[8192];
        uintmax_t total_bytes = 0;
-       int r;
-       int checksum;
 
        /* The sum of all the input bytes, modulo (UINT_MAX + 1).  */
        unsigned int s = 0;
@@ -109,45 +94,42 @@ static int sysv_sum_file(const char *file, int print_name)
                have_read_stdin = 1;
        } else {
                fd = open(file, O_RDONLY);
-               if (fd == -1) {
-                       bb_perror_msg(file);
-                       return 0;
-               }
+               if (fd == -1)
+                       goto release_and_ret;
        }
 
        while (1) {
-               size_t i;
-               size_t bytes_read = safe_read(fd, buf, sizeof(buf));
+               size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
 
                if (bytes_read == 0)
                        break;
 
                if (bytes_read == -1) {
+release_and_ret:
                        bb_perror_msg(file);
+                       RELEASE_CONFIG_BUFFER(buf);
                        if (!IS_STDIN(file))
                                close(fd);
                        return 0;
                }
 
-               for (i = 0; i < bytes_read; i++)
-                       s += buf[i];
                total_bytes += bytes_read;
+               while (bytes_read--)
+                       s += buf[bytes_read];
        }
 
-       if (!IS_STDIN(file) && close(fd) == -1) {
-               bb_perror_msg(file);
-               return 0;
-       }
+       if (!IS_STDIN(file) && close(fd) == -1)
+               goto release_and_ret;
+       else
+               RELEASE_CONFIG_BUFFER(buf);
 
-       r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
-       checksum = (r & 0xffff) + (r >> 16);
+       {
+               int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
+               s = (r & 0xffff) + (r >> 16);
 
-       printf("%d %s ", checksum,
-              make_human_readable_str(total_bytes, 1, 512));
-       if (print_name)
-               puts(file);
-       else
-               printf("\n");
+               printf("%d %ju ", s, (total_bytes+511)/512);
+       }
+       puts(print_name ? file : "");
 
        return 1;
 }
@@ -156,7 +138,6 @@ int sum_main(int argc, char **argv)
 {
        int flags;
        int ok;
-       int files_given;
        int (*sum_func)(const char *, int) = bsd_sum_file;
 
        /* give the bsd func priority over sysv func */
@@ -167,12 +148,11 @@ int sum_main(int argc, char **argv)
                sum_func = bsd_sum_file;
 
        have_read_stdin = 0;
-       files_given = argc - optind;
-       if (files_given <= 0)
-               ok = sum_func("-", files_given);
+       if ((argc - optind) == 0)
+               ok = sum_func("-", 0);
        else
                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)
                bb_perror_msg_and_die("-");