aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-23 15:59:12 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-23 15:59:12 +0000
commit8517371905ddd69d43154e5eb029366e18f43a91 (patch)
treeaecd9a1b0bab2d639961be3481d6dd25cef3be87
parent6e5ed45d045322f3461fa6cb69ea380d1c1b45c0 (diff)
downloadbusybox-w32-8517371905ddd69d43154e5eb029366e18f43a91.tar.gz
busybox-w32-8517371905ddd69d43154e5eb029366e18f43a91.tar.bz2
busybox-w32-8517371905ddd69d43154e5eb029366e18f43a91.zip
md5sum: make -c grok formats like "68b329da9893e34099c7d8ad5cb9c940 *foo".
Also eradicate 95 bytes of bloat. git-svn-id: svn://busybox.net/trunk/busybox@16648 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/md5_sha1_sum.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index 93d894655..e8d3a1509 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -16,17 +16,14 @@ typedef enum { HASH_SHA1, HASH_MD5 } hash_algo_t;
16 16
17/* This might be useful elsewhere */ 17/* This might be useful elsewhere */
18static unsigned char *hash_bin_to_hex(unsigned char *hash_value, 18static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
19 unsigned char hash_length) 19 unsigned hash_length)
20{ 20{
21 int x, len, max; 21 int len = 0;
22 unsigned char *hex_value; 22 char *hex_value = xmalloc((hash_length * 2) + 2);
23 23 while (hash_length--) {
24 max = (hash_length * 2) + 2; 24 len += sprintf(hex_value + len, "%02x", *hash_value++);
25 hex_value = xmalloc(max);
26 for (x = len = 0; x < hash_length; x++) {
27 len += snprintf((char*)(hex_value + len), max - len, "%02x", hash_value[x]);
28 } 25 }
29 return (hex_value); 26 return hex_value;
30} 27}
31 28
32static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo) 29static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
@@ -41,11 +38,13 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
41 void (*update)(const void*, size_t, void*); 38 void (*update)(const void*, size_t, void*);
42 void (*final)(void*, void*); 39 void (*final)(void*, void*);
43 40
44 if (strcmp(filename, "-") == 0) { 41 src_fd = STDIN_FILENO;
45 src_fd = STDIN_FILENO; 42 if (filename[0] != '-' || filename[1]) { /* not "-" */
46 } else if(0 > (src_fd = open(filename, O_RDONLY))) { 43 src_fd = open(filename, O_RDONLY);
47 bb_perror_msg("%s", filename); 44 if (src_fd < 0) {
48 return NULL; 45 bb_perror_msg("%s", filename);
46 return NULL;
47 }
49 } 48 }
50 49
51 /* figure specific hash algorithims */ 50 /* figure specific hash algorithims */
@@ -63,7 +62,7 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
63 bb_error_msg_and_die("algorithm not supported"); 62 bb_error_msg_and_die("algorithm not supported");
64 } 63 }
65 64
66 while (0 < (count = read(src_fd, in_buf, 4096))) { 65 while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
67 update(in_buf, count, &context); 66 update(in_buf, count, &context);
68 } 67 }
69 68
@@ -85,7 +84,7 @@ int md5_sha1_sum_main(int argc, char **argv)
85{ 84{
86 int return_value = EXIT_SUCCESS; 85 int return_value = EXIT_SUCCESS;
87 uint8_t *hash_value; 86 uint8_t *hash_value;
88 unsigned int flags; 87 unsigned flags;
89 hash_algo_t hash_algo = ENABLE_MD5SUM 88 hash_algo_t hash_algo = ENABLE_MD5SUM
90 ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5) 89 ? (ENABLE_SHA1SUM ? (**argv=='m' ? HASH_MD5 : HASH_SHA1) : HASH_MD5)
91 : HASH_SHA1; 90 : HASH_SHA1;
@@ -108,7 +107,7 @@ int md5_sha1_sum_main(int argc, char **argv)
108 argv[argc++] = "-"; 107 argv[argc++] = "-";
109 } 108 }
110 109
111 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && flags & FLAG_CHECK) { 110 if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
112 FILE *pre_computed_stream; 111 FILE *pre_computed_stream;
113 int count_total = 0; 112 int count_total = 0;
114 int count_failed = 0; 113 int count_failed = 0;
@@ -120,9 +119,8 @@ int md5_sha1_sum_main(int argc, char **argv)
120 ("only one argument may be specified when using -c"); 119 ("only one argument may be specified when using -c");
121 } 120 }
122 121
123 if (strcmp(file_ptr, "-") == 0) { 122 pre_computed_stream = stdin;
124 pre_computed_stream = stdin; 123 if (file_ptr[0] != '-' || file_ptr[1]) { /* not "-" */
125 } else {
126 pre_computed_stream = xfopen(file_ptr, "r"); 124 pre_computed_stream = xfopen(file_ptr, "r");
127 } 125 }
128 126
@@ -131,6 +129,10 @@ int md5_sha1_sum_main(int argc, char **argv)
131 129
132 count_total++; 130 count_total++;
133 filename_ptr = strstr(line, " "); 131 filename_ptr = strstr(line, " ");
132 /* handle format for binary checksums */
133 if (filename_ptr == NULL) {
134 filename_ptr = strstr(line, " *");
135 }
134 if (filename_ptr == NULL) { 136 if (filename_ptr == NULL) {
135 if (flags & FLAG_WARN) { 137 if (flags & FLAG_WARN) {
136 bb_error_msg("invalid format"); 138 bb_error_msg("invalid format");
@@ -162,9 +164,11 @@ int md5_sha1_sum_main(int argc, char **argv)
162 bb_error_msg("WARNING: %d of %d computed checksums did NOT match", 164 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
163 count_failed, count_total); 165 count_failed, count_total);
164 } 166 }
167 /*
165 if (fclose_if_not_stdin(pre_computed_stream) == EOF) { 168 if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
166 bb_perror_msg_and_die("cannot close file %s", file_ptr); 169 bb_perror_msg_and_die("cannot close file %s", file_ptr);
167 } 170 }
171 */
168 } else { 172 } else {
169 while (optind < argc) { 173 while (optind < argc) {
170 char *file_ptr = argv[optind++]; 174 char *file_ptr = argv[optind++];