Hello,

I noticed that md5(1) returns 0 when you target a file that does not
exist:
=====
$ md5 foobar
md5: cannot open foobar: No such file or directory
$ echo $?
0
=====

This seems wrong according to the man page, and I have looked at the
FreeBSD and Linux equivalent tool which returns 1 in this case.

I have attempted to write a patch that changes this behaviour. My
limited testing has been successful at least, see diff below.

Regards,
Patrik Lundin

Index: md5.c
===================================================================
RCS file: /cvs/src/bin/md5/md5.c,v
retrieving revision 1.54
diff -u -r1.54 md5.c
--- md5.c       4 Dec 2012 02:38:51 -0000       1.54
+++ md5.c       28 Mar 2013 14:09:43 -0000
@@ -194,7 +194,7 @@
 TAILQ_HEAD(hash_list, hash_function);
 
 void digest_end(const struct hash_function *, void *, char *, size_t, int);
-void digest_file(const char *, struct hash_list *, int);
+int  digest_file(const char *, struct hash_list *, int);
 int  digest_filelist(const char *, struct hash_function *);
 void digest_print(const struct hash_function *, const char *, const char *);
 void digest_printstr(const struct hash_function *, const char *, const char *);
@@ -386,10 +386,10 @@
                                error += digest_filelist(*argv++,
                                    TAILQ_FIRST(&hl));
        } else if (pflag || argc == 0)
-               digest_file("-", &hl, pflag);
+               error = digest_file("-", &hl, pflag);
        else
                while (argc--)
-                       digest_file(*argv++, &hl, 0);
+                       error += digest_file(*argv++, &hl, 0);
 
        return(error ? EXIT_FAILURE : EXIT_SUCCESS);
 }
@@ -476,7 +476,7 @@
        }
 }
 
-void
+int
 digest_file(const char *file, struct hash_list *hl, int echo)
 {
        struct hash_function *hf;
@@ -489,7 +489,7 @@
                fp = stdin;
        else if ((fp = fopen(file, "r")) == NULL) {
                warn("cannot open %s", file);
-               return;
+               return(1);
        }
 
        if (echo)
@@ -510,7 +510,7 @@
                warn("%s: read error", file);
                if (fp != stdin)
                        fclose(fp);
-               return;
+               return(1);
        }
        if (fp != stdin)
                fclose(fp);
@@ -523,6 +523,7 @@
                else
                        digest_print(hf, file, digest);
        }
+       return(0);
 }
 
 /*

Reply via email to