Portability enhancements to make better use of autoconf. All system
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 5 Nov 2008 21:15:37 +0000 (21:15 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 5 Nov 2008 21:15:37 +0000 (21:15 +0000)
#includes (apart from the Linux kernel syscall ickery in du.c) have
been centralised into agedu.h and been made conditional on the
relevant header files being shown to exist by configure. My hacky
personal config.h has been removed and the static development
Makefile now runs the real configure script when necessary. There
are a couple of other tweaks which work around buggy printfs on
Solaris.

git-svn-id: svn://svn.tartarus.org/sgt/agedu@8281 cda61777-01e9-0310-a592-d414129be87e

13 files changed:
GNUmakefile
TODO
agedu.c
agedu.h
alloc.c
config.h [deleted file]
configure.ac
du.c
fgetline.c
html.c
httpd.c
index.c
trie.c

index a5c01fb..cd7229e 100644 (file)
@@ -20,11 +20,23 @@ ALLMODULES := $(sort $(AGEDU_MODULES))
 ALLOBJS := $(patsubst %,%.o,$(ALLMODULES))
 ALLDEPS := $(patsubst %,%.d,$(ALLMODULES))
 
-binaries: agedu
+BINARIES = agedu
 
-agedu: $(AGEDU_OBJS)
+binaries: $(BINARIES)
+
+agedu: config.h $(AGEDU_OBJS)
        gcc $(LFLAGS) -o agedu $(AGEDU_OBJS)
 
+config.h: configure
+       ./configure
+       rm -f Makefile # we keep using _this_ Makefile
+
+configure: configure.ac
+       aclocal
+       autoconf
+       autoheader
+       automake -a --foreign
+
 INTERNALFLAGS=#
 
 $(ALLOBJS): %.o: %.c
@@ -38,6 +50,12 @@ $(MANPAGES): %.1: %.but
        halibut --man=$*.1 $*.but
 
 clean:
-       rm -f agedu $(ALLOBJS) $(ALLDEPS)
+       rm -f $(ALLOBJS) $(ALLDEPS) $(MANPAGES) $(BINARIES)
+
+spotless: clean
+       rm -f config.h config.h.in config.log config.status configure
+       rm -f depcomp install-sh missing stamp-h1
+       rm -f Makefile.in aclocal.m4
+       rm -rf autom4te.cache .deps
 
 -include $(ALLDEPS)
diff --git a/TODO b/TODO
index 0cd19f8..f34f507 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,13 +1,44 @@
 TODO list for agedu
 ===================
 
-Future possibilities:
+ - adjust the default web server address selection.
+    + some systems (e.g. OS X) don't like us binding to random
+      localhost addresses. So if that fails, try falling back to
+      127.0.0.1 proper (and a randomly selected port) before giving
+      up.
+    + since binding to port 80 isn't generally feasible, we should
+      adjust the default behaviour when the user specifies --addr
+      with no port: it should select port zero, and then print the
+      port number on standard output. (Possibly also print the URL
+      as usual, in that situation: translate INADDR_ANY to
+      INADDR_LOOPBACK and then do the same as when we made the
+      entire address up ourself.)
 
- - integrate more usefully with the output of configure. It's
-   generating oodles of automatic boilerplate in config.h and I'm
-   sure three quarters of it _ought_ to be usable to add
-   portability, if only I had the gumption to actually pay attention
-   to all those HAVE_FOO macros it's defined for me.
+ - we should munmap in all operating modes where we mmapped,
+   otherwise chaining them will run out of address space
+
+ - we could still be using more of the information coming from
+   autoconf. Our config.h is defining a whole bunch of HAVE_FOOs for
+   particular functions (e.g. HAVE_INET_NTOA, HAVE_MEMCHR,
+   HAVE_FNMATCH). We could usefully supply alternatives for some of
+   these functions (e.g. cannibalise the PuTTY wildcard matcher for
+   use in the absence of fnmatch, switch to vanilla truncate() in
+   the absence of ftruncate); where we don't have alternative code,
+   it would perhaps be polite to throw an error at configure time
+   rather than allowing the subsequent build to fail.
+    + however, I don't see anything here that looks very
+      controversial; IIRC it's all in POSIX, for one thing. So more
+      likely this should simply wait until somebody complains.
+
+ - it would be useful to support a choice of indexing strategies.
+   The current system's tradeoff of taking O(N log N) space in order
+   to be able to support any age cutoff you like is not going to be
+   ideal for everybody. A second more conventional mechanism which
+   allows the user to specify a number of fixed cutoffs and just
+   indexes each directory on those alone would undoubtedly be a
+   useful thing for large-scale users. This will require
+   considerable thought about how to make the indexers pluggable at
+   both index-generation time and query time.
 
  - IPv6 support in the HTTP server
     * of course, Linux magic auth can still work in this context; we
diff --git a/agedu.c b/agedu.c
index c3ac6a7..1a68eb4 100644 (file)
--- a/agedu.c
+++ b/agedu.c
@@ -2,25 +2,8 @@
  * Main program for agedu.
  */
 
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-#include <time.h>
-#include <assert.h>
-
-#include <unistd.h>
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <termios.h>
-#include <sys/ioctl.h>
-#include <fnmatch.h>
-
 #include "agedu.h"
+
 #include "du.h"
 #include "trie.h"
 #include "index.h"
@@ -1033,8 +1016,8 @@ int main(int argc, char **argv)
                    return 1;
                }
 
-               printf("Built pathname index, %d entries, %ju bytes\n", count,
-                      (intmax_t)st.st_size);
+               printf("Built pathname index, %d entries, %llu bytes\n", count,
+                      (unsigned long long)st.st_size);
 
                totalsize = index_compute_size(st.st_size, count);
 
@@ -1047,8 +1030,8 @@ int main(int argc, char **argv)
                    return 1;
                }
 
-               printf("Upper bound on index file size = %ju bytes\n",
-                      (intmax_t)totalsize);
+               printf("Upper bound on index file size = %llu bytes\n",
+                      (unsigned long long)totalsize);
 
                mappedfile = mmap(NULL, totalsize, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
                if (!mappedfile) {
@@ -1073,7 +1056,8 @@ int main(int argc, char **argv)
                munmap(mappedfile, totalsize);
                ftruncate(fd, realsize);
                close(fd);
-               printf("Actual index file size = %ju bytes\n", (intmax_t)realsize);
+               printf("Actual index file size = %llu bytes\n",
+                      (unsigned long long)realsize);
            }
        } else if (mode == TEXT) {
            char *querydir = actions[action].arg;
diff --git a/agedu.h b/agedu.h
index ca53f1e..fecce2e 100644 (file)
--- a/agedu.h
+++ b/agedu.h
@@ -4,6 +4,91 @@
 
 #include "config.h"
 
+#ifdef HAVE_FEATURES_H
+#define _GNU_SOURCE
+#include <features.h>
+#endif
+
+#ifdef HAVE_STDIO_H
+#  include <stdio.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#  include <errno.h>
+#endif
+#ifdef HAVE_TIME_H
+#  include <time.h>
+#endif
+#ifdef HAVE_ASSERT_H
+#  include <assert.h>
+#endif
+#ifdef HAVE_STRING_H
+#  include <string.h>
+#endif
+#ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+#endif
+#ifdef HAVE_STDARG_H
+#  include <stdarg.h>
+#endif
+#ifdef HAVE_STDINT_H
+#  include <stdint.h>
+#endif
+#ifdef HAVE_STDDEF_H
+#  include <stddef.h>
+#endif
+#ifdef HAVE_LIMITS_H
+#  include <limits.h>
+#endif
+#ifdef HAVE_CTYPE_H
+#  include <ctype.h>
+#endif
+
+#ifdef HAVE_SYS_TYPES_H
+#  include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+#  include <sys/stat.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#  include <fcntl.h>
+#endif
+#ifdef HAVE_SYS_MMAN_H
+#  include <sys/mman.h>
+#endif
+#ifdef HAVE_TERMIOS_H
+#  include <termios.h>
+#endif
+#ifdef HAVE_SYS_IOCTL_H
+#  include <sys/ioctl.h>
+#endif
+#ifdef HAVE_FNMATCH_H
+#  include <fnmatch.h>
+#endif
+#ifdef HAVE_PWD_H
+#  include <pwd.h>
+#endif
+#ifdef HAVE_SYS_WAIT_H
+#  include <sys/wait.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+#  include <sys/socket.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
+#  include <arpa/inet.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+#  include <netinet/in.h>
+#endif
+#ifdef HAVE_SYSLOG_H
+#  include <syslog.h>
+#endif
+#ifdef HAVE_SYS_SELECT_H
+#  include <sys/select.h>
+#endif
+    
 #define PNAME "agedu"
 
 #define DUMPHDR "agedu dump file. pathsep="
diff --git a/alloc.c b/alloc.c
index e5c018f..265d6a4 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -2,13 +2,6 @@
  * alloc.c: implementation of alloc.h
  */
 
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <time.h>
-#include <assert.h>
-#include <stdio.h>
-
 #include "agedu.h"
 #include "alloc.h"
 
diff --git a/config.h b/config.h
deleted file mode 100644 (file)
index 27135bd..0000000
--- a/config.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * config.h: stub version of the autoconf-generated config.h, to
- * stop the #include in agedu.h from failing when run from the
- * master source directory instead of from the unpacked contents
- * of the autotoolsified tarball.
- *
- * This version of config.h hardwires some parameters that match
- * the sorts of systems I tend to be building on. A nasty hack and
- * a self-centred one, but autotools is so icky that I can't quite
- * bring myself to inflict it on my main development directory,
- * and am instead keeping it at arm's length in the distribution
- * tarballs. Sorry about that.
- */
-
-#define HAVE_LSTAT64
-#define HAVE_FEATURES_H
index 8117e81..21a5ff8 100644 (file)
@@ -1,4 +1,4 @@
-# autoconf input for agedu.
+# autoconf input for agedu. -*- sh -*-
 
 AC_INIT([agedu], [6.66], [anakin@pobox.com])
 AC_CONFIG_SRCDIR([agedu.c])
@@ -17,7 +17,7 @@ AC_PROG_INSTALL
 AC_HEADER_DIRENT
 AC_HEADER_STDC
 AC_HEADER_SYS_WAIT
-AC_CHECK_HEADERS([arpa/inet.h fcntl.h features.h limits.h netinet/in.h stddef.h stdint.h stdlib.h string.h sys/ioctl.h sys/socket.h syslog.h termios.h unistd.h])
+AC_CHECK_HEADERS([assert.h arpa/inet.h ctype.h errno.h fcntl.h features.h fnmatch.h limits.h netinet/in.h pwd.h stdarg.h stddef.h stdint.h stdio.h stdlib.h string.h sys/ioctl.h sys/mman.h sys/socket.h syslog.h termios.h time.h unistd.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
@@ -34,5 +34,8 @@ AC_FUNC_STRFTIME
 AC_FUNC_VPRINTF
 AC_CHECK_FUNCS([ftruncate fdopendir inet_ntoa lstat64 memchr munmap select socket strcasecmp strchr strcspn strerror strrchr strspn strtoul strtoull])
 
+AC_SEARCH_LIBS(connect, socket nsl)
+# AC_SEARCH_LIBS(gethostbyname, socket nsl resolv)
+
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
diff --git a/du.c b/du.c
index 1a375bf..3c8d5ee 100644 (file)
--- a/du.c
+++ b/du.c
@@ -2,34 +2,31 @@
  * du.c: implementation of du.h.
  */
 
-#include "agedu.h" /* for config.h */
-
-#ifdef HAVE_FEATURES_H
-#define _GNU_SOURCE
-#include <features.h>
-#endif
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
+#include "agedu.h"
 #include "du.h"
 #include "alloc.h"
 
 #if !defined __linux__ || defined HAVE_FDOPENDIR
 
+#ifdef HAVE_DIRENT_H
+#  include <dirent.h>
+#endif
+#ifdef HAVE_NDIR_H
+#  include <ndir.h>
+#endif
+#ifdef HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+#endif
+#ifdef HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+#endif
+
 /*
  * Wrappers around POSIX opendir, readdir and closedir, which
  * permit me to replace them with different wrappers in special
  * circumstances.
  */
 
-#include <dirent.h>
 typedef DIR *dirhandle;
 
 int open_dir(const char *path, dirhandle *dh)
@@ -106,8 +103,6 @@ void close_dir(dirhandle *dh)
  */
 
 #define __KERNEL__
-#include <unistd.h>
-#include <fcntl.h>
 #include <linux/types.h>
 #include <linux/dirent.h>
 #include <linux/unistd.h>
index 39696ac..e7ffe7b 100644 (file)
@@ -2,9 +2,7 @@
  * fgetline.c: implementation of fgetline.h.
  */
 
-#include <stdio.h>
-#include <string.h>
-
+#include "agedu.h"
 #include "alloc.h"
 #include "fgetline.h"
 
diff --git a/html.c b/html.c
index 48f1827..db6c25b 100644 (file)
--- a/html.c
+++ b/html.c
@@ -2,15 +2,6 @@
  * html.c: implementation of html.h.
  */
 
-#include <assert.h>
-#include <stddef.h>
-#include <string.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <limits.h>
-#include <time.h>
-
 #include "agedu.h"
 #include "html.h"
 #include "alloc.h"
@@ -36,9 +27,16 @@ static void vhtprintf(struct html *ctx, char *fmt, va_list ap)
 {
     va_list ap2;
     int size, size2;
+    char testbuf[2];
 
     va_copy(ap2, ap);
-    size = vsnprintf(NULL, 0, fmt, ap2);
+    /*
+     * Some C libraries (Solaris, I'm looking at you) don't like
+     * an output buffer size of zero in vsnprintf, but will return
+     * sensible values given any non-zero buffer size. Hence, we
+     * use testbuf to gauge the length of the string.
+     */
+    size = vsnprintf(testbuf, 1, fmt, ap2);
     va_end(ap2);
 
     if (ctx->buflen + size >= ctx->bufsize) {
diff --git a/httpd.c b/httpd.c
index dfb17b7..7c3f0f8 100644 (file)
--- a/httpd.c
+++ b/httpd.c
@@ -2,24 +2,6 @@
  * httpd.c: implementation of httpd.h.
  */
 
-#define _GNU_SOURCE
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-#include <unistd.h>
-#include <pwd.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <syslog.h>
-
 #include "agedu.h"
 #include "alloc.h"
 #include "html.h"
@@ -581,7 +563,9 @@ void run_httpd(const void *t, int authmask, const struct httpd_config *dcfg,
      */
     while (1) {
        fd_set rfds, wfds;
-       int i, j, maxfd, ret;
+       int i, j;
+       SELECT_TYPE_ARG1 maxfd;
+       int ret;
 
 #define FD_SET_MAX(fd, set, max) \
         do { FD_SET((fd),(set)); (max) = ((max)<=(fd)?(fd)+1:(max)); } while(0)
@@ -630,7 +614,9 @@ void run_httpd(const void *t, int authmask, const struct httpd_config *dcfg,
        }
        nfds = i;
 
-        ret = select(maxfd, &rfds, &wfds, NULL, NULL);
+        ret = select(maxfd, SELECT_TYPE_ARG234 &rfds,
+                    SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 NULL,
+                    SELECT_TYPE_ARG5 NULL);
        if (ret <= 0) {
            if (ret < 0 && (errno != EINTR)) {
                fprintf(stderr, "select: %s", strerror(errno));
diff --git a/index.c b/index.c
index 56d657d..19cde65 100644 (file)
--- a/index.c
+++ b/index.c
@@ -2,10 +2,7 @@
  * index.c: Implementation of index.h.
  */
 
-#include <assert.h>
-#include <stdio.h>
-#include <stddef.h>
-
+#include "agedu.h"
 #include "trie.h"
 #include "index.h"
 #include "alloc.h"
diff --git a/trie.c b/trie.c
index eb9fb81..51fba3e 100644 (file)
--- a/trie.c
+++ b/trie.c
@@ -2,15 +2,6 @@
  * trie.c: implementation of trie.h.
  */
 
-#include <assert.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-
-#include <sys/types.h>
-#include <unistd.h>
-
 #include "agedu.h"
 #include "alloc.h"
 #include "trie.h"