+
+int html_dump(const void *t, unsigned long index, unsigned long endindex,
+ int maxdepth, const struct html_config *cfg,
+ const char *pathprefix)
+{
+ /*
+ * Determine the filename for this file.
+ */
+ assert(cfg->format != NULL);
+ int prefixlen = strlen(pathprefix);
+ int fnmax = strlen(pathprefix) + strlen(cfg->format) + 100;
+ char filename[fnmax];
+ strcpy(filename, pathprefix);
+ make_filename(filename + prefixlen, fnmax - prefixlen,
+ cfg->format, cfg->rootpage, index);
+
+ /*
+ * Create the HTML itself. Don't write out downlinks from our
+ * deepest level.
+ */
+ char *html = html_query(t, index, cfg, maxdepth != 0);
+
+ /*
+ * Write it out.
+ */
+ FILE *fp = fopen(filename, "w");
+ if (!fp) {
+ fprintf(stderr, "%s: %s: open: %s\n", PNAME,
+ filename, strerror(errno));
+ return 1;
+ }
+ if (fputs(html, fp) < 0) {
+ fprintf(stderr, "%s: %s: write: %s\n", PNAME,
+ filename, strerror(errno));
+ fclose(fp);
+ return 1;
+ }
+ if (fclose(fp) < 0) {
+ fprintf(stderr, "%s: %s: fclose: %s\n", PNAME,
+ filename, strerror(errno));
+ return 1;
+ }
+
+ /*
+ * Recurse.
+ */
+ if (maxdepth != 0) {
+ unsigned long subindex, subendindex;
+ int newdepth = (maxdepth > 0 ? maxdepth - 1 : maxdepth);
+ char path[1+trie_maxpathlen(t)];
+
+ index++;
+ while (index < endindex) {
+ trie_getpath(t, index, path);
+ get_indices(t, path, &subindex, &subendindex);
+ index = subendindex;
+ if (subendindex - subindex > 1) {
+ if (html_dump(t, subindex, subendindex, newdepth,
+ cfg, pathprefix))
+ return 1;
+ }
+ }
+ }
+ return 0;
+}