2 * Copyright (c) 2007 Denys Vlasenko <vda.linux@googlemail.com>
4 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 * This program is a CGI application. It creates directory index page.
9 * Put it into cgi-bin/index.cgi and chmod 0755.
13 i486-linux-uclibc-gcc \
14 -static -static-libgcc \
15 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
16 -Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Werror \
17 -Wold-style-definition -Wdeclaration-after-statement -Wno-pointer-sign \
18 -Wmissing-prototypes -Wmissing-declarations \
19 -Os -fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer \
20 -ffunction-sections -fdata-sections -fno-guess-branch-probability \
22 -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 \
23 -march=i386 -mpreferred-stack-boundary=2 \
24 -Wl,-Map -Wl,link.map -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
25 httpd_indexcgi.c -o index.cgi
28 text data bss dec hex filename
29 22642 160 3052 25854 64fe index.cgi
32 /* TODO: get rid of printf's: printf code is more than 50%
33 * of the entire executable when built against static uclibc */
35 #include <sys/types.h>
46 /* Appearance of the table is controlled by style sheet *ONLY*,
47 * formatting code uses <TAG class=CLASS> to apply style
48 * to elements. Edit stylesheet to your liking and recompile. */
50 static const char str_header[] =
51 "" /* Additional headers (currently none) */
52 "\r\n" /* Mandatory empty line after headers */
53 "<html><head><title>Index of %s</title>" "\n"
57 " background-color: #fff5ee;" "\n"
58 " border-width: 1px;" /* 1px 1px 1px 1px; */ "\n"
59 " border-spacing: 2px;" "\n"
60 " border-style: solid;" /* solid solid solid solid; */ "\n"
61 " border-color: black;" /* black black black black; */ "\n"
62 " border-collapse: collapse;" "\n"
65 " border-width: 1px;" /* 1px 1px 1px 1px; */ "\n"
66 " padding: 1px;" /* 1px 1px 1px 1px; */ "\n"
67 " border-style: solid;" /* solid solid solid solid; */ "\n"
68 " border-color: black;" /* black black black black; */ "\n"
71 /* top right bottom left */
72 " border-width: 0px 1px 0px 1px;" "\n"
73 " padding: 1px;" /* 1px 1px 1px 1px; */ "\n"
74 " border-style: solid;" /* solid solid solid solid; */ "\n"
75 " border-color: black;" /* black black black black; */ "\n"
77 "tr.hdr { background-color:#eee5de; }" "\n"
78 "tr.o { background-color:#ffffff; }" "\n"
79 /* tr.e { ... } - for even rows (currently none) */
80 "tr.foot { background-color:#eee5de; }" "\n"
81 "th.cnt { text-align:left; }" "\n"
82 "th.sz { text-align:right; }" "\n"
83 "th.dt { text-align:right; }" "\n"
84 "td.sz { text-align:right; }" "\n"
85 "td.dt { text-align:right; }" "\n"
86 "col.nm { width: 98%%; }" "\n"
87 "col.sz { width: 1%%; }" "\n"
88 "col.dt { width: 1%%; }" "\n"
92 "<h1>Index of %s</h1>" "\n"
95 "<col class=nm><col class=sz><col class=dt>" "\n"
96 "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n"
99 static const char str_footer[] =
100 "<tr class=foot><th class=cnt>Files: %u, directories: %u<th class=sz>%llu<th class=dt> " "\n"
101 /* "</table></body></html>" - why bother? */
104 static int bad_url_char(unsigned c)
106 return (c - '0') > 9 /* not a digit */
107 && ((c|0x20) - 'a') > 26 /* not A-Z or a-z */
108 && !strchr("._-+@", c);
111 static char *url_encode(const char *name)
115 int len = strlen(name);
120 if (bad_url_char((unsigned)name[i]))
129 /* Each %xx requires 2 additional chars */
130 size = size * 2 + len + 1;
131 p = result = malloc(size);
136 if (bad_url_char((unsigned)name[i])) {
138 *p++ = "0123456789ABCDEF"[(uint8_t)(name[i]) >> 4];
139 *p = "0123456789ABCDEF"[(uint8_t)(name[i]) & 0xf];
148 static char *html_encode(const char *name)
152 int len = strlen(name);
166 /* No < etc needed! */
170 /* & requires 4 additional chars */
171 size = size * 4 + len + 1;
172 p = result = malloc(size);
194 typedef struct dir_list_t {
201 static int compare_dl(dir_list_t *a, dir_list_t *b)
203 if (strcmp(a->dl_name, "..") == 0) {
204 /* ".." is 'less than' any other dir entry */
207 if (strcmp(b->dl_name, "..") == 0) {
210 if (S_ISDIR(a->dl_mode) != S_ISDIR(b->dl_mode)) {
211 /* 1 if b is a dir (and thus a is 'after' b, a > b),
213 return (S_ISDIR(b->dl_mode) != 0) ? 1 : -1;
215 return strcmp(a->dl_name, b->dl_name);
220 dir_list_t *dir_list;
222 unsigned dir_list_count;
224 unsigned count_files;
225 unsigned long long size_total;
230 QUERY_STRING = getenv("QUERY_STRING");
232 || QUERY_STRING[0] != '/'
233 || strstr(QUERY_STRING, "/../")
234 || strcmp(strrchr(QUERY_STRING, '/'), "/..") == 0
240 || (QUERY_STRING[1] && chdir(QUERY_STRING + 1))
258 if (dp->d_name[0] == '.' && !dp->d_name[1])
260 if (stat(dp->d_name, &sb) != 0)
262 dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
263 dir_list[dir_list_count].dl_name = strdup(dp->d_name);
264 dir_list[dir_list_count].dl_mode = sb.st_mode;
265 dir_list[dir_list_count].dl_size = sb.st_size;
266 dir_list[dir_list_count].dl_mtime = sb.st_mtime;
270 qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
272 /* Guard against directories wit &, > etc */
273 QUERY_STRING = html_encode(QUERY_STRING);
274 printf(str_header, QUERY_STRING, QUERY_STRING);
282 while (dir_list_count--) {
283 char size_str[sizeof(long long) * 3];
284 const char *slash_if_dir;
288 char datetime_str[sizeof("2000-02-02 02:02:02")];
291 if (S_ISDIR(cdir->dl_mode)) {
294 } else if (S_ISREG(cdir->dl_mode)) {
296 size_total += cdir->dl_size;
297 slash_if_dir++; /* points to "" now */
298 sprintf(size_str, "%llu", (unsigned long long)(cdir->dl_size));
301 href = url_encode(cdir->dl_name); /* %20 etc */
302 filename = html_encode(cdir->dl_name); /* < etc */
303 tm = gmtime(&cdir->dl_mtime);
304 sprintf(datetime_str, "%04u-%02u-%02u %02u:%02u:%02u",
305 (unsigned)(1900 + tm->tm_year),
306 (unsigned)(tm->tm_mon + 1),
307 (unsigned)(tm->tm_mday),
308 (unsigned)(tm->tm_hour),
309 (unsigned)(tm->tm_min),
310 (unsigned)(tm->tm_sec)
312 printf("<tr class=%c><td class=nm><a href='%s%s'>%s%s</a><td class=sz>%s<td class=dt>%s\n",
315 filename, slash_if_dir,
319 if (cdir->dl_name != href)
321 if (cdir->dl_name != filename)
328 /* count_dirs - 1: we don't want to count ".." */
329 printf(str_footer, count_files, count_dirs - 1, size_total);