From mph@mph124.rh.psu.edu  Sat Dec 27 22:52:12 1997
Received: from mph124.rh.psu.edu (mph@i.cant.think.of.a.cool.hostname.ending.in.rsd.jtwn.k12.pa.us [147.160.218.4])
          by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id WAA23262
          for <FreeBSD-gnats-submit@freebsd.org>; Sat, 27 Dec 1997 22:51:55 -0800 (PST)
          (envelope-from mph@mph124.rh.psu.edu)
Received: (from mph@localhost)
	by mph124.rh.psu.edu (8.8.8/8.8.8) id BAA06973;
	Sun, 28 Dec 1997 01:51:33 -0500 (EST)
	(envelope-from mph)
Message-Id: <199712280651.BAA06973@mph124.rh.psu.edu>
Date: Sun, 28 Dec 1997 01:51:33 -0500 (EST)
From: Matthew Hunt <mph@pobox.com>
To: FreeBSD-gnats-submit@freebsd.org
Subject: md5(1) does not use getopt(3)
X-Send-Pr-Version: 3.2

>Number:         5387
>Category:       bin
>Synopsis:       md5(1) does not use getopt(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    steve
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Dec 27 23:00:01 PST 1997
>Closed-Date:    Sun Dec 28 19:45:10 PST 1997
>Last-Modified:  Sun Dec 28 19:46:24 PST 1997
>Originator:     Matthew Hunt
>Release:        FreeBSD 2.2.5-STABLE i386
>Organization:
none
>Environment:
FreeBSD mph124.rh.psu.edu 2.2.5-STABLE FreeBSD 2.2.5-STABLE #1: Fri Dec 19 11:07:21 EST 1997     hunt@mph124.rh.psu.edu:/usr/src/sys/compile/WOPR  i386

$Id: md5.c,v 1.7.2.1 1997/09/03 06:49:48 jkh Exp $

CVS web interface shows 3.0-CURRENT md5.c to be essentially identical.

>Description:

/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".

>How-To-Repeat:
>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.


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	Sat Dec 27 02:35:42 1997
@@ -9,7 +9,7 @@
 .Op Fl p
 .Op Fl t
 .Op Fl x
-.Op Fl s Ns Ar string
+.Op Fl s Ar string
 .Op Ar filename Ns Pq s
 .Sh DESCRIPTION
 .Nm
@@ -34,7 +34,7 @@
 .Ar filename Ns Pq s
 must be the last objects on the command line.
 .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
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	Sat Dec 27 02:34:14 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 [-p] [-t] [-x] [-s string] [filename(s)]\n");
+	exit(1);
 }
>Release-Note:
>Audit-Trail:

From: Wolfram Schneider <wosch@cs.tu-berlin.de>
To: Matthew Hunt <mph@pobox.com>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/5387: md5(1) does not use getopt(3)
Date: 28 Dec 1997 12:48:33 +0100

 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

From: Matthew Hunt <mph@astro.psu.edu>
To: Wolfram Schneider <wosch@cs.tu-berlin.de>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/5387: md5(1) does not use getopt(3)
Date: Sun, 28 Dec 1997 19:03:47 -0500

 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);
  }
State-Changed-From-To: open->closed 
State-Changed-By: steve 
State-Changed-When: Sun Dec 28 19:45:10 PST 1997 
State-Changed-Why:  
Slightly modified patch applied to revision 1.5 of md5.1 
and revision 1.10 of md5.c.  Thanks. 
>Unformatted:
