+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();
+}
+