Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 28 Dec 1997 01:51:33 -0500 (EST)
From:      Matthew Hunt <mph@pobox.com>
To:        FreeBSD-gnats-submit@FreeBSD.ORG
Subject:   bin/5387: md5(1) does not use getopt(3)
Message-ID:  <199712280651.BAA06973@mph124.rh.psu.edu>
Resent-Message-ID: <199712280700.XAA23687@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         5387
>Category:       bin
>Synopsis:       md5(1) does not use getopt(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Dec 27 23:00:01 PST 1997
>Last-Modified:
>Originator:     Matthew Hunt
>Organization:
none
>Release:        FreeBSD 2.2.5-STABLE i386
>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);
 }
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199712280651.BAA06973>