From 2763fe1cc750e84a4ac4863d0dfd57b39ce8e693 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Thu, 3 Jun 2021 20:41:00 -0400 Subject: [PATCH 1/1] Initial commit --- .gitignore | 3 ++ LICENSE | 15 ++++++++++ Makefile | 23 +++++++++++++++ README | 12 ++++++++ cdir.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ contrib/Caddyfile | 8 ++++++ man/cdir.1.scd | 1 + man/cdirrc.5.scd | 1 + 8 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README create mode 100644 cdir.c create mode 100644 contrib/Caddyfile create mode 100644 man/cdir.1.scd create mode 100644 man/cdirrc.5.scd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25a15a5 --- /dev/null +++ b/.gitignore @@ -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 index 0000000..90490a5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +Copyright 2021 Armaan Bhojwani + +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 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 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 index 0000000..bffbc12 --- /dev/null +++ b/cdir.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +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(""); + printf(""); + printf("

%d files found.

\n", files.num); + printf(""); + for (int i = 0; i < files.num; i++) { + printf("\n\n"); + } + printf("
"); + printf("%s

\n", files.items[i]->d_name, + files.items[i]->d_name); + printf("
\n"); + printf(""); + return 0; +} diff --git a/contrib/Caddyfile b/contrib/Caddyfile new file mode 100644 index 0000000..e2f9ffa --- /dev/null +++ b/contrib/Caddyfile @@ -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 index 0000000..a5494a7 --- /dev/null +++ b/man/cdir.1.scd @@ -0,0 +1 @@ +cdir(1) diff --git a/man/cdirrc.5.scd b/man/cdirrc.5.scd new file mode 100644 index 0000000..3e68ccd --- /dev/null +++ b/man/cdirrc.5.scd @@ -0,0 +1 @@ +cdrirrc(5) -- 2.39.2