[hackers] [sbase] Refactor cryptcheck() to allow multiple list-files and stdin || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 3 Mar 2015 14:32:25 +0100 (CET)

commit e390151e99593778192545f9e1bfc6c2d6248419
Author: FRIGN <dev_AT_frign.de>
Date: Sun Mar 1 22:51:52 2015 +0100

    Refactor cryptcheck() to allow multiple list-files and stdin
    
    Previously, it was not possible to use
    
    sha1sum test.c | sha1sum -c
    
    because the program would not differenciate between an empty
    argument and a non-specified argument.
    Moreover, why not allow this?
    
    sha1sum -c hashlist1 hashlist2
    
    Digging deeper I found that using function pointers and a
    modification in the crypt-backend might simplify the program
    a lot by passing the argument-list to both cryptmain and
    cryptcheck.
    Allowing more than one list-file to be specified is also
    consistent with what the other implementations support,
    so we not only have simpler code, we also do not silently
    break if there's a script around passing multiple files to
    check.

diff --git a/crypt.h b/crypt.h
index 1855d37..e0cc08d 100644
--- a/crypt.h
+++ b/crypt.h
_AT_@ -6,7 +6,7 @@ struct crypt_ops {
         void *s;
 };
 
-int cryptcheck(char *, int, char **, struct crypt_ops *, uint8_t *, size_t);
+int cryptcheck(int, char **, struct crypt_ops *, uint8_t *, size_t);
 int cryptmain(int, char **, struct crypt_ops *, uint8_t *, size_t);
 int cryptsum(struct crypt_ops *, FILE *, const char *, uint8_t *);
 void mdprint(const uint8_t *, const char *, size_t);
diff --git a/libutil/crypt.c b/libutil/crypt.c
index 2d2ab39..9237cc5 100644
--- a/libutil/crypt.c
+++ b/libutil/crypt.c
_AT_@ -37,27 +37,21 @@ mdcheckline(const char *s, uint8_t *md, size_t sz)
         return (i == sz) ? 1 : 0;
 }
 
-int
-cryptcheck(char *sumfile, int argc, char *argv[],
- struct crypt_ops *ops, uint8_t *md, size_t sz)
-{
- FILE *cfp, *fp;
- char *line = NULL, *file, *p;
- int r, nonmatch = 0, formatsucks = 0, noread = 0, ret = 0;
+static void
+mdchecklist(FILE *listfp, struct crypt_ops *ops, uint8_t *md, size_t sz,
+ int *formatsucks, int *noread, int *nonmatch) {
+ FILE *fp;
         size_t bufsiz = 0;
+ int r;
+ char *line = NULL, *file, *p;
 
- if (!sumfile)
- cfp = stdin;
- else if (!(cfp = fopen(sumfile, "r")))
- eprintf("fopen %s:", sumfile);
-
- while (getline(&line, &bufsiz, cfp) != -1) {
+ while (getline(&line, &bufsiz, listfp) != -1) {
                 if (!(file = strstr(line, " "))) {
- formatsucks++;
+ (*formatsucks)++;
                         continue;
                 }
                 if ((file - line) / 2 != sz) {
- formatsucks++; /* checksum length mismatch */
+ (*formatsucks)++; /* checksum length mismatch */
                         continue;
                 }
                 *file = '\0';
_AT_@ -66,7 +60,7 @@ cryptcheck(char *sumfile, int argc, char *argv[],
                 *p = '\0';
                 if (!(fp = fopen(file, "r"))) {
                         weprintf("fopen %s:", file);
- noread++;
+ (*noread)++;
                         continue;
                 }
                 cryptsum(ops, fp, file, md);
_AT_@ -75,33 +69,53 @@ cryptcheck(char *sumfile, int argc, char *argv[],
                         printf("%s: OK\n", file);
                 } else if (r == 0) {
                         printf("%s: FAILED\n", file);
- nonmatch++;
+ (*nonmatch)++;
                 } else {
- formatsucks++;
+ (*formatsucks)++;
                 }
                 fclose(fp);
         }
- if (sumfile)
- fclose(cfp);
         free(line);
- if (formatsucks > 0) {
+}
+
+int
+cryptcheck(int argc, char *argv[], struct crypt_ops *ops, uint8_t *md, size_t sz)
+{
+ FILE *fp;
+ int formatsucks = 0, noread = 0, nonmatch = 0, ret = 0;
+
+ if (argc == 0) {
+ mdchecklist(stdin, ops, md, sz, &formatsucks, &noread, &nonmatch);
+ } else {
+ for (; argc > 0; argc--, argv++) {
+ if (!(fp = fopen(*argv, "r"))) {
+ weprintf("fopen %s:", *argv);
+ ret = 1;
+ continue;
+ }
+ mdchecklist(fp, ops, md, sz, &formatsucks, &noread, &nonmatch);
+ fclose(fp);
+ }
+ }
+
+ if (formatsucks) {
                 weprintf("%d lines are improperly formatted\n", formatsucks);
                 ret = 1;
         }
- if (noread > 0) {
+ if (noread) {
                 weprintf("%d listed file could not be read\n", noread);
                 ret = 1;
         }
- if (nonmatch > 0) {
+ if (nonmatch) {
                 weprintf("%d computed checksums did NOT match\n", nonmatch);
                 ret = 1;
         }
+
         return ret;
 }
 
 int
-cryptmain(int argc, char *argv[],
- struct crypt_ops *ops, uint8_t *md, size_t sz)
+cryptmain(int argc, char *argv[], struct crypt_ops *ops, uint8_t *md, size_t sz)
 {
         FILE *fp;
         int ret = 0;
_AT_@ -110,7 +124,7 @@ cryptmain(int argc, char *argv[],
                 cryptsum(ops, stdin, "<stdin>", md);
                 mdprint(md, "<stdin>", sz);
         } else {
- for (; argc > 0; argc--) {
+ for (; argc > 0; argc--, argv++) {
                         if (!(fp = fopen(*argv, "r"))) {
                                 weprintf("fopen %s:", *argv);
                                 ret = 1;
_AT_@ -121,7 +135,6 @@ cryptmain(int argc, char *argv[],
                         else
                                 mdprint(md, *argv, sz);
                         fclose(fp);
- argv++;
                 }
         }
         return ret;
diff --git a/md5sum.c b/md5sum.c
index 1618507..5baeecf 100644
--- a/md5sum.c
+++ b/md5sum.c
_AT_@ -24,20 +24,16 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
+ int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
         uint8_t md[MD5_DIGEST_LENGTH];
- char *checkfile = NULL;
- int cflag = 0;
 
         ARGBEGIN {
         case 'c':
- cflag = 1;
- checkfile = ARGF();
+ cryptfunc = cryptcheck;
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (cflag)
- return cryptcheck(checkfile, argc, argv, &md5_ops, md, sizeof(md));
- return cryptmain(argc, argv, &md5_ops, md, sizeof(md));
+ return cryptfunc(argc, argv, &md5_ops, md, sizeof(md));
 }
diff --git a/sha1sum.c b/sha1sum.c
index 54ea33f..b7222fd 100644
--- a/sha1sum.c
+++ b/sha1sum.c
_AT_@ -17,24 +17,22 @@ struct crypt_ops sha1_ops = {
 static void
 usage(void)
 {
- eprintf("usage: %s [-c] [file...]\n", argv0);
+ eprintf("usage: %s [-c] [file ...]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
+ int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
         uint8_t md[SHA1_DIGEST_LENGTH];
- char *checkfile = NULL;
 
         ARGBEGIN {
         case 'c':
- checkfile = ARGF();
+ cryptfunc = cryptcheck;
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (checkfile)
- return cryptcheck(checkfile, argc, argv, &sha1_ops, md, sizeof(md));
- return cryptmain(argc, argv, &sha1_ops, md, sizeof(md));
+ return cryptfunc(argc, argv, &sha1_ops, md, sizeof(md));
 }
diff --git a/sha256sum.c b/sha256sum.c
index 09ef584..06e8832 100644
--- a/sha256sum.c
+++ b/sha256sum.c
_AT_@ -17,24 +17,22 @@ struct crypt_ops sha256_ops = {
 static void
 usage(void)
 {
- eprintf("usage: %s [-c] [file...]\n", argv0);
+ eprintf("usage: %s [-c] [file ...]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
+ int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
         uint8_t md[SHA256_DIGEST_LENGTH];
- char *checkfile = NULL;
 
         ARGBEGIN {
         case 'c':
- checkfile = ARGF();
+ cryptfunc = cryptcheck;
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (checkfile)
- return cryptcheck(checkfile, argc, argv, &sha256_ops, md, sizeof(md));
- return cryptmain(argc, argv, &sha256_ops, md, sizeof(md));
+ return cryptfunc(argc, argv, &sha256_ops, md, sizeof(md));
 }
diff --git a/sha512sum.c b/sha512sum.c
index 0018401..a0fc3cc 100644
--- a/sha512sum.c
+++ b/sha512sum.c
_AT_@ -17,24 +17,22 @@ struct crypt_ops sha512_ops = {
 static void
 usage(void)
 {
- eprintf("usage: %s [-c] [file...]\n", argv0);
+ eprintf("usage: %s [-c] [file ...]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
+ int (*cryptfunc)(int, char **, struct crypt_ops *, uint8_t *, size_t) = cryptmain;
         uint8_t md[SHA512_DIGEST_LENGTH];
- char *checkfile = NULL;
 
         ARGBEGIN {
         case 'c':
- checkfile = ARGF();
+ cryptfunc = cryptcheck;
                 break;
         default:
                 usage();
         } ARGEND;
 
- if (checkfile)
- return cryptcheck(checkfile, argc, argv, &sha512_ops, md, sizeof(md));
- return cryptmain(argc, argv, &sha512_ops, md, sizeof(md));
+ return cryptfunc(argc, argv, &sha512_ops, md, sizeof(md));
 }
Received on Tue Mar 03 2015 - 14:32:25 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 03 2015 - 14:36:10 CET