--- /dev/null
+cdir.1
+cdirrc.5
+cdir
\ No newline at end of file
--- /dev/null
+Copyright 2021 Armaan Bhojwani <me@armaanb.net>
+
+Permission to use, copy, modify, and/or distribute this software for
+any purpose with or without fee is hereby granted, provided that the
+above copyright notice and this permission notice appear in all
+copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
\ No newline at end of file
--- /dev/null
+CFLAGS = -std=c99 -Wall -Wextra -pedantic -g
+
+all: man binary
+
+binary:
+ ${CC} cdir.c -o cdir `pkg-config --libs libarchive` ${LDFLAGS} \
+ -std=c99 ${CFLAGS}
+
+man:
+ scdoc < man/cdir.1.scd > man/cdir.1
+ scdoc < man/cdirrc.5.scd > man/cdirrc.5
+
+install:
+ mkdir -p ${DESTDIR}${PREFIX}/bin ${DESTDIR}${PREFIX}/man
+ cp cdir ${DESTDIR}${PREFIX}/bin/cdir
+ cp cdir.1 ${DESTDIR}${PREFIX}/man/man1/cdir.1
+
+uninstall:
+ rm ${DESTDIR}${PREFIX}/bin/cdir
+ rm ${DESTDIR}${PREFIX}/man/man1/cdir.1
+
+devserver: binary
+ caddy run --config ./contrib/Caddyfile
--- /dev/null
+cdir
+----
+
+A better directory listing for your web server.
+
+Dependencies:
+ - libarchive
+ - a POSIX c99 compiler and libc
+ - POSIX make (build)
+ - scdoc (build, for generating manpages)
+
+Copyright 2021 Armaan Bhojwani, ISC license.
\ No newline at end of file
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/dir.h>
+
+struct Directory {
+ struct dirent **items;
+ int num;
+};
+
+void
+err(int code, char *msg)
+{
+ printf("Status: %d\n", code);
+ printf("Content-Type: text/plain\n\n");
+ printf("%s", msg);
+ exit(code);
+}
+
+struct Directory
+get_files(char *dirname) {
+ struct Directory outp;
+ outp.items = calloc(1, sizeof(struct dirent **));
+
+ DIR *d;
+ struct dirent *dir;
+ d = opendir(dirname);
+
+ if (!d) {
+ err(500, "ERROR: Could not open directory.\n");
+ }
+
+ outp.num = 0;
+ while ((dir = readdir(d)) != NULL) {
+ outp.items = realloc(outp.items, (outp.num + 1) * sizeof(struct dirent **));
+ outp.items[outp.num] = dir;
+ outp.num++;
+ }
+
+ closedir(d);
+ return outp;
+}
+
+int
+main(void) {
+ char *url = getenv("PATH_INFO");
+
+ if (!url) {
+ err(404, "ERROR: No path given.\n");
+ return 1;
+ }
+
+ int len = strlen(url) + 2;
+ char *path = malloc(len);
+ snprintf(path, len, ".%s", url);
+ struct Directory files = get_files(path);
+ free(path);
+
+ printf("Content-Type: text/html\n\n");
+ printf("<!DOCTYPE html><html lang=\"en\"><head>");
+ printf("</head><body>");
+ printf("<p>%d files found.</p>\n", files.num);
+ printf("<table>");
+ for (int i = 0; i < files.num; i++) {
+ printf("<tr>\n<td>");
+ printf("<a href=\"%s\">%s</p>\n", files.items[i]->d_name,
+ files.items[i]->d_name);
+ printf("</td>\n</tr>");
+ }
+ printf("</table>\n");
+ printf("</body></html>");
+ return 0;
+}
--- /dev/null
+{
+ order cgi last
+}
+
+:8080 {
+ root .
+ cgi * ./cdir
+}
--- /dev/null
+cdrirrc(5)