[BusyBox-cvs] busybox/coreutils md5_sha1_sum.c,1.2,1.3

Glenn McGrath bug1 at busybox.net
Tue Nov 18 23:56:46 UTC 2003


Update of /var/cvs/busybox/coreutils
In directory winder:/tmp/cvs-serv24857/coreutils

Modified Files:
	md5_sha1_sum.c 
Log Message:
woops, we needed that function


Index: md5_sha1_sum.c
===================================================================
RCS file: /var/cvs/busybox/coreutils/md5_sha1_sum.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- md5_sha1_sum.c	18 Nov 2003 19:35:06 -0000	1.2
+++ md5_sha1_sum.c	18 Nov 2003 23:56:41 -0000	1.3
@@ -47,10 +47,43 @@
 	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 = fileno(stdin);
+	} 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;
@@ -129,7 +162,6 @@
 	} else
 #endif
 	{
-		uint8_t *hash_value_bin;
 		uint8_t hash_length;
 
 		if (hash_algo == HASH_MD5) {
@@ -137,27 +169,12 @@
 		} 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++;
 			} else {




More information about the busybox-cvs mailing list