Bug 5387

Summary: md5(1) does not use getopt(3)
Product: Base System Reporter: mph <mph>
Component: binAssignee: Steve Price <steve>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 2.2.5-STABLE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description mph 1997-12-28 07:00:01 UTC
/sbin/md5 does not use the getopt(3) interface to parse command-line
options.  The result is a non-standard interface.  IMHO, the differences
from getopt(3) are not features.

For example, the argument to "-s" must be given as "-sstring" and
not "-s string".

Fix: Apply the following patch.  md5(1) will then behave more like the
other Unix commands.  This patch also provides usage help if an invalid
option is given.
Comment 1 Wolfram Schneider 1997-12-28 11:48:33 UTC
Matthew Hunt <mph@pobox.com> writes:
> +static void
> +usage(void)
> +{
> +	(void)fprintf(stderr,
> +		"usage: md5 [-p] [-t] [-x] [-s string] [filename(s)]\n");

	"usage: md5 [-ptx] [-s string] [file ...]\n");

See style(9) or as an example the cat(1) manpage.

Wolfram
Comment 2 mph 1997-12-29 00:03:47 UTC
On Sun, Dec 28, 1997 at 12:48:33PM +0100, Wolfram Schneider wrote:

> See style(9) or as an example the cat(1) manpage.

Mea culpa.  Attached is a new diff that should be conformant.  I have
likewise changed the man page to appear more like the others.


diff -Nru /usr/src/sbin/md5/md5.1 md5/md5.1
--- /usr/src/sbin/md5/md5.1	Sat Sep  6 12:25:42 1997
+++ md5/md5.1	Sun Dec 28 19:00:56 1997
@@ -6,11 +6,9 @@
 .Nd calculate a message-digest fingerprint (checksum) for a file
 .Sh SYNOPSIS
 .Nm
-.Op Fl p
-.Op Fl t
-.Op Fl x
-.Op Fl s Ns Ar string
-.Op Ar filename Ns Pq s
+.Op Fl ptx
+.Op Fl s Ar string
+.Op Ar file ...
 .Sh DESCRIPTION
 .Nm
 takes as input a message of arbitrary length and produces
@@ -29,12 +27,12 @@
 key under a public-key cryptosystem such as
 .Em RSA .
 .Pp
-The following four options may be used in any combination, except
-that
-.Ar filename Ns Pq s
-must be the last objects on the command line.
+The following four options may be used in any combination and must
+precede any files named on the command line.  The MD5
+sum of each file listed on the command line is printed after the options
+are processed.
 .Bl -tag -width Fl
-.It Fl s Ns Ar string
+.It Fl s Ar string
 prints a checksum of the given
 .Dq string .
 .It Fl p
@@ -43,10 +41,6 @@
 runs a built-in time trial.
 .It Fl x
 runs a built-in test script.
-.It Ar filename Ns Pq s
-prints a checksum
-.Pq s
-for each of the files.
 .El
 .Sh SEE ALSO
 .Xr cksum 1
diff -Nru /usr/src/sbin/md5/md5.c md5/md5.c
--- /usr/src/sbin/md5/md5.c	Sat Sep  6 12:25:42 1997
+++ md5/md5.c	Sun Dec 28 18:53:10 1997
@@ -40,6 +40,7 @@
 static void MDTimeTrial PROTO_LIST((void));
 static void MDTestSuite PROTO_LIST((void));
 static void MDFilter PROTO_LIST((int));
+static void usage PROTO_LIST((void));
 
 /* Main driver.
 
@@ -58,25 +59,38 @@
 	int     i;
 	char   *p;
 	char	buf[33];
+	extern char 	*optarg;
+	extern int	optind;
 
-	if (argc > 1)
-		for (i = 1; i < argc; i++)
-			if (argv[i][0] == '-' && argv[i][1] == 's')
-				MDString(argv[i] + 2);
-			else if (strcmp(argv[i], "-t") == 0)
+	if (argc > 1) {
+		while ((i = getopt(argc, argv, "s:tpx")) != EOF) {
+			switch(i) {
+			case 's':
+				MDString(optarg);
+				break;
+			case 't':
 				MDTimeTrial();
-			else if (strcmp(argv[i], "-p") == 0)
+				break;
+			case 'p':
 				MDFilter(1);
-			else if (strcmp(argv[i], "-x") == 0)
+				break;
+			case 'x':
 				MDTestSuite();
-			else {
-				p = MD5File(argv[i],buf);
-				if (!p)
-					perror(argv[i]);
-				else
-					printf("MD5 (%s) = %s\n", argv[i], p);
+				break;
+			default:
+				usage();
 			}
-	else
+		}
+
+		while (optind < argc) {
+			p = MD5File(argv[optind],buf);
+			if (!p)
+				perror(argv[optind]);
+			else
+				printf("MD5 (%s) = %s\n", argv[optind], p);
+			optind++;
+		}
+	} else
 		MDFilter(0);
 
 	return (0);
@@ -174,4 +188,16 @@
 		MD5Update(&context, buffer, len);
 	}
 	printf("%s\n", MD5End(&context,buf));
+}
+
+/*
+ * Displays a usage summary.
+ */
+
+static void
+usage(void)
+{
+	(void)fprintf(stderr,
+		"usage: md5 [-ptx] [-s string] [file ...]\n");
+	exit(1);
 }
Comment 3 Steve Price freebsd_committer freebsd_triage 1997-12-29 03:45:10 UTC
State Changed
From-To: open->closed

Slightly modified patch applied to revision 1.5 of md5.1 
and revision 1.10 of md5.c.  Thanks.