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 outputs 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 /* We don't use printf, as it pulls in >12 kb of code from uclibc (i386). */
29 /* Currently malloc machinery is the biggest part of libc we pull in. */
30 /* We have only one realloc and one strdup, any idea how to do without? */
31 /* Size (i386, approximate):
32 * text data bss dec hex filename
33 * 13036 44 3052 16132 3f04 index.cgi
34 * 2576 4 2048 4628 1214 index.cgi.o
37 #include <sys/types.h>
48 /* Appearance of the table is controlled by style sheet *ONLY*,
49 * formatting code uses <TAG class=CLASS> to apply style
50 * to elements. Edit stylesheet to your liking and recompile. */
56 "background-color:#fff5ee;" "\n"\
57 "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
58 "border-spacing:2px;" "\n"\
59 "border-style:solid;" /* solid solid solid solid; */ "\n"\
60 "border-color:black;" /* black black black black; */ "\n"\
61 "border-collapse:collapse;" "\n"\
64 "border-width:1px;" /* 1px 1px 1px 1px; */ "\n"\
65 "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
66 "border-style:solid;" /* solid solid solid solid; */ "\n"\
67 "border-color:black;" /* black black black black; */ "\n"\
70 /* top right bottom left */ \
71 "border-width:0px 1px 0px 1px;" "\n"\
72 "padding:1px;" /* 1px 1px 1px 1px; */ "\n"\
73 "border-style:solid;" /* solid solid solid solid; */ "\n"\
74 "border-color:black;" /* black black black black; */ "\n"\
75 "white-space:nowrap;" "\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"\
91 typedef struct dir_list_t {
98 static int compare_dl(dir_list_t *a, dir_list_t *b)
100 /* ".." is 'less than' any other dir entry */
101 if (strcmp(a->dl_name, "..") == 0) {
104 if (strcmp(b->dl_name, "..") == 0) {
107 if (S_ISDIR(a->dl_mode) != S_ISDIR(b->dl_mode)) {
108 /* 1 if b is a dir (and thus a is 'after' b, a > b),
110 return (S_ISDIR(b->dl_mode) != 0) ? 1 : -1;
112 return strcmp(a->dl_name, b->dl_name);
115 static char buffer[2*1024 > sizeof(STYLE_STR) ? 2*1024 : sizeof(STYLE_STR)];
116 static char *dst = buffer;
118 BUFFER_SIZE = sizeof(buffer),
122 /* After this call, you have at least size + HEADROOM bytes available
124 static void guarantee(int size)
126 if (buffer + (BUFFER_SIZE-HEADROOM) - dst >= size)
128 write(STDOUT_FILENO, buffer, dst - buffer);
132 /* NB: formatters do not store terminating NUL! */
134 /* HEADROOM bytes are available after dst after this call */
135 static void fmt_str(/*char *dst,*/ const char *src)
137 unsigned len = strlen(src);
139 memcpy(dst, src, len);
143 /* HEADROOM bytes after dst are available after this call */
144 static void fmt_url(/*char *dst,*/ const char *name)
147 unsigned c = *name++;
150 if ((c - '0') > 9 /* not a digit */
151 && ((c|0x20) - 'a') > 26 /* not A-Z or a-z */
152 && !strchr("._-+@", c)
155 *dst++ = "0123456789ABCDEF"[c >> 4];
156 *dst = "0123456789ABCDEF"[c & 0xf];
162 /* HEADROOM bytes are available after dst after this call */
163 static void fmt_html(/*char *dst,*/ const char *name)
181 /* HEADROOM bytes are available after dst after this call */
182 static void fmt_ull(/*char *dst,*/ unsigned long long n)
184 char buf[sizeof(n)*3 + 2];
187 p = buf + sizeof(buf) - 1;
190 *--p = (n % 10) + '0';
196 /* Does not call guarantee - eats into headroom instead */
197 static void fmt_02u(/*char *dst,*/ unsigned n)
199 /* n %= 100; - not needed, callers don't pass big n */
200 dst[0] = (n / 10) + '0';
201 dst[1] = (n % 10) + '0';
205 /* Does not call guarantee - eats into headroom instead */
206 static void fmt_04u(/*char *dst,*/ unsigned n)
208 /* n %= 10000; - not needed, callers don't pass big n */
215 dir_list_t *dir_list;
217 unsigned dir_list_count;
219 unsigned count_files;
220 unsigned long long size_total;
225 QUERY_STRING = getenv("QUERY_STRING");
227 || QUERY_STRING[0] != '/'
228 || strstr(QUERY_STRING, "/../")
229 || strcmp(strrchr(QUERY_STRING, '/'), "/..") == 0
235 || (QUERY_STRING[1] && chdir(QUERY_STRING + 1))
252 if (dp->d_name[0] == '.' && !dp->d_name[1])
254 if (stat(dp->d_name, &sb) != 0)
256 dir_list = realloc(dir_list, (dir_list_count + 1) * sizeof(dir_list[0]));
257 dir_list[dir_list_count].dl_name = strdup(dp->d_name);
258 dir_list[dir_list_count].dl_mode = sb.st_mode;
259 dir_list[dir_list_count].dl_size = sb.st_size;
260 dir_list[dir_list_count].dl_mtime = sb.st_mtime;
265 qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
268 "" /* Additional headers (currently none) */
269 "\r\n" /* Mandatory empty line after headers */
270 "<html><head><title>Index of ");
271 /* Guard against directories with &, > etc */
272 fmt_html(QUERY_STRING);
279 fmt_html(QUERY_STRING);
283 "<col class=nm><col class=sz><col class=dt>" "\n"
284 "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
291 while (dir_list_count--) {
294 if (S_ISDIR(cdir->dl_mode)) {
296 } else if (S_ISREG(cdir->dl_mode)) {
298 size_total += cdir->dl_size;
302 fmt_str("<tr class=");
303 *dst++ = (odd ? 'o' : 'e');
304 fmt_str("><td class=nm><a href='");
305 fmt_url(cdir->dl_name); /* %20 etc */
306 if (S_ISDIR(cdir->dl_mode))
309 fmt_html(cdir->dl_name); /* < etc */
310 if (S_ISDIR(cdir->dl_mode))
312 fmt_str("</a><td class=sz>");
313 if (S_ISREG(cdir->dl_mode))
314 fmt_ull(cdir->dl_size);
315 fmt_str("<td class=dt>");
316 tm = gmtime(&cdir->dl_mtime);
317 fmt_04u(1900 + tm->tm_year); *dst++ = '-';
318 fmt_02u(tm->tm_mon + 1); *dst++ = '-';
319 fmt_02u(tm->tm_mday); *dst++ = ' ';
320 fmt_02u(tm->tm_hour); *dst++ = ':';
321 fmt_02u(tm->tm_min); *dst++ = ':';
330 fmt_str("<tr class=foot><th class=cnt>Files: ");
331 fmt_ull(count_files);
332 /* count_dirs - 1: we don't want to count ".." */
333 fmt_str(", directories: ");
334 fmt_ull(count_dirs - 1);
335 fmt_str("<th class=sz>");
337 fmt_str("<th class=dt>\n");
338 /* "</table></body></html>" - why bother? */
339 guarantee(BUFFER_SIZE * 2); /* flush */