summary refs log tree commit diff
path: root/www/temp.causal.agency
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--www/temp.causal.agency/up.c88
1 files changed, 42 insertions, 46 deletions
diff --git a/www/temp.causal.agency/up.c b/www/temp.causal.agency/up.c
index f4faa792..d0177979 100644
--- a/www/temp.causal.agency/up.c
+++ b/www/temp.causal.agency/up.c
@@ -33,8 +33,6 @@
 #include <kcgi.h>
 #include <kcgihtml.h>
 
-static int dir = -1;
-
 static const char *Page = "up";
 static const struct kvalid Key = { NULL, "file" };
 
@@ -49,6 +47,7 @@ static enum kcgi_err fail(struct kreq *req, enum khttp http) {
 		|| khttp_printf(req, "%s\n", khttps[http]);
 }
 
+static int dir = -1;
 static const char *upload(const char *ext, void *ptr, size_t len) {
 	static char name[256];
 	snprintf(
@@ -137,48 +136,70 @@ static enum kcgi_err handle(struct kreq *req) {
 	}
 }
 
-static void cgi(void) {
+int main(int argc, char *argv[]) {
+	int error;
+	const char *path = (argc > 1 ? argv[1] : ".");
+	dir = open(path, O_DIRECTORY);
+	if (dir < 0) err(EX_NOINPUT, "%s", path);
+
+#ifdef __OpenBSD__
+	error = unveil(path, "wc");
+	if (error) err(EX_OSERR, "unveil");
+#endif
+
+#ifdef __FreeBSD__
+	cap_rights_t rights;
+	cap_rights_init(&rights, CAP_LOOKUP, CAP_CREATE, CAP_PWRITE);
+	error = cap_rights_limit(dir, &rights);
+	if (error) err(EX_OSERR, "cap_rights_limit");
+#endif
+
+	if (!khttp_fcgi_test()) {
 #ifdef __OpenBSD__
-	if (pledge("stdio wpath cpath proc", NULL)) err(EX_OSERR, "pledge");
+		error = pledge("stdio wpath cpath proc", NULL);
+		if (error) err(EX_OSERR, "pledge");
 #endif
 
-	struct kreq req;
-	enum kcgi_err error = khttp_parse(&req, &Key, 1, &Page, 1, 0);
-	if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
+		struct kreq req;
+		error = khttp_parse(&req, &Key, 1, &Page, 1, 0);
+		if (error) errx(EX_PROTOCOL, "khttp_parse: %s", kcgi_strerror(error));
 
 #ifdef __OpenBSD__
-	if (pledge("stdio wpath cpath", NULL)) err(EX_OSERR, "pledge");
+		error = pledge("stdio wpath cpath", NULL);
+		if (error) err(EX_OSERR, "pledge");
 #endif
 #ifdef __FreeBSD__
-	if (cap_enter()) err(EX_OSERR, "cap_enter");
+		error = cap_enter();
+		if (error) err(EX_OSERR, "cap_enter");
 #endif
 
-	error = handle(&req);
-	if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
-	khttp_free(&req);
-}
+		error = handle(&req);
+		if (error) errx(EX_PROTOCOL, "%s", kcgi_strerror(error));
+		khttp_free(&req);
+		return EX_OK;
+	}
 
-static void fcgi(void) {
 #ifdef __OpenBSD__
-	if (pledge("stdio wpath cpath unix sendfd recvfd proc", NULL)) {
-		err(EX_OSERR, "pledge");
-	}
+	error = pledge("stdio wpath cpath unix sendfd recvfd proc", NULL);
+	if (error) err(EX_OSERR, "pledge");
 #endif
 
 	struct kfcgi *fcgi;
-	enum kcgi_err error = khttp_fcgi_init(&fcgi, &Key, 1, &Page, 1, 0);
+	error = khttp_fcgi_init(&fcgi, &Key, 1, &Page, 1, 0);
 	if (error) errx(EX_CONFIG, "khttp_fcgi_init: %s", kcgi_strerror(error));
 
 #ifdef __OpenBSD__
-	if (pledge("stdio wpath cpath recvfd", NULL)) err(EX_OSERR, "pledge");
+	error = pledge("stdio wpath cpath recvfd", NULL);
+	if (error) err(EX_OSERR, "pledge");
 #endif
 #ifdef __FreeBSD__
-	if (cap_enter()) err(EX_OSERR, "cap_enter");
+	error = cap_enter();
+	if (error) err(EX_OSERR, "cap_enter");
 #endif
 
 	for (
 		struct kreq req;
-		KCGI_OK == (error = khttp_fcgi_parse(fcgi, &req));
+		!(error = khttp_fcgi_parse(fcgi, &req));
 		khttp_free(&req)
 	) {
 		error = handle(&req);
@@ -189,28 +210,3 @@ static void fcgi(void) {
 	}
 	khttp_fcgi_free(fcgi);
 }
-
-int main(int argc, char *argv[]) {
-	int error;
-	const char *path = (argc > 1 ? argv[1] : ".");
-	dir = open(path, O_DIRECTORY);
-	if (dir < 0) err(EX_NOINPUT, "%s", path);
-
-#ifdef __OpenBSD__
-	error = unveil(path, "wc");
-	if (error) err(EX_OSERR, "unveil");
-#endif
-
-#ifdef __FreeBSD__
-	cap_rights_t rights;
-	cap_rights_init(&rights, CAP_LOOKUP, CAP_CREATE, CAP_PWRITE);
-	error = cap_rights_limit(dir, &rights);
-	if (error) err(EX_OSERR, "cap_rights_limit");
-#endif
-
-	if (khttp_fcgi_test()) {
-		fcgi();
-	} else {
-		cgi();
-	}
-}