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