Information about the request is passed on via the environment, just like when executing a CGI program. I use the following program to log the download of the Hiawatha source code.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define TIMESTAMP_SIZE 40
int main(void) {
char *ip_address, timestamp[TIMESTAMP_SIZE], *request_uri;
time_t t;
struct tm *s;
FILE *fp;
if ((ip_address = getenv("REMOTE_ADDR")) == NULL) {
ip_address = "unknown";
}
time(&t);
s = localtime(&t);
timestamp[TIMESTAMP_SIZE - 1] = '\0';
strftime(timestamp, TIMESTAMP_SIZE - 1, "%Y-%m-%d %H:%M:%S", s);
if ((request_uri = getenv("REQUEST_URI")) == NULL) {
request_uri = "unknown";
} else if (strlen(request_uri) > 7) {
request_uri += 7;
}
if ((fp = fopen("download.log", "a")) != NULL) {
fprintf(fp, "%s|%s|%s\n", ip_address, timestamp, request_uri);
fclose(fp);
}
return EXIT_SUCCESS;
}