From: Armaan Bhojwani Date: Mon, 24 May 2021 18:13:32 +0000 (-0400) Subject: fortune-online: add program X-Git-Url: https://git.armaanb.net/?p=bin.git;a=commitdiff_plain;h=e492921467747e639ff4c11e8e37d55ee5cd3bcf fortune-online: add program CGI program to get fortunes. Powers https://fortune.armaanb.net. --- diff --git a/fortune-online.c b/fortune-online.c new file mode 100644 index 0000000..63693d7 --- /dev/null +++ b/fortune-online.c @@ -0,0 +1,36 @@ +// CGI program to get fortunes. Powers https://fortune.armaanb.net. + +#include +#include +#include + +int +main(void) +{ + const char *path = getenv("PATH_INFO"); + FILE *fp; + char buf[255]; + + if (strcmp(path, "/latin-cowsay") == 0) { + fp = popen("phrases | cowsay", "r"); + } else if (strcmp(path, "/cowsay") == 0) { + fp = popen("fortune | cowsay", "r"); + } else if (strcmp(path, "/latin") == 0) { + fp = popen("phrases", "r"); + } else if (strcmp(path, "/") == 0) { + fp = popen("fortune", "r"); + } else { + printf("Status: 404" + "Content-type: text/plain\n\n" + "Page not found. Try /, /cowsay, /latin, or /latin-cowsay\n"); + return 0; + } + + printf("Content-type: text/plain\n\n"); + while (fgets(buf, sizeof(buf), fp) != NULL) { + printf("%s", buf); + } + pclose(fp); + + return 0; +}