-/* $OpenBSD: md5.c,v 1.48 2007/11/07 09:52:25 chl Exp $ */
+/* $OpenBSD: md5.c,v 1.49 2008/07/29 18:24:31 sobrado Exp $ */
/*
* Copyright (c) 2001,2003,2005-2006 Todd C. Miller <Todd.Miller@courtesan.com>
#define MAX_DIGEST_LEN 128
+enum program_mode {
+ MODE_MD5,
+ MODE_SHA1,
+ MODE_RMD160,
+ MODE_CKSUM,
+ MODE_SUM
+} pmode;
+
union ANY_CTX {
CKSUM_CTX cksum;
MD4_CTX md4;
extern char *__progname;
int qflag = 0;
-#define OPTSTRING "a:bco:pqrs:tx"
-
int
main(int argc, char **argv)
{
int fl, error, base64;
int bflag, cflag, pflag, rflag, tflag, xflag;
+ static const char *optstr[5] = {
+ "bcpqrs:tx",
+ "bcpqrs:tx",
+ "bcpqrs:tx",
+ "a:bco:pqrs:tx",
+ "a:bco:pqrs:tx"
+ };
+
TAILQ_INIT(&hl);
input_string = NULL;
error = bflag = cflag = pflag = qflag = rflag = tflag = xflag = 0;
+ pmode = MODE_MD5;
+ if (strcmp(__progname, "md5") == 0)
+ pmode = MODE_MD5;
+ else if (strcmp(__progname, "sha1") == 0)
+ pmode = MODE_SHA1;
+ else if (strcmp(__progname, "rmd160") == 0)
+ pmode = MODE_RMD160;
+ else if (strcmp(__progname, "cksum") == 0)
+ pmode = MODE_CKSUM;
+ else if (strcmp(__progname, "sum") == 0)
+ pmode = MODE_SUM;
+
/* Check for -b option early since it changes behavior. */
- while ((fl = getopt(argc, argv, ":" OPTSTRING)) != -1) {
- if (fl == 'b')
+ while ((fl = getopt(argc, argv, optstr[pmode])) != -1) {
+ switch (fl) {
+ case 'b':
bflag = 1;
+ break;
+ case '?':
+ usage();
+ }
}
optind = 1;
optreset = 1;
- while ((fl = getopt(argc, argv, OPTSTRING)) != -1) {
+ while ((fl = getopt(argc, argv, optstr[pmode])) != -1) {
switch (fl) {
case 'a':
while ((cp = strsep(&optarg, " \t,")) != NULL) {
}
if (hf->base64 == -1 && base64 != -1) {
warnx("%s doesn't support %s",
- hf->name, base64 ? "base64" : "hex");
+ hf->name,
+ base64 ? "base64" : "hex");
usage();
}
/* Check for dupes. */
cmp = strcasecmp(checksum, digest);
if (cmp == 0) {
if (qflag == 0)
- (void)printf("(%s) %s: OK\n", algorithm, filename);
+ (void)printf("(%s) %s: OK\n", algorithm,
+ filename);
} else {
(void)printf("(%s) %s: FAILED\n", algorithm, filename);
error = 1;
void
usage(void)
{
- fprintf(stderr, "usage: %s [-bpqrtx] [-c [checklist ...]] "
- "[-s string] [file ...]\n", __progname);
- if (strcmp(__progname, "cksum") == 0)
- fprintf(stderr, " [-a algorithms]] [-o 1 | 2]\n");
+ switch (pmode) {
+ case MODE_MD5:
+ case MODE_SHA1:
+ case MODE_RMD160:
+ fprintf(stderr, "usage: %s [-bpqrtx] [-c [checklist ...]] "
+ "[-s string] [file ...]\n", __progname);
+ break;
+ case MODE_CKSUM:
+ case MODE_SUM:
+ fprintf(stderr, "usage: %s [-bpqrtx] [-a algorithms] "
+ "[-c [checklist ...]] [-o 1 | 2]\n"
+ " %*s [-s string] [file ...]\n",
+ __progname, (int)strlen(__progname), "");
+ break;
+ }
exit(EXIT_FAILURE);
}