+
+ /* Display in units of 1Kb */
+ fprintf(fp, "%-11llu %s\n", (size) / 1024, querydir);
+}
+
+/*
+ * Largely frivolous way to define all my command-line options. I
+ * present here a parametric macro which declares a series of
+ * _logical_ option identifiers, and for each one declares zero or
+ * more short option characters and zero or more long option
+ * words. Then I repeatedly invoke that macro with its arguments
+ * defined to be various other macros, which allows me to
+ * variously:
+ *
+ * - define an enum allocating a distinct integer value to each
+ * logical option id
+ * - define a string consisting of precisely all the short option
+ * characters
+ * - define a string array consisting of all the long option
+ * strings
+ * - define (with help from auxiliary enums) integer arrays
+ * parallel to both of the above giving the logical option id
+ * for each physical short and long option
+ * - define an array indexed by logical option id indicating
+ * whether the option in question takes a value
+ * - define a function which prints out brief online help for all
+ * the options.
+ *
+ * It's not at all clear to me that this trickery is actually
+ * particularly _efficient_ - it still, after all, requires going
+ * linearly through the option list at run time and doing a
+ * strcmp, whereas in an ideal world I'd have liked the lists of
+ * long and short options to be pre-sorted so that a binary search
+ * or some other more efficient lookup was possible. (Not that
+ * asymptotic algorithmic complexity is remotely vital in option
+ * parsing, but if I were doing this in, say, Lisp or something
+ * with an equivalently powerful preprocessor then once I'd had
+ * the idea of preparing the option-parsing data structures at
+ * compile time I would probably have made the effort to prepare
+ * them _properly_. I could have Perl generate me a source file
+ * from some sort of description, I suppose, but that would seem
+ * like overkill. And in any case, it's more of a challenge to
+ * achieve as much as possible by cunning use of cpp and enum than
+ * to just write some sensible and logical code in a Turing-
+ * complete language. I said it was largely frivolous :-)
+ *
+ * This approach does have the virtue that it brings together the
+ * option ids, option spellings and help text into a single
+ * combined list and defines them all in exactly one place. If I
+ * want to add a new option, or a new spelling for an option, I
+ * only have to modify the main OPTHELP macro below and then add
+ * code to process the new logical id.
+ *
+ * (Though, really, even that isn't ideal, since it still involves
+ * modifying the source file in more than one place. In a
+ * _properly_ ideal world, I'd be able to interleave the option
+ * definitions with the code fragments that process them. And then
+ * not bother defining logical identifiers for them at all - those
+ * would be automatically generated, since I wouldn't have any
+ * need to specify them manually in another part of the code.)
+ *
+ * One other helpful consequence of the enum-based structure here
+ * is that it causes a compiler error if I accidentally try to
+ * define the same option (short or long) twice.
+ */
+
+#define OPTHELP(NOVAL, VAL, SHORT, LONG, HELPPFX, HELPARG, HELPLINE, HELPOPT) \
+ HELPPFX("usage") HELPLINE(PNAME " [options] action [action...]") \
+ HELPPFX("actions") \
+ VAL(SCAN) SHORT(s) LONG(scan) \
+ HELPARG("directory") HELPOPT("scan and index a directory") \
+ NOVAL(HTTPD) SHORT(w) LONG(web) LONG(server) LONG(httpd) \
+ HELPOPT("serve HTML reports from a temporary web server") \
+ VAL(TEXT) SHORT(t) LONG(text) \
+ HELPARG("subdir") HELPOPT("print a plain text report on a subdirectory") \
+ NOVAL(REMOVE) SHORT(R) LONG(remove) LONG(delete) LONG(unlink) \
+ HELPOPT("remove the index file") \
+ NOVAL(DUMP) SHORT(D) LONG(dump) HELPOPT("dump the index file on stdout") \
+ NOVAL(LOAD) SHORT(L) LONG(load) \
+ HELPOPT("load and index a dump file") \
+ VAL(SCANDUMP) SHORT(S) LONG(scan_dump) \
+ HELPARG("directory") HELPOPT("scan only, generating a dump") \
+ VAL(HTML) SHORT(H) LONG(html) \
+ HELPARG("subdir") HELPOPT("print an HTML report on a subdirectory") \
+ NOVAL(CGI) LONG(cgi) \
+ HELPOPT("do the right thing when run from a CGI script") \
+ HELPPFX("options") \
+ VAL(DATAFILE) SHORT(f) LONG(file) \
+ HELPARG("filename") HELPOPT("[most modes] specify index file") \
+ NOVAL(CROSSFS) LONG(cross_fs) \
+ HELPOPT("[--scan] cross filesystem boundaries") \
+ NOVAL(NOCROSSFS) LONG(no_cross_fs) \
+ HELPOPT("[--scan] stick to one filesystem") \
+ VAL(PRUNE) LONG(prune) \
+ HELPARG("wildcard") HELPOPT("[--scan] prune files matching pattern") \
+ VAL(PRUNEPATH) LONG(prune_path) \
+ HELPARG("wildcard") HELPOPT("[--scan] prune pathnames matching pattern") \
+ VAL(EXCLUDE) LONG(exclude) \
+ HELPARG("wildcard") HELPOPT("[--scan] exclude files matching pattern") \
+ VAL(EXCLUDEPATH) LONG(exclude_path) \
+ HELPARG("wildcard") HELPOPT("[--scan] exclude pathnames matching pattern") \
+ VAL(INCLUDE) LONG(include) \
+ HELPARG("wildcard") HELPOPT("[--scan] include files matching pattern") \
+ VAL(INCLUDEPATH) LONG(include_path) \
+ HELPARG("wildcard") HELPOPT("[--scan] include pathnames matching pattern") \
+ NOVAL(PROGRESS) LONG(progress) LONG(scan_progress) \
+ HELPOPT("[--scan] report progress on stderr") \
+ NOVAL(NOPROGRESS) LONG(no_progress) LONG(no_scan_progress) \
+ HELPOPT("[--scan] do not report progress") \
+ NOVAL(TTYPROGRESS) LONG(tty_progress) LONG(tty_scan_progress) \
+ LONG(progress_tty) LONG(scan_progress_tty) \
+ HELPOPT("[--scan] report progress if stderr is a tty") \
+ NOVAL(DIRATIME) LONG(dir_atime) LONG(dir_atimes) \
+ HELPOPT("[--scan,--load] keep real atimes on directories") \
+ NOVAL(NODIRATIME) LONG(no_dir_atime) LONG(no_dir_atimes) \
+ HELPOPT("[--scan,--load] fake atimes on directories") \
+ NOVAL(NOEOF) LONG(no_eof) LONG(noeof) \
+ HELPOPT("[--web] do not close web server on EOF") \
+ NOVAL(MTIME) LONG(mtime) \
+ HELPOPT("[--scan] use mtime instead of atime") \
+ NOVAL(SHOWFILES) LONG(files) \
+ HELPOPT("[--web,--html,--text] list individual files") \
+ VAL(AGERANGE) SHORT(r) LONG(age_range) LONG(range) LONG(ages) \
+ HELPARG("age[-age]") HELPOPT("[--web,--html] set limits of colour coding") \
+ VAL(OUTFILE) SHORT(o) LONG(output) \
+ HELPARG("filename") HELPOPT("[--html] specify output file or directory name") \
+ VAL(SERVERADDR) LONG(address) LONG(addr) LONG(server_address) \
+ LONG(server_addr) \
+ HELPARG("addr[:port]") HELPOPT("[--web] specify HTTP server address") \
+ VAL(AUTH) LONG(auth) LONG(http_auth) LONG(httpd_auth) \
+ LONG(server_auth) LONG(web_auth) \
+ HELPARG("type") HELPOPT("[--web] specify HTTP authentication method") \
+ VAL(AUTHFILE) LONG(auth_file) \
+ HELPARG("filename") HELPOPT("[--web] read HTTP Basic user/pass from file") \
+ VAL(AUTHFD) LONG(auth_fd) \
+ HELPARG("fd") HELPOPT("[--web] read HTTP Basic user/pass from fd") \
+ VAL(HTMLTITLE) LONG(title) \
+ HELPARG("title") HELPOPT("[--web,--html] title prefix for web pages") \
+ VAL(DEPTH) SHORT(d) LONG(depth) LONG(max_depth) LONG(maximum_depth) \
+ HELPARG("levels") HELPOPT("[--text,--html] recurse to this many levels") \
+ VAL(MINAGE) SHORT(a) LONG(age) LONG(min_age) LONG(minimum_age) \
+ HELPARG("age") HELPOPT("[--text] include only files older than this") \
+ HELPPFX("also") \
+ NOVAL(HELP) SHORT(h) LONG(help) HELPOPT("display this help text") \
+ NOVAL(VERSION) SHORT(V) LONG(version) HELPOPT("report version number") \
+ NOVAL(LICENCE) LONG(licence) LONG(license) \
+ HELPOPT("display (MIT) licence text") \
+
+#define IGNORE(x)
+#define DEFENUM(x) OPT_ ## x,
+#define ZERO(x) 0,
+#define ONE(x) 1,
+#define STRING(x) #x ,
+#define STRINGNOCOMMA(x) #x
+#define SHORTNEWOPT(x) SHORTtmp_ ## x = OPT_ ## x,
+#define SHORTTHISOPT(x) SHORTtmp2_ ## x, SHORTVAL_ ## x = SHORTtmp2_ ## x - 1,
+#define SHORTOPTVAL(x) SHORTVAL_ ## x,
+#define SHORTTMP(x) SHORTtmp3_ ## x,
+#define LONGNEWOPT(x) LONGtmp_ ## x = OPT_ ## x,
+#define LONGTHISOPT(x) LONGtmp2_ ## x, LONGVAL_ ## x = LONGtmp2_ ## x - 1,
+#define LONGOPTVAL(x) LONGVAL_ ## x,
+#define LONGTMP(x) SHORTtmp3_ ## x,
+
+#define OPTIONS(NOVAL, VAL, SHORT, LONG) \
+ OPTHELP(NOVAL, VAL, SHORT, LONG, IGNORE, IGNORE, IGNORE, IGNORE)
+
+enum { OPTIONS(DEFENUM,DEFENUM,IGNORE,IGNORE) NOPTIONS };
+enum { OPTIONS(IGNORE,IGNORE,SHORTTMP,IGNORE) NSHORTOPTS };
+enum { OPTIONS(IGNORE,IGNORE,IGNORE,LONGTMP) NLONGOPTS };
+static const int opthasval[NOPTIONS] = {OPTIONS(ZERO,ONE,IGNORE,IGNORE)};
+static const char shortopts[] = {OPTIONS(IGNORE,IGNORE,STRINGNOCOMMA,IGNORE)};
+static const char *const longopts[] = {OPTIONS(IGNORE,IGNORE,IGNORE,STRING)};
+enum { OPTIONS(SHORTNEWOPT,SHORTNEWOPT,SHORTTHISOPT,IGNORE) UNUSEDENUMVAL1 };
+enum { OPTIONS(LONGNEWOPT,LONGNEWOPT,IGNORE,LONGTHISOPT) UNUSEDENUMVAL2 };
+static const int shortvals[] = {OPTIONS(IGNORE,IGNORE,SHORTOPTVAL,IGNORE)};
+static const int longvals[] = {OPTIONS(IGNORE,IGNORE,IGNORE,LONGOPTVAL)};
+
+static void usage(FILE *fp)
+{
+ char longbuf[80];
+ const char *prefix, *shortopt, *longopt, *optarg;
+ int i, optex;
+
+#define HELPRESET prefix = shortopt = longopt = optarg = NULL, optex = -1
+#define HELPNOVAL(s) optex = 0;
+#define HELPVAL(s) optex = 1;
+#define HELPSHORT(s) if (!shortopt) shortopt = "-" #s;
+#define HELPLONG(s) if (!longopt) { \
+ strcpy(longbuf, "--" #s); longopt = longbuf; \
+ for (i = 0; longbuf[i]; i++) if (longbuf[i] == '_') longbuf[i] = '-'; }
+#define HELPPFX(s) prefix = s;
+#define HELPARG(s) optarg = s;
+#define HELPLINE(s) assert(optex == -1); \
+ fprintf(fp, "%7s%c %s\n", prefix?prefix:"", prefix?':':' ', s); \
+ HELPRESET;
+#define HELPOPT(s) assert((optex == 1 && optarg) || (optex == 0 && !optarg)); \
+ assert(shortopt || longopt); \
+ i = fprintf(fp, "%7s%c %s%s%s%s%s", prefix?prefix:"", prefix?':':' ', \
+ shortopt?shortopt:"", shortopt&&longopt?", ":"", longopt?longopt:"", \
+ optarg?" ":"", optarg?optarg:""); \
+ fprintf(fp, "%*s %s\n", i<32?32-i:0,"",s); HELPRESET;
+
+ HELPRESET;
+ OPTHELP(HELPNOVAL, HELPVAL, HELPSHORT, HELPLONG,
+ HELPPFX, HELPARG, HELPLINE, HELPOPT);
+
+#undef HELPRESET
+#undef HELPNOVAL
+#undef HELPVAL
+#undef HELPSHORT
+#undef HELPLONG
+#undef HELPPFX
+#undef HELPARG
+#undef HELPLINE
+#undef HELPOPT
+}
+
+static time_t parse_age(time_t now, const char *agestr)
+{
+ time_t t;
+ struct tm tm;
+ int nunits;
+ char unit[2];
+
+ t = now;
+
+ if (2 != sscanf(agestr, "%d%1[DdWwMmYy]", &nunits, unit)) {
+ fprintf(stderr, "%s: age specification should be a number followed by"
+ " one of d,w,m,y\n", PNAME);
+ exit(1);
+ }
+
+ if (unit[0] == 'd') {
+ t -= 86400 * nunits;
+ } else if (unit[0] == 'w') {
+ t -= 86400 * 7 * nunits;
+ } else {
+ int ym;
+
+ tm = *localtime(&t);
+ ym = tm.tm_year * 12 + tm.tm_mon;
+
+ if (unit[0] == 'm')
+ ym -= nunits;
+ else
+ ym -= 12 * nunits;
+
+ tm.tm_year = ym / 12;
+ tm.tm_mon = ym % 12;
+
+ t = mktime(&tm);
+ }
+
+ return t;