-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNowServing.c
More file actions
192 lines (161 loc) · 5.78 KB
/
NowServing.c
File metadata and controls
192 lines (161 loc) · 5.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int currentNumber = 0;
void serveNextNumber() {
currentNumber++;
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<script>window.location.href = \"now_serving.html\";</script>");
printf("</head>");
printf("<body>");
printf("</body>");
printf("</html>");
}
int main() {
char *requestData = getenv("QUERY_STRING");
if (requestData != NULL) {
char *nextParam = strtok(requestData, "&");
while (nextParam != NULL) {
char paramName[50], paramValue[50];
sscanf(nextParam, "%[^=]=%s", paramName, paramValue);
if (strcmp(paramName, "action") == 0 && strcmp(paramValue, "next") == 0) {
serveNextNumber();
break;
}
nextParam = strtok(NULL, "&");
}
}
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<meta http-equiv=\"refresh\" content=\"5\">");
printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">");
printf("</head>");
printf("<body>");
printf("<h2>Current Number: %d</h2>", currentNumber);
printf("<a href=\"now_serving.html?action=next\"><button>Next</button></a>");
printf("</body>");
printf("</html>");
return 0;
}
int main() {
char *requestData = getenv("QUERY_STRING");
// If there is a query string
if (requestData != NULL) {
char *nextParam = strtok(requestData, "&");
// Check each parameter in the query string
while (nextParam != NULL) {
char paramName[50], paramValue[50];
sscanf(nextParam, "%[^=]=%s", paramName, paramValue);
// Check if the action parameter is 'next'
if (strcmp(paramName, "action") == 0 && strcmp(paramValue, "next") == 0) {
serveNextNumber();
exit(0);
}
nextParam = strtok(NULL, "&");
}
// If the action parameter is missing or invalid
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<title>Error</title>");
printf("</head>");
printf("<body>");
printf("<h1>Error: Invalid Request</h1>");
printf("</body>");
printf("</html>");
}
// If there is no query string
else {
// Increment the current number
currentNumber++;
// Open the file for writing
FILE *fptr = fopen("current_number.txt", "w");
// If the file could not be opened
if (fptr == NULL) {
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<title>Error</title>");
printf("</head>");
printf("<body>");
printf("<h1>Error: Unable to Open File</h1>");
printf("</body>");
printf("</html>");
}
// If the file was successfully opened
else {
// Print the current number to the file
fprintf(fptr, "%d", currentNumber);
// Close the file
fclose(fptr);
// Reload the page with the updated current number
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<script>window.location.href = \"now_serving.html\";</script>");
printf("</head>");
printf("<body>");
printf("</body>");
printf("</html>");
}
}
return 0;
}
void serveNextNumber() {
// Open the file for reading
FILE *fptr = fopen("current_number.txt", "r");
// If the file could not be opened
if (fptr == NULL) {
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<title>Error</title>");
printf("</head>");
printf("<body>");
printf("<h1>Error: Unable to Open File</h1>");
printf("</body>");
printf("</html>");
}
// If the file was successfully opened
else {
// Read the current number from the file
fscanf(fptr, "%d", ¤tNumber);
// Close the file
fclose(fptr);
// Increment the current number
currentNumber++;
// Open the file for writing
fptr = fopen("current_number.txt", "w");
// If the file could not be opened
if (fptr == NULL) {
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<title>Error</title>");
printf("</head>");
printf("<body>");
printf("<h1>Error: Unable to Open File</h1>");
printf("</body>");
printf("</html>");
}
// If the file was successfully opened
else {
// Print the current number to the file
fprintf(fptr, "%d", currentNumber);
// Close the file
fclose(fptr);
// Reload the page with the updated current number
printf("Content-Type: text/html\n\n");
printf("<html>");
printf("<head>");
printf("<script>window.location.href = \"now_serving.html\";</script>");
printf("</head>");
printf("<body>");
printf("</body>");
printf("</html>");
}
}
}