On Mon, May 11, 2009 at 10:16:25AM +0200, Peter J. Philipp wrote:
> Hi,
>
> I did some research on different operating systems regarding checksumming and
> found that solaris had a nice option called "digest -l" which prints the
> available algorithms and exits. I wrote this functionality into cksum(1)
> that you can have if you want it. Patch follows.
>
> -peter
I kinda like this, but comments inline.
> ? cksum.1-orig
> ? cksum.patch
> ? md5.c-orig
> Index: cksum.1
> ===================================================================
> RCS file: /cvs/src/bin/md5/cksum.1,v
> retrieving revision 1.19
> diff -u -r1.19 cksum.1
> --- cksum.1 8 Feb 2009 17:15:09 -0000 1.19
> +++ cksum.1 11 May 2009 08:07:36 -0000
> @@ -42,7 +42,7 @@
> .Sh SYNOPSIS
> .Nm cksum
> .Bk -words
> -.Op Fl bpqrtx
> +.Op Fl blpqrtx
> .Op Fl a Ar algorithms
> .Op Fl c Op Ar checklist ...
> .Op Fl o Ar 1 | 2
> @@ -162,6 +162,8 @@
> option may not be used in conjunction with more than a single
> .Fl a
> option.
> +.It Fl l
> +outputs the algorithms available and exits.
> .It Fl o Ar 1 | 2
> Use historic algorithms instead of the (superior) default one
> (see below).
> Index: md5.c
> ===================================================================
> RCS file: /cvs/src/bin/md5/md5.c,v
> retrieving revision 1.50
> diff -u -r1.50 md5.c
> --- md5.c 6 Sep 2008 12:01:34 -0000 1.50
> +++ md5.c 11 May 2009 08:07:36 -0000
> @@ -201,6 +201,7 @@
> void usage(void) __attribute__((__noreturn__));
>
> extern char *__progname;
> +extern int errno;
Better include <errno.h>
> int qflag = 0;
>
> int
> @@ -210,14 +211,14 @@
> struct hash_list hl;
> size_t len;
> char *cp, *input_string;
> - int fl, error, base64;
> + int fl, error, base64, i;
> int bflag, cflag, pflag, rflag, tflag, xflag;
>
> static const char *optstr[5] = {
> "bcpqrs:tx",
> "bcpqrs:tx",
> "bcpqrs:tx",
> - "a:bco:pqrs:tx",
> + "a:bco:lpqrs:tx",
> "a:bco:pqrs:tx"
> };
>
> @@ -315,6 +316,20 @@
> if (hftmp == TAILQ_END(&hl))
> hash_insert(&hl, hf, 0);
> break;
> + case 'l':
> + for (hf = functions; hf->name != NULL; hf++) {
> + len = strlen(hf->name);
> + if ((cp = calloc(1, len + 1)) == NULL)
> + errx(1, "malloc: %s", strerror(errno));
better use
err(1, "malloc");
it has the strerror call built in
> +
> + for (i = 0; i < len; i++) {
> + cp[i] = tolower(*(hf->name + i));
better use the same style for the lhs and rhs of the assigmnent. i.e.:
cp[i] = tolower(hf->name[i]))
> + }
> +
> + printf("%s\n", cp);
> + free(cp);
> + }
> + exit(0);
> case 'p':
> pflag = 1;
> break;
Come to think of it, why don't you just putchar(tolower(hf->name[i]))
in a loop? Saves you the calloc and error handling.
Also, don't forget to fix usage().
-Otto