The sha256 supplied in bsd.rd is compiled from md5.c with the option
-DSHA2_ONLY.

This means that -a, -c and -C are not supported, and as far as I can tell
from the CVS log, this is indeed the intended behavior. However md5.c still
contains references to these arguments in both the usage string (-c and -C),
the string for getopt (-c and -C) and in the getopt switch statement (only
-a).

There are some additional small problems related to this switch.

If compiled with -DSHA2_ONLY, then the function digest_filelist is not
#ifdef'ed in, but has a prototype. The function digest_printstr does not
have a prototype, but is #ifdef'ed in (but unused).

I have included a patch, that I believe will fix all these problems. Please
tell me if I made a mistake somewhere, or if the patch is not good enough.
:)

There is also a check that can be determined at compile-time if SHA2_ONLY is
defined. I have included a change that will put the check inside an #if,
however I am not sure if this is desired or not.

--
Mathias Svensson
--- md5.c
+++ md5.c
@@ -174,9 +174,9 @@ TAILQ_HEAD(hash_list, hash_function);
 
 void digest_end(const struct hash_function *, void *, char *, size_t, int);
 int  digest_file(const char *, struct hash_list *, int);
-int  digest_filelist(const char *, struct hash_function *, int, char **);
 void digest_print(const struct hash_function *, const char *, const char *);
 #if !defined(SHA2_ONLY)
+int  digest_filelist(const char *, struct hash_function *, int, char **);
 void digest_printstr(const struct hash_function *, const char *, const char *);
 void digest_string(char *, struct hash_list *);
 void digest_test(struct hash_list *);
@@ -205,12 +205,14 @@ main(int argc, char **argv)
 	selective_checklist = NULL;
 	error = bflag = cflag = pflag = qflag = rflag = tflag = xflag = 0;
 
-#if !defined(SHA2_ONLY)
+#if defined(SHA2_ONLY)
+	optstr = "bh:pqrs:tx";
+#else /* defined(SHA2_ONLY) */
 	if (strcmp(__progname, "cksum") == 0)
 		optstr = "a:bC:ch:pqrs:tx";
 	else
-#endif /* !defined(SHA2_ONLY) */
 		optstr = "bC:ch:pqrs:tx";
+#endif /* defined(SHA2_ONLY) */
 
 	/* Check for -b option early since it changes behavior. */
 	while ((fl = getopt(argc, argv, optstr)) != -1) {
@@ -226,6 +228,7 @@ main(int argc, char **argv)
 	optreset = 1;
 	while ((fl = getopt(argc, argv, optstr)) != -1) {
 		switch (fl) {
+#if !defined(SHA2_ONLY)
 		case 'a':
 			while ((cp = strsep(&optarg, " \t,")) != NULL) {
 				if (*cp == '\0')
@@ -267,6 +270,7 @@ main(int argc, char **argv)
 					hash_insert(&hl, hf, base64);
 			}
 			break;
+#endif /* !defined(SHA2_ONLY) */
 		case 'b':
 			/* has already been parsed */
 			break;
@@ -316,11 +320,13 @@ main(int argc, char **argv)
 	if (fl > 1 || (fl && argc && cflag == 0) || (rflag && qflag) ||
 	    (selective_checklist != NULL && argc == 0))
 		usage();
+#if !defined(SHA2_ONLY)
 	if (selective_checklist || cflag) {
 		if (TAILQ_FIRST(&hl) != TAILQ_LAST(&hl, hash_list))
 			errx(1, "only a single algorithm may be specified "
 			    "in -C or -c mode");
 	}
+#endif /* !defined(SHA2_ONLY) */
 
 	/* No algorithm specified, check the name we were called as. */
 	if (TAILQ_EMPTY(&hl)) {
@@ -442,6 +448,7 @@ digest_print(const struct hash_function *hf, const cha
 	}
 }
 
+#if !defined(SHA2_ONLY)
 void
 digest_printstr(const struct hash_function *hf, const char *what,
     const char *digest)
@@ -458,6 +465,7 @@ digest_printstr(const struct hash_function *hf, const 
 		break;
 	}
 }
+#endif /* !defined(SHA2_ONLY) */
 
 int
 digest_file(const char *file, struct hash_list *hl, int echo)
@@ -798,18 +806,23 @@ digest_test(struct hash_list *hl)
 void
 usage(void)
 {
-#if !defined(SHA2_ONLY)
+#if defined(SHA2_ONLY)
+	fprintf(stderr, "usage:"
+	    "\t%s [-bpqrtx] [-h hashfile] [-s string] "
+	    "[file ...]\n",
+	    __progname);
+#else /* defined(SHA2_ONLY) */ 
 	if (strcmp(__progname, "cksum") == 0)
 		fprintf(stderr, "usage: %s [-bcpqrtx] [-a algorithms] [-C checklist] "
 		    "[-h hashfile]\n"
 		    "	[-s string] [file ...]\n",
 		    __progname);
 	else
-#endif /* !defined(SHA2_ONLY) */
 		fprintf(stderr, "usage:"
 		    "\t%s [-bcpqrtx] [-C checklist] [-h hashfile] [-s string] "
 		    "[file ...]\n",
 		    __progname);
+#endif /* !defined(SHA2_ONLY) */
 
 	exit(EXIT_FAILURE);
 }

Reply via email to