From 716847a66938ee1256b344179295e37a0fe97fc9 Mon Sep 17 00:00:00 2001 From: Steve Price Date: Mon, 29 Dec 1997 03:40:37 +0000 Subject: [PATCH] Get md5(1) to use getopt(3). Also some minor -Wall cleaning while here. PR: 5387 Submitted by: Matthew Hunt --- sbin/md5/md5.1 | 24 ++++++++------------ sbin/md5/md5.c | 59 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/sbin/md5/md5.1 b/sbin/md5/md5.1 index cadc2066af18..676e4a00c555 100644 --- a/sbin/md5/md5.1 +++ b/sbin/md5/md5.1 @@ -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,24 +27,20 @@ in a secure manner before being encrypted with a private 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 . +.Ar string . .It Fl p echos stdin to stdout and appends the MD5 sum to stdout. .It Fl t 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 --git a/sbin/md5/md5.c b/sbin/md5/md5.c index 02dbd2d2a9cd..1ab105146943 100644 --- a/sbin/md5/md5.c +++ b/sbin/md5/md5.c @@ -1,5 +1,5 @@ /* - * $Id$ + * $Id: md5.c,v 1.10 1997/02/22 14:32:37 peter Exp $ * * Derived from: */ @@ -27,6 +27,7 @@ #include #include #include +#include #include "global.h" @@ -40,6 +41,7 @@ static void MDString PROTO_LIST((char *)); 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. @@ -59,24 +61,34 @@ main(argc, argv) char *p; char buf[33]; - 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) - MDTimeTrial(); - else if (strcmp(argv[i], "-p") == 0) + if (argc > 1) { + while ((i = getopt(argc, argv, "ps:tx")) != EOF) { + switch (i) { + case 'p': MDFilter(1); - else if (strcmp(argv[i], "-x") == 0) + break; + case 's': + MDString(optarg); + break; + case 't': + MDTimeTrial(); + 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); @@ -162,11 +174,11 @@ MDFilter(int pipe) { MD5_CTX context; int len; - unsigned char buffer[BUFSIZ], digest[16]; + unsigned char buffer[BUFSIZ]; char buf[33]; MD5Init(&context); - while (len = fread(buffer, 1, BUFSIZ, stdin)) { + while ((len = fread(buffer, 1, BUFSIZ, stdin))) { if(pipe && (len != fwrite(buffer, 1, len, stdout))) { perror("stdout"); exit(1); @@ -175,3 +187,14 @@ MDFilter(int pipe) } 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); +}