tAdd working example - vote - simple cgi voting system for web and gopher
(HTM) git clone git://src.adamsgaard.dk/vote
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 3925bb8d6b4fe0b136655a5adbfef61ae27b1f48
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Sun, 27 Sep 2020 00:02:57 +0200
Add working example
Diffstat:
A Makefile | 22 ++++++++++++++++++++++
A vote.c | 55 +++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/Makefile b/Makefile
t@@ -0,0 +1,22 @@
+.POSIX:
+
+NAME = vote
+
+HERE_CFLAGS = ${CFLAGS}
+HERE_LDFLAGS = -static ${LDFLAGS}
+
+SRC = vote.c
+OBJ = ${SRC:.c=.o}
+
+all: ${NAME}
+
+${OBJ}:
+
+${NAME}: ${OBJ}
+ ${CC} -o $@ ${OBJ} ${HERE_LDFLAGS}
+
+clean:
+ rm -f *.o
+ rm -f ${NAME}
+
+.PHONY: default clean
(DIR) diff --git a/vote.c b/vote.c
t@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#define OUT(s) (fputs((s), stdout))
+
+void
+print_html_head() {
+ OUT("Content-type: text/html; charset=utf-8\r\n\r\n");
+ OUT("<!DOCTYPE html>\n"
+ "<html>\n"
+ "<body>\n");
+}
+
+void
+print_html_foot() {
+ OUT("</body>\n"
+ "</html>\n");
+}
+
+char *
+getparam(const char *query, const char *s) {
+ const char *p, *last = NULL;
+ size_t len;
+
+ len = strlen(s);
+ for (p = query; (p = strstr(p, s)); p += len) {
+ if (p[len] == '=' && (p == query || p[-1] == '&' || p[-1] == '?'))
+ last = p + len + 1;
+ }
+ return (char *)last;
+}
+
+int
+main() {
+ char *query;
+ char *q;
+
+ if (pledge("stdio", NULL) == -1) {
+ OUT("Status: 500 Internal Server Error\r\n\r\n");
+ exit(1);
+ }
+
+ if (!(query = getenv("QUERY_STRING")))
+ query = "";
+
+ q = getparam(query, "q");
+
+ print_html_head();
+ printf("<p>query string: '%s', q: '%s'</p>\n", query, q);
+ print_html_foot();
+
+ return 0;
+}