Debian Codegraph @

listings/chapter-11/time.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <sys/time.h>
10 #include <time.h>
11
12 #include "server.h"
13
14 /* A template for the HTML page this module generates. */
15
16 static char* page_template =
17 "<html>\n"
18 " <head>\n"
19 " <meta http-equiv=\"refresh\" content=\"5\">\n"
20 " </head>\n"
21 " <body>\n"
22 " <p>The current time is %s.</p>\n"
23 " </body>\n"
24 "</html>\n";
25
26 void module_generate (int fd)
27 {
28 struct timeval tv;
29 struct tm* ptm;
30 char time_string[40];
31 FILE* fp;
32
33 /* Obtain the time of day, and convert it to a tm struct. */
34 gettimeofday (&tv, NULL);
35 ptm = localtime (&tv.tv_sec);
36 /* Format the date and time, down to a single second. */
37 strftime (time_string, sizeof (time_string), "%H:%M:%S", ptm);
38
39 /* Create a stream corresponding to the client socket file
40 descriptor. */
41 fp = fdopen (fd, "w");
42 assert (fp != NULL);
43 /* Generate the HTML output. */
44 fprintf (fp, page_template, time_string);
45 /* All done; flush the stream. */
46 fflush (fp);
47 }