chiark / gitweb /
Typo fixes to man page.
[vbig.git] / vbig.cc
diff --git a/vbig.cc b/vbig.cc
index f4f6217e5a28aee60d3bd8516c96f3ce84ad8a6f..a243fb7262602703b2eeed2038866778039c9f19 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -1,6 +1,7 @@
 /*
  * This file is part of vbig.
- * Copyright (C) 2011 Richard Kettlewell
+ * Copyright (C) 2011, 2013 Richard Kettlewell
+ * Copyright (C) 2013 Ian Jackson
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <sys/stat.h>
 #include "Arcfour.h"
 
+#define DEFAULT_SEED_LENGTH 2048;
+
 // Command line options
 const struct option opts[] = {
   { "seed", required_argument, 0, 's' },
+  { "seed-file", required_argument, 0, 'S' },
+  { "seed-length", required_argument, 0, 'L' },
   { "both", no_argument, 0, 'b' },
   { "verify", no_argument, 0, 'v' },
   { "create", no_argument, 0, 'c' },
   { "flush", no_argument, 0, 'f' },
   { "entire", no_argument, 0, 'e' },
+  { "progress", no_argument, 0, 'p' },
   { "help", no_argument, 0, 'h' },
   { "version", no_argument, 0, 'V' },
   { 0, 0, 0, 0 },
@@ -47,16 +53,20 @@ static void help(void) {
   printf("vbig - create or verify a large but pseudo-random file\n"
          "\n"
          "Usage:\n"
-         "  vbig [--seed SEED] --verify|--create PATH [SIZE]\n"
+         "  vbig [OPTIONS] [--both|--verify|--create] PATH] [SIZE]\n"
          "\n"
          "Options:\n"
-         "  --seed, -s     Specify random seed\n"
-         "  --verify, -v   Verify that PATH contains the expected contents\n"
-         "  --create, -c   Create PATH with psuedo-random contents\n"
-         "  --flush, -f    Flush cache\n"
-         "  --entire, -e   Write until full; read until EOF\n"
-         "  --help, -h     Display usage message\n"
-         "  --version, -V  Display version string\n");
+         "  --seed, -s        Specify random seed as string\n"
+         "  --seed-file, -S   Read random seed from (start of) this file\n"
+         "  --seed-length, -L Set (maximum) seed length to read from file\n"
+         "  --verify, -v      Verify that PATH contains the expected contents\n"
+         "  --create, -c      Create PATH with psuedo-random contents\n"
+         "  --both, -b        Do both create and verify (default)\n"
+         "  --flush, -f       Flush cache\n"
+         "  --entire, -e      Write until full; read until EOF\n"
+         "  --progress, -p    Show progress as we go\n"
+         "  --help, -h        Display usage message\n"
+         "  --version, -V     Display version string\n");
 }
 
 // Possible modes of operation
@@ -66,9 +76,12 @@ enum mode_type {
   BOTH
 };
 
+static void clearprogress();
+
 // Report an error and exit
 static void fatal(int errno_value, const char *fmt, ...) {
   va_list ap;
+  clearprogress();
   fprintf(stderr, "ERROR: ");
   va_start(ap, fmt);
   vfprintf(stderr, fmt, ap);
@@ -111,23 +124,32 @@ static void flushCache(FILE *fp) {
 static long long execute(mode_type mode, bool entire, const char *show);
 
 static const char default_seed[] = "hexapodia as the key insight";
-static const void *seed;
+static void *seed;
 static size_t seedlen;
+static const char *seedpath;
 static const char *path;
 static bool entireopt = false;
 static bool flush = false;
+static bool progress = false;
 static long long size;
 
 int main(int argc, char **argv) {
   mode_type mode = BOTH;
   int n;
-  while((n = getopt_long(argc, argv, "+s:vcefhV", opts, 0)) >= 0) {
+  char *ep;
+  while((n = getopt_long(argc, argv, "+s:S:L:vcepfhV", opts, 0)) >= 0) {
     switch(n) {
     case 's': seed = optarg; seedlen = strlen(optarg); break;
+    case 'S': seedpath = optarg; break;
+    case 'L':
+      seedlen = strtoul(optarg,&ep,0);
+      if(ep==optarg || *ep) fatal(0, "bad number for -S");
+      break;
     case 'b': mode = BOTH; break;
     case 'v': mode = VERIFY; break;
     case 'c': mode = CREATE; break;
     case 'e': entireopt = true; break;
+    case 'p': progress = true; break;
     case 'f': flush = true; break;
     case 'h': help(); exit(0);
     case 'V': puts(VERSION); exit(0);
@@ -148,8 +170,32 @@ int main(int argc, char **argv) {
     if(argc < (mode == VERIFY ? 1 : 2))
       fatal(0, "insufficient arguments");
   }
+  if(seed && seedpath)
+    fatal(0, "both --seed and --seed-file specified");
+  if(mode == BOTH && !seed && !seedpath) {
+#ifdef HAVE_RANDOM_DEVICE
+    seedpath = RANDOM_DEVICE;
+#else
+    fatal(0, "no --seed or --seed-file specified in --both mode"
+         " and random device not supported on this system");
+#endif
+  }
+  if(seedpath) {
+    if(!seedlen)
+      seedlen = DEFAULT_SEED_LENGTH;
+    FILE *seedfile = fopen(seedpath, "rb");
+    if(!seedfile)
+      fatal(errno, "%s", seedpath);
+    seed = malloc(seedlen);
+    if(!seed)
+      fatal(errno, "allocate seed");
+    seedlen = fread(seed, 1, seedlen, seedfile);
+    if(ferror(seedfile))
+      fatal(errno, "read %s", seedpath);
+    fclose(seedfile);
+  }
   if (!seed) {
-    seed = default_seed;
+    seed = (void*)default_seed;
     seedlen = sizeof(default_seed)-1;
   }
   path = argv[0];
@@ -170,7 +216,7 @@ int main(int argc, char **argv) {
     else if(*end)
       fatal(0, "invalid size");
   } else if(entireopt) {
-    size = LONG_LONG_MAX;
+    size = LLONG_MAX;
   } else {
     struct stat sb;
     if(stat(path, &sb) < 0)
@@ -187,6 +233,37 @@ int main(int argc, char **argv) {
   return 0;
 }
 
+static void flushstdout() {
+  if(ferror(stdout) || fflush(stdout))
+    fatal(errno, "flush stdout");
+}
+
+static void clearprogress() {
+  if (!progress) return;
+  printf(" %-10s %*s   \r", "", (int)sizeof(long long)*4, "");
+  flushstdout();
+}
+
+static void showprogress(long long amount, const char *show) {
+  if (!progress) return;
+
+  static int counter;
+  if (counter++ < 1000) return;
+  counter = 0;
+
+  int triples = sizeof(amount);
+  char rawbuf[triples*3 + 1];
+  char outbuf[triples*4 + 1];
+  snprintf(rawbuf, sizeof(rawbuf), "% *lld", (int)sizeof(rawbuf)-1, amount);
+  for (int i=0; i<triples; i++) {
+    outbuf[i*4] = ' ';
+    memcpy(outbuf + i*4 + 1, rawbuf + i*3, 3);
+  }
+  outbuf[triples*4] = 0;
+  printf(" %-10s %s...\r", outbuf, show);
+  flushstdout();
+}
+
 static long long execute(mode_type mode, bool entire, const char *show) {
   Arcfour rng((const char*)seed, seedlen);
   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
@@ -237,7 +314,9 @@ static long long execute(mode_type mode, bool entire, const char *show) {
       }
     }
     remain -= bytesGenerated;
+    showprogress(size - remain, mode == VERIFY ? "verifying" : "writing");
   }
+  clearprogress();
   if(mode == VERIFY && !entire && getc(fp) != EOF)
     fatal(0, "%s: extended beyond %lld bytes",
             path, size);
@@ -253,8 +332,7 @@ static long long execute(mode_type mode, bool entire, const char *show) {
     printf("%lld bytes (%lldM, %lldG) %s\n",
           done, done >> 20, done >> 30,
           show);
-    if(ferror(stdout) || fflush(stdout))
-      fatal(errno, "flush stdout");
+    flushstdout();
   }
   return done;
 }