jfb2 writes in Bug 119:
[oweals/busybox.git] / coreutils / md5_sha1_sum.c
index 9a243a26e91065211dccf3d8e828861d96c2a1a6..bd1c9fc29a60c6b085cc72ba0222373e8c0e73df 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Copyright (C) 2003 Glenn L. McGrath
- *  Copyright (C) 2003 Erik Andersen
- * 
+ *  Copyright (C) 2003-2004 Erik Andersen
+ *
  *  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
@@ -32,7 +32,7 @@
 #define FLAG_CHECK     2
 #define FLAG_WARN      4
 
-/* This might be usefull elsewhere */
+/* This might be useful elsewhere */
 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
                                                                          unsigned char hash_length)
 {
@@ -47,10 +47,43 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
        return (hex_value);
 }
 
+static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
+{
+       uint8_t *hash_value_bin;
+       uint8_t *hash_value = NULL;
+       uint8_t hash_length;
+       int src_fd;
+
+       if (strcmp(filename, "-") == 0) {
+               src_fd = STDIN_FILENO;
+       } else {
+               src_fd = open(filename, O_RDONLY);
+       }
+
+       if (hash_algo == HASH_MD5) {
+               hash_length = 16;
+       } else {
+               hash_length = 20;
+       }
+
+       hash_value_bin = xmalloc(hash_length);
+
+       if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
+               hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
+       } else {
+               bb_perror_msg("%s", filename);
+       }
+
+       close(src_fd);
+
+       return(hash_value);
+}
+
 /* This could become a common function for md5 as well, by using md5_stream */
 extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
 {
        int return_value = EXIT_SUCCESS;
+       uint8_t *hash_value;
 
 #ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
        unsigned int flags;
@@ -110,16 +143,19 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
                        hash_value = hash_file(filename_ptr, hash_algo);
 
                        if (hash_value && (strcmp(hash_value, line) == 0)) {
-                               printf("%s: OK\n", filename_ptr);
+                               if (!(flags & FLAG_SILENT))
+                                       printf("%s: OK\n", filename_ptr);
                        } else {
-                               printf("%s: FAILED\n", filename_ptr);
+                               if (!(flags & FLAG_SILENT))
+                                       printf("%s: FAILED\n", filename_ptr);
                                count_failed++;
+                               return_value = EXIT_FAILURE;
                        }
                        /* possible free(NULL) */
                        free(hash_value);
                        free(line);
                }
-               if (count_failed) {
+               if (count_failed && !(flags & FLAG_SILENT)) {
                        bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
                                                 count_failed, count_total);
                }
@@ -129,7 +165,6 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
        } else
 #endif
        {
-               uint8_t *hash_value_bin;
                uint8_t hash_length;
 
                if (hash_algo == HASH_MD5) {
@@ -137,34 +172,16 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
                } else {
                        hash_length = 20;
                }
-               hash_value_bin = xmalloc(hash_length);
+               hash_value = xmalloc(hash_length);
 
                while (optind < argc) {
                        unsigned char *file_ptr = argv[optind++];
-                       uint8_t *hash_value;
-                       int src_fd;
-
-                       if ((file_ptr[0] == '-') && (file_ptr[1] == '\0')) {
-                               src_fd = fileno(stdin);
-                       } else {
-                               src_fd = open(file_ptr, O_RDONLY);
-                       }
-
-                       if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
-                               hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
-                       } else {
-                               bb_perror_msg("%s", file_ptr);
-                               continue;
-                       }
-                       close(src_fd);
 
+                       hash_value = hash_file(file_ptr, hash_algo);
                        if (hash_value == NULL) {
-                               return_value++;
+                               return_value = EXIT_FAILURE;
                        } else {
-#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
-                               if (!flags & FLAG_SILENT)
-#endif
-                                       printf("%s  %s\n", hash_value, file_ptr);
+                               printf("%s  %s\n", hash_value, file_ptr);
                                free(hash_value);
                        }
                }