12 err(int code, char *msg)
14 printf("Status: %d\n", code);
15 printf("Content-Type: text/plain\n\n");
21 get_files(char *dirname) {
22 struct Directory outp;
23 outp.items = calloc(1, sizeof(struct dirent **));
30 err(500, "ERROR: Could not open directory.\n");
34 while ((dir = readdir(d)) != NULL) {
35 outp.items = realloc(outp.items, (outp.num + 1) * sizeof(struct dirent **));
36 outp.items[outp.num] = dir;
46 char *url = getenv("PATH_INFO");
49 err(404, "ERROR: No path given.\n");
53 int len = strlen(url) + 2;
54 char *path = malloc(len);
55 snprintf(path, len, ".%s", url);
56 struct Directory files = get_files(path);
59 printf("Content-Type: text/html\n\n");
60 printf("<!DOCTYPE html><html lang=\"en\"><head>");
61 printf("</head><body>");
62 printf("<p>%d files found.</p>\n", files.num);
64 for (int i = 0; i < files.num; i++) {
66 printf("<a href=\"%s\">%s</p>\n", files.items[i]->d_name,
67 files.items[i]->d_name);
68 printf("</td>\n</tr>");
71 printf("</body></html>");