[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

md5/sha1/rmd160 patch



i've patched the md5 program to take a new argument, -P, which rather
than appending the digest to the stdout stream-- after the hashed data--
will append it to a file instead.

i have two questions:
	- the current patch just appends the hex digest to the file. should
	  i also add a \n, and/or print [file name] = [digest] like the
	  standard output is?
	- i probably messed up the mode when opening up the file for
	  appending: rw-rw-rw. what should this be? is their a default macro
	  to use?

if anybody finds it useful, here's the md5.c patch. i also have patches
for the man pages as well, if this could possibly go in the tree :)

--- /usr/src/bin/md5/md5.c	Thu Sep  6 06:29:08 2001
+++ ./md5.c	Thu Jul 25 09:22:57 2002
@@ -28,6 +28,7 @@
  */
 
 #include <sys/param.h>
+#include <sys/stat.h>
 #include <err.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -73,7 +74,7 @@
 
 extern char *__progname;
 static void usage(void);
-static void digest_file(char *, struct hash_functions *, int);
+static void digest_file(char *, struct hash_functions *, int, char *);
 static void digest_string(char *, struct hash_functions *);
 static void digest_test(struct hash_functions *);
 static void digest_time(struct hash_functions *);
@@ -84,6 +85,8 @@
 	int fl, digest_type;
 	int pflag, tflag, xflag;
 	char *input_string;
+	char *chksum_file;
+
 
 	/* Set digest type based on program name, defaults to MD5. */
 	if (strcmp(__progname, "rmd160") == 0)
@@ -94,9 +97,12 @@
 		digest_type = DIGEST_MD5;
 
 	input_string = NULL;
+	chksum_file = NULL;
 	pflag = tflag = xflag = 0;
-	while ((fl = getopt(argc, argv, "ps:tx")) != -1) {
+	while ((fl = getopt(argc, argv, "P:ps:tx")) != -1) {
 		switch (fl) {
+		case 'P':
+			chksum_file = optarg;
 		case 'p':
 			pflag = 1;
 			break;
@@ -128,10 +134,10 @@
 	else if (input_string)
 		digest_string(input_string, &functions[digest_type]);
 	else if (pflag || argc == 0)
-		digest_file("-", &functions[digest_type], pflag);
+		digest_file("-", &functions[digest_type], pflag, chksum_file);
 	else
 		while (argc--)
-			digest_file(*argv++, &functions[digest_type], 0);
+			digest_file(*argv++, &functions[digest_type], 0, NULL);
 
 	exit(0);
 }
@@ -147,17 +153,23 @@
 }
 
 static void
-digest_file(char *file, struct hash_functions *hf, int echo)
+digest_file(char *file, struct hash_functions *hf, int echo, char *chksum_file)
 {
-	int fd;
+	int fd, chksum_fd = -1;
 	ssize_t nread;
 	u_char data[BUFSIZ];
 	char *digest;
 	union ANY_CTX context;
 
-	if (strcmp(file, "-") == 0)
+	if (strcmp(file, "-") == 0) {
 		fd = STDIN_FILENO;
-	else if ((fd = open(file, O_RDONLY, 0)) == -1) {
+
+		if (chksum_file && (chksum_fd = open(chksum_file,O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1) {
+			warn("cannot open %s", chksum_file);
+			return;
+		}
+
+	} else if ((fd = open(file, O_RDONLY, 0)) == -1) {
 		warn("cannot open %s", file);
 		return;
 	}
@@ -173,9 +185,9 @@
 	}
 	digest = hf->end(&context, NULL);
 
-	if (fd == STDIN_FILENO) {
-		(void)puts(digest);
-	} else {
+	if (fd == STDIN_FILENO)
+		(void)write(((chksum_fd == -1)?fd:chksum_fd), digest, strlen(digest));
+	else {
 		close(fd);
 		(void)printf("%s (%s) = %s\n", hf->name, file, digest);
 	}
@@ -264,7 +276,7 @@
 static void
 usage()
 {
-	fprintf(stderr, "usage: %s [-p | -t | -x | -s string | file ...]\n",
+	fprintf(stderr, "usage: %s [-p | -P file | -t | -x | -s string | file ...]\n",
 	    __progname);
 	exit(1);
 }