Cleanups.
[u/mdw/catacomb] / hashsum.c
index 8419779..8a14a76 100644 (file)
--- a/hashsum.c
+++ b/hashsum.c
 
 /*----- Guts --------------------------------------------------------------*/
 
+static int checkjunk(const char *path, const struct stat *st, void *p)
+{
+  const char *what;
+  fhashstate *fh = p;
+
+  if (!st) {
+    if (fh->f & f_verbose)
+      fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
+    else
+      moan("couldn't stat junk file `%s': %s", path, strerror(errno));
+  } else {
+    what = describefile(st);
+    if (fh->f & f_verbose)
+      fprintf(stderr, "JUNK %s %s\n", what, path);
+    else
+      moan("found junk %s `%s'", what, path);
+  }
+  return (0);
+}
+
+static int warnjunk(const char *path, const struct stat *st, void *p)
+{
+  if (st)
+    moan("unexpected %s `%s'", describefile(st), path);
+  else
+    moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
+  return (0);
+}
+
 static int checkhash(fhashstate *fh, const char *file, const encodeops *e)
 {
   int rc;
@@ -277,13 +306,14 @@ int main(int argc, char *argv[])
 
       { "escape",      0,              0,      'e' },
       { "check",       0,              0,      'c' },
+      { "junk",                0,              0,      'j' },
       { "binary",      0,              0,      'b' },
       { "verbose",     0,              0,      'v' },
       { "progress",    0,              0,      'p' },
 
       { 0,             0,              0,      0 }
     };
-    int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0);
+    int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
     if (i < 0)
       break;
 
@@ -321,6 +351,9 @@ int main(int argc, char *argv[])
       case 'c':
        fh.f |= f_check;
        break;
+      case 'j':
+       fh.f |= FHF_JUNK;
+       break;
       case 'b':
        fh.f |= FHF_BINARY;
        break;
@@ -363,6 +396,15 @@ int main(int argc, char *argv[])
     }
   }
 
+  if (fh.f & FHF_JUNK) {
+    if (fh.f & f_check) {
+      if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
+    } else {
+      if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
+    }
+  }
+  fhash_free(&fh);
+
   return (rc);
 }