Change the magic number used to introduce a trie file, so that instead
[sgt/agedu] / agedu.c
diff --git a/agedu.c b/agedu.c
index fd8aaea..1847e2c 100644 (file)
--- a/agedu.c
+++ b/agedu.c
@@ -361,6 +361,8 @@ static void text_query(const void *mappedfile, const char *querydir,
         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) \
@@ -500,10 +502,11 @@ int main(int argc, char **argv)
     time_t now = time(NULL);
     time_t textcutoff = now, htmlnewest = now, htmloldest = now;
     int htmlautoagerange = 1;
-    const char *httpserveraddr = NULL;
-    int httpserverport = 0;
+    const char *httpserveraddr = "localhost";
+    const char *httpserverport = NULL;
     const char *httpauthdata = NULL;
     const char *outfile = NULL;
+    const char *html_title = PNAME;
     int auth = HTTPD_AUTH_MAGIC | HTTPD_AUTH_BASIC;
     int progress = 1;
     struct inclusion_exclusion *inex = NULL;
@@ -791,6 +794,9 @@ int main(int argc, char **argv)
                  case OPT_OUTFILE:
                    outfile = optval;
                    break;
+                  case OPT_HTMLTITLE:
+                    html_title = optval;
+                    break;
                  case OPT_MINAGE:
                    textcutoff = parse_age(now, optval);
                    break;
@@ -815,10 +821,13 @@ int main(int argc, char **argv)
                        else
                            port = optval;
                        port += strcspn(port, ":");
-                       if (port)
+                       if (port && *port)
                            *port++ = '\0';
-                       httpserveraddr = optval;
-                       httpserverport = atoi(port);
+                        if (!strcmp(optval, "ANY"))
+                            httpserveraddr = NULL;
+                        else
+                            httpserveraddr = optval;
+                       httpserverport = port;
                    }
                    break;
                  case OPT_AUTH:
@@ -1137,7 +1146,6 @@ int main(int argc, char **argv)
                prevbuf[0] = '\0';
                tf = triewalk_next(tw, buf);
                assert(tf);
-               prevtf = NULL;         /* placate lint */
                while (1) {
                    int i;
 
@@ -1172,9 +1180,6 @@ int main(int argc, char **argv)
                        triewalk_rebase(tw, mappedfile);
                        diff = (const unsigned char *)mappedfile -
                            (const unsigned char *)oldfile;
-                       if (prevtf)
-                           prevtf = (const struct trie_file *)
-                               (((const unsigned char *)prevtf) + diff);
                        if (tf)
                            tf = (const struct trie_file *)
                                (((const unsigned char *)tf) + diff);
@@ -1241,7 +1246,8 @@ int main(int argc, char **argv)
                indexbuild_free(ib);
 
                munmap(mappedfile, totalsize);
-               ftruncate(fd, realsize);
+               if (ftruncate(fd, realsize) < 0)
+                    fatal("%s: truncate: %s\n", filename, strerror(errno));
                close(fd);
                printf("Final index file size = %llu bytes\n",
                       (unsigned long long)realsize);
@@ -1266,6 +1272,11 @@ int main(int argc, char **argv)
                perror(PNAME ": mmap");
                return 1;
            }
+            if (!trie_check_magic(mappedfile)) {
+               fprintf(stderr, "%s: %s: magic numbers did not match\n"
+                        "%s: check that the index was built by this version of agedu on this platform\n", PNAME, filename, PNAME);
+               return 1;
+            }
            pathsep = trie_pathsep(mappedfile);
 
            /*
@@ -1349,6 +1360,21 @@ int main(int argc, char **argv)
                }
                return 1;
            }
+           if (!trie_check_magic(mappedfile)) {
+               fprintf(stderr, "%s: %s: magic numbers did not match\n"
+                        "%s: check that the index was built by this version of agedu on this platform\n", PNAME, filename, PNAME);
+               if (!querydir) {
+                   printf("Status: 500\nContent-type: text/html\n\n"
+                          "<html><head>"
+                          "<title>500 Internal Server Error</title>"
+                          "</head><body>"
+                          "<h1>500 Internal Server Error</h1>"
+                          "<p><code>agedu</code> suffered an internal error."
+                          "</body></html>\n");
+                   return 0;
+               }
+               return 1;
+           }
            pathsep = trie_pathsep(mappedfile);
 
            maxpathlen = trie_maxpathlen(mappedfile);
@@ -1375,6 +1401,7 @@ int main(int argc, char **argv)
                cfg.newest = htmlnewest;
                cfg.showfiles = showfiles;
            }
+            cfg.html_title = html_title;
 
            if (!querydir) {
                /*
@@ -1550,6 +1577,11 @@ int main(int argc, char **argv)
                perror(PNAME ": mmap");
                return 1;
            }
+            if (!trie_check_magic(mappedfile)) {
+               fprintf(stderr, "%s: %s: magic numbers did not match\n"
+                        "%s: check that the index was built by this version of agedu on this platform\n", PNAME, filename, PNAME);
+               return 1;
+            }
            pathsep = trie_pathsep(mappedfile);
 
            maxpathlen = trie_maxpathlen(mappedfile);
@@ -1582,6 +1614,11 @@ int main(int argc, char **argv)
                perror(PNAME ": mmap");
                return 1;
            }
+            if (!trie_check_magic(mappedfile)) {
+               fprintf(stderr, "%s: %s: magic numbers did not match\n"
+                        "%s: check that the index was built by this version of agedu on this platform\n", PNAME, filename, PNAME);
+               return 1;
+            }
            pathsep = trie_pathsep(mappedfile);
 
            dcfg.address = httpserveraddr;
@@ -1593,6 +1630,7 @@ int main(int argc, char **argv)
            pcfg.oldest = htmloldest;
            pcfg.newest = htmlnewest;
            pcfg.showfiles = showfiles;
+            pcfg.html_title = html_title;
            run_httpd(mappedfile, auth, &dcfg, &pcfg);
            munmap(mappedfile, totalsize);
        } else if (mode == REMOVE) {