]> git.armaanb.net Git - bin.git/commitdiff
fortune-online: add program
authorArmaan Bhojwani <me@armaanb.net>
Mon, 24 May 2021 18:13:32 +0000 (14:13 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 24 May 2021 18:14:30 +0000 (14:14 -0400)
CGI program to get fortunes. Powers https://fortune.armaanb.net.

fortune-online.c [new file with mode: 0644]

diff --git a/fortune-online.c b/fortune-online.c
new file mode 100644 (file)
index 0000000..63693d7
--- /dev/null
@@ -0,0 +1,36 @@
+// CGI program to get fortunes. Powers https://fortune.armaanb.net.
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+}