]> git.armaanb.net Git - bin.git/blob - fortune-cgi.c
chrooter: drop script
[bin.git] / fortune-cgi.c
1 // CGI program to get fortunes. Powers https://fortune.armaanb.net.
2
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 int
8 main(void)
9 {
10         const char *path = getenv("PATH_INFO");
11         FILE *fp;
12         char buf[255];
13
14         if (strcmp(path, "/latin-cowsay") == 0) {
15                 fp = popen("phrases | cowsay", "r");
16         } else if (strcmp(path, "/cowsay") == 0) {
17                 fp = popen("fortune | cowsay", "r");
18         } else if (strcmp(path, "/latin") == 0) {
19                 fp = popen("phrases", "r");
20         } else if (strcmp(path, "/") == 0) {
21                 fp = popen("fortune", "r");
22         } else {
23                 printf("Status: 404"
24                                          "Content-type: text/plain\n\n"
25                                          "Page not found. Try /, /cowsay, /latin, or /latin-cowsay\n");
26                 return 0;
27         }
28
29         printf("Content-type: text/plain\n\n");
30         while (fgets(buf, sizeof(buf), fp) != NULL) {
31                 printf("%s", buf);
32         }
33         pclose(fp);
34
35         return 0;
36 }