-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspin.c
57 lines (50 loc) · 1.44 KB
/
spin.c
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
/*
Include headers
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "definitions.h"
/*
get_seconds():
Calculates the present time in seconds.
*/
double get_seconds() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6);
}
/*
Main function calls the get_seconds() function to
spin for the specified amount of time on the server.
*/
int main(int argc, char *argv[]) {
// Extract arguments
double spin_for = 0.0;
char *buf;
if ((buf = getenv("QUERY_STRING")) != NULL) {
// just expecting a single number
spin_for = (double) atoi(buf);
}
double t1 = get_seconds();
while ((get_seconds() - t1) < spin_for)
sleep(1);
double t2 = get_seconds();
/* Make the response body */
char content[MAXBUF + 1];
char spunfor[MAXBUF];
snprintf(content, MAXBUF, "<p>Welcome to the CGI program (%s)</p>\r\n", buf);
strncat(content, "%s<p>My only purpose is to waste time on the server!</p>\r\n", MAXBUF);
snprintf(spunfor, MAXBUF, "<p>I spun for %.2f seconds</p>\r\n", t2 - t1);
strncat(content, spunfor, MAXBUF);
/* Generate the HTTP response */
printf("Content-length: %lu\r\n", strlen(content));
printf("Content-type: text/html\r\n\r\n");
printf("%s", content);
fflush(stdout);
exit(0);
}