]> git.armaanb.net Git - cdir.git/commitdiff
Initial commit main
authorArmaan Bhojwani <me@armaanb.net>
Fri, 4 Jun 2021 00:41:00 +0000 (20:41 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Fri, 4 Jun 2021 00:41:00 +0000 (20:41 -0400)
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
Makefile [new file with mode: 0644]
README [new file with mode: 0644]
cdir.c [new file with mode: 0644]
contrib/Caddyfile [new file with mode: 0644]
man/cdir.1.scd [new file with mode: 0644]
man/cdirrc.5.scd [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..25a15a5
--- /dev/null
@@ -0,0 +1,3 @@
+cdir.1
+cdirrc.5
+cdir
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..90490a5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+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
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..6ff7cfe
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+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
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..f3df48a
--- /dev/null
+++ b/README
@@ -0,0 +1,12 @@
+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
diff --git a/cdir.c b/cdir.c
new file mode 100644 (file)
index 0000000..bffbc12
--- /dev/null
+++ b/cdir.c
@@ -0,0 +1,73 @@
+#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;
+}
diff --git a/contrib/Caddyfile b/contrib/Caddyfile
new file mode 100644 (file)
index 0000000..e2f9ffa
--- /dev/null
@@ -0,0 +1,8 @@
+{
+       order cgi last
+}
+
+:8080 {
+       root .
+       cgi * ./cdir
+}
diff --git a/man/cdir.1.scd b/man/cdir.1.scd
new file mode 100644 (file)
index 0000000..a5494a7
--- /dev/null
@@ -0,0 +1 @@
+cdir(1)
diff --git a/man/cdirrc.5.scd b/man/cdirrc.5.scd
new file mode 100644 (file)
index 0000000..3e68ccd
--- /dev/null
@@ -0,0 +1 @@
+cdrirrc(5)