+ path = argv[0];
+ if(argc > 1) {
+ /* Explicit size specified */
+ errno = 0;
+ char *end;
+ size = strtoll(argv[1], &end, 10);
+ if(errno)
+ fatal(errno, "invalid size");
+ if(end == argv[1])
+ fatal(0, "invalid size");
+ if(*end) {
+ if(end[1])
+ fatal(0, "invalid scale");
+ scale(*end, size);
+ }
+ } else if(entireopt) {
+ /* Use stupidly large size as a proxy for 'infinite' */
+ size = LLONG_MAX;
+ } else {
+ /* Retrieve size from target (which must exist) */
+ struct stat sb;
+ if(stat(path, &sb) < 0)
+ fatal(errno, "stat %s", path);
+ size = sb.st_size;
+ }
+ const char *show = entireopt ? (mode == CREATE ? "written" : "verified") : 0;
+ if(mode == BOTH) {
+ size = execute(CREATE, entireopt, 0);
+ execute(VERIFY, false, show);
+ } else {
+ execute(mode, entireopt, show);
+ }
+ return 0;
+}
+
+// flush stdout, fatal on error
+static void flushstdout() {
+ if(ferror(stdout) || fflush(stdout))
+ fatal(errno, "flush stdout");
+}
+
+// clear the progress indicator
+static void clearprogress() {
+ if (!progress) return;
+ printf(" %-10s %*s \r", "", (int)sizeof(long long)*4, "");
+ flushstdout();
+}
+
+// update progress indicator
+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();
+}
+
+// write/verify the target file
+static long long execute(mode_type mode, bool entire, const char *show) {
+ Arcfour rng((const uint8_t *)seed, seedlen);