aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-11-18 19:35:06 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-11-18 19:35:06 +0000
commitde3ea9f049167fdd6813f53af23096b6110afe05 (patch)
tree4fb19635a41ff089a0c441024e01d230b3b4a06e
parentaee0fd8320514627d8ea5306bd4666bcd74d9c46 (diff)
downloadbusybox-w32-de3ea9f049167fdd6813f53af23096b6110afe05.tar.gz
busybox-w32-de3ea9f049167fdd6813f53af23096b6110afe05.tar.bz2
busybox-w32-de3ea9f049167fdd6813f53af23096b6110afe05.zip
Only use getopt and associated flags if checking is enabled
-rw-r--r--coreutils/md5_sha1_sum.c80
1 files changed, 36 insertions, 44 deletions
diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c
index f7c44c850..9a243a26e 100644
--- a/coreutils/md5_sha1_sum.c
+++ b/coreutils/md5_sha1_sum.c
@@ -47,49 +47,15 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
47 return (hex_value); 47 return (hex_value);
48} 48}
49 49
50static uint8_t *hash_file(const char *filename, uint8_t hash_algo)
51{
52 uint8_t *hash_value_bin;
53 uint8_t *hash_value = NULL;
54 uint8_t hash_length;
55 int src_fd;
56
57 if (strcmp(filename, "-") == 0) {
58 src_fd = fileno(stdin);
59 } else {
60 src_fd = open(filename, O_RDONLY);
61 }
62
63 if (hash_algo == HASH_MD5) {
64 hash_length = 16;
65 } else {
66 hash_length = 20;
67 }
68
69 hash_value_bin = xmalloc(hash_length);
70
71 if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
72 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
73 } else {
74 bb_perror_msg("%s", filename);
75 }
76
77 close(src_fd);
78
79 return(hash_value);
80}
81
82/* This could become a common function for md5 as well, by using md5_stream */ 50/* This could become a common function for md5 as well, by using md5_stream */
83extern int hash_files(int argc, char **argv, const uint8_t hash_algo) 51extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
84{ 52{
85 uint8_t *hash_value;
86 unsigned int flags;
87 int return_value = EXIT_SUCCESS; 53 int return_value = EXIT_SUCCESS;
88 54
89#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK 55#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
56 unsigned int flags;
57
90 flags = bb_getopt_ulflags(argc, argv, "scw"); 58 flags = bb_getopt_ulflags(argc, argv, "scw");
91#else
92 flags = bb_getopt_ulflags(argc, argv, "s");
93#endif 59#endif
94 60
95#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK 61#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
@@ -162,21 +128,47 @@ extern int hash_files(int argc, char **argv, const uint8_t hash_algo)
162 } 128 }
163 } else 129 } else
164#endif 130#endif
131 {
132 uint8_t *hash_value_bin;
133 uint8_t hash_length;
134
135 if (hash_algo == HASH_MD5) {
136 hash_length = 16;
137 } else {
138 hash_length = 20;
139 }
140 hash_value_bin = xmalloc(hash_length);
141
165 while (optind < argc) { 142 while (optind < argc) {
166 unsigned char *file_ptr = argv[optind]; 143 unsigned char *file_ptr = argv[optind++];
144 uint8_t *hash_value;
145 int src_fd;
146
147 if ((file_ptr[0] == '-') && (file_ptr[1] == '\0')) {
148 src_fd = fileno(stdin);
149 } else {
150 src_fd = open(file_ptr, O_RDONLY);
151 }
152
153 if ((src_fd != -1) && (hash_fd(src_fd, -1, hash_algo, hash_value_bin) != -2)) {
154 hash_value = hash_bin_to_hex(hash_value_bin, hash_length);
155 } else {
156 bb_perror_msg("%s", file_ptr);
157 continue;
158 }
159 close(src_fd);
167 160
168 optind++;
169
170 hash_value = hash_file(file_ptr, hash_algo);
171 if (hash_value == NULL) { 161 if (hash_value == NULL) {
172 return_value++; 162 return_value++;
173 } 163 } else {
174 else if (!flags & FLAG_SILENT) { 164#ifdef CONFIG_FEATURE_MD5_SHA1_SUM_CHECK
175 printf("%s %s\n", hash_value, file_ptr); 165 if (!flags & FLAG_SILENT)
166#endif
167 printf("%s %s\n", hash_value, file_ptr);
176 free(hash_value); 168 free(hash_value);
177 } 169 }
178 } 170 }
179 171 }
180 return (return_value); 172 return (return_value);
181} 173}
182 174