forked from sternenseemann/sternenblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.c
More file actions
69 lines (54 loc) · 1.56 KB
/
simple.c
File metadata and controls
69 lines (54 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include "../template.h"
#include "../config.h"
void template_header(void) {
printf("<!doctype html>\n"
"<html>\n"
"<head>\n"
"\t<title>%s</title>\n"
"\t<meta charset=\"utf-8\"/>\n"
"\t<style type=\"text/css\">\n"
"\ta:link, a:visited{\n"
"\tcolor:blue;\n"
"\t}</style>\n"
"</head>\n"
"<body><h1>%s</h1>\n", BLOG_TITLE, BLOG_TITLE);
}
void template_footer(void) {
printf("\t<footer><a href=\"%s/rss.xml\">RSS feed</a></footer>\n"
"</body></html>", getenv("SCRIPT_NAME"));
}
void template_post_single_entry(struct blogpost post) {
FILE *fp = fopen(post.path, "r");
char c;
if(fp == NULL) {
template_error_404();
exit(EXIT_SUCCESS);
/* TODO: does CGI still work correctly if we exit
* with EXIT_FAILURE? */
}
printf("<article>\n");
while((c = fgetc(fp)) != EOF) {
printf("%c", c);
}
printf("<p><a href=\"%s\">Back</a></p></article>\n", getenv("SCRIPT_NAME"));
fclose(fp);
}
void template_post_index_entry(struct blogpost post) {
FILE *fp = fopen(post.path, "r");
char c;
if(fp == NULL) {
fprintf(stderr, "No such file or directory: %s\n", post.path);
exit(EXIT_FAILURE);
}
printf("<article>\n");
while((c = fgetc(fp)) != EOF) {
printf("%c", c);
}
printf("<p><a href=\"%s\">Permalink</a></p></article>\n", post.link);
fclose(fp);
}
void template_error_404(void) {
printf("<article><h2>Error 404</h2><p>This page does not exist. I am so sorry</p></article>");
}