--- trunk/mkinitrd-magellan/busybox/coreutils/md5_sha1_sum.c 2007/09/01 22:45:15 532 +++ trunk/mkinitrd-magellan/busybox/coreutils/md5_sha1_sum.c 2009/04/24 18:33:46 816 @@ -6,7 +6,7 @@ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ -#include "busybox.h" +#include "libbb.h" typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t; @@ -21,7 +21,7 @@ /* xzalloc zero-terminates */ char *hex_value = xzalloc((hash_length * 2) + 1); bin2hex(hex_value, (char*)hash_value, hash_length); - return hex_value; + return (unsigned char *)hex_value; } static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo) @@ -33,28 +33,24 @@ } context; uint8_t *hash_value = NULL; RESERVE_CONFIG_UBUFFER(in_buf, 4096); - void (*update)(const void*, size_t, void*); - void (*final)(void*, void*); + void FAST_FUNC (*update)(const void*, size_t, void*); + void FAST_FUNC (*final)(void*, void*); - src_fd = STDIN_FILENO; - if (NOT_LONE_DASH(filename)) { - src_fd = open(filename, O_RDONLY); - if (src_fd < 0) { - bb_perror_msg("%s", filename); - return NULL; - } + src_fd = open_or_warn_stdin(filename); + if (src_fd < 0) { + return NULL; } /* figure specific hash algorithims */ if (ENABLE_MD5SUM && hash_algo==HASH_MD5) { md5_begin(&context.md5); - update = (void (*)(const void*, size_t, void*))md5_hash; - final = (void (*)(void*, void*))md5_end; + update = (void*)md5_hash; + final = (void*)md5_end; hash_len = 16; } else if (ENABLE_SHA1SUM && hash_algo==HASH_SHA1) { sha1_begin(&context.sha1); - update = (void (*)(const void*, size_t, void*))sha1_hash; - final = (void (*)(void*, void*))sha1_end; + update = (void*)sha1_hash; + final = (void*)sha1_end; hash_len = 20; } else { bb_error_msg_and_die("algorithm not supported"); @@ -78,18 +74,23 @@ return hash_value; } -int md5_sha1_sum_main(int argc, char **argv) +int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv) { int return_value = EXIT_SUCCESS; uint8_t *hash_value; unsigned flags; hash_algo_t hash_algo = ENABLE_MD5SUM - ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) + ? (ENABLE_SHA1SUM ? (applet_name[0] == 'm' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) : HASH_SHA1; if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) - flags = getopt32(argc, argv, "scw"); + flags = getopt32(argv, "scw"); else optind = 1; + argv += optind; + //argc -= optind; + if (!*argv) + *--argv = (char*)"-"; if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { if (flags & FLAG_SILENT) { @@ -101,28 +102,20 @@ } } - if (argc == optind) { - argv[argc++] = "-"; - } - if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { FILE *pre_computed_stream; int count_total = 0; int count_failed = 0; - char *file_ptr = argv[optind]; char *line; - if (optind + 1 != argc) { + if (argv[1]) { bb_error_msg_and_die ("only one argument may be specified when using -c"); } - pre_computed_stream = stdin; - if (NOT_LONE_DASH(file_ptr)) { - pre_computed_stream = xfopen(file_ptr, "r"); - } + pre_computed_stream = xfopen_stdin(argv[0]); - while ((line = xmalloc_getline(pre_computed_stream)) != NULL) { + while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) { char *filename_ptr; count_total++; @@ -168,17 +161,15 @@ } */ } else { - while (optind < argc) { - char *file_ptr = argv[optind++]; - - hash_value = hash_file(file_ptr, hash_algo); + do { + hash_value = hash_file(*argv, hash_algo); if (hash_value == NULL) { return_value = EXIT_FAILURE; } else { - printf("%s %s\n", hash_value, file_ptr); + printf("%s %s\n", hash_value, *argv); free(hash_value); } - } + } while (*++argv); } return return_value; }