1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#define _GNU_SOURCE 1
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <errno.h>
42#include <stdint.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46#include <stdio.h>
47#include <dirent.h>
48#include <time.h>
49
50
51
52
53
54#define STYLE_STR \
55"<style>" \
56"table {" \
57 "width:100%;" \
58 "background-color:#fff5ee;" \
59 "border-width:1px;" \
60 "border-spacing:2px;" \
61 "border-style:solid;" \
62 "border-color:black;" \
63 "border-collapse:collapse" \
64"}" \
65"th {" \
66 "border-width:1px;" \
67 "padding:1px;" \
68 "border-style:solid;" \
69 "border-color:black" \
70"}" \
71"td {" \
72 \
73 "border-width:0 1px 0 1px;" \
74 "padding:1px;" \
75 "border-style:solid;" \
76 "border-color:black;" \
77 "white-space:nowrap" \
78"}" \
79"tr:nth-child(odd) { background-color:#ffffff }" \
80"tr.hdr { background-color:#eee5de }" \
81"tr.foot { background-color:#eee5de }" \
82"th.cnt { text-align:left }" \
83"th.sz { text-align:right }" \
84"th.dt { text-align:right }" \
85"td.sz { text-align:right }" \
86"td.dt { text-align:right }" \
87"col.nm { width:98% }" \
88"col.sz { width:1% }" \
89"col.dt { width:1% }" \
90"</style>" \
91
92typedef struct dir_list_t {
93 char *dl_name;
94 mode_t dl_mode;
95 off_t dl_size;
96 time_t dl_mtime;
97} dir_list_t;
98
99static int compare_dl(dir_list_t *a, dir_list_t *b)
100{
101
102 if (strcmp(a->dl_name, "..") == 0) {
103 return -1;
104 }
105 if (strcmp(b->dl_name, "..") == 0) {
106 return 1;
107 }
108 if (S_ISDIR(a->dl_mode) != S_ISDIR(b->dl_mode)) {
109
110
111 return (S_ISDIR(b->dl_mode) != 0) ? 1 : -1;
112 }
113 return strcmp(a->dl_name, b->dl_name);
114}
115
116static char buffer[2*1024 > sizeof(STYLE_STR) ? 2*1024 : sizeof(STYLE_STR)];
117static char *dst = buffer;
118enum {
119 BUFFER_SIZE = sizeof(buffer),
120 HEADROOM = 64,
121};
122
123
124
125static void guarantee(int size)
126{
127 if (buffer + (BUFFER_SIZE-HEADROOM) - dst >= size)
128 return;
129 write(STDOUT_FILENO, buffer, dst - buffer);
130 dst = buffer;
131}
132
133
134
135
136static void fmt_str( const char *src)
137{
138 unsigned len = strlen(src);
139 guarantee(len);
140 memcpy(dst, src, len);
141 dst += len;
142}
143
144
145static void fmt_url( const char *name)
146{
147 while (*name) {
148 unsigned c = *name++;
149 guarantee(3);
150 *dst = c;
151 if ((c - '0') > 9
152 && ((c|0x20) - 'a') > ('z' - 'a')
153 && !strchr("._-+@", c)
154 ) {
155 *dst++ = '%';
156 *dst++ = "0123456789ABCDEF"[c >> 4];
157 *dst = "0123456789ABCDEF"[c & 0xf];
158 }
159 dst++;
160 }
161}
162
163
164static void fmt_html( const char *name)
165{
166 while (*name) {
167 char c = *name++;
168 if (c == '<')
169 fmt_str("<");
170 else if (c == '>')
171 fmt_str(">");
172 else if (c == '&') {
173 fmt_str("&");
174 } else {
175 guarantee(1);
176 *dst++ = c;
177 continue;
178 }
179 }
180}
181
182
183static void fmt_ull( unsigned long long n)
184{
185 char buf[sizeof(n)*3 + 2];
186 char *p;
187
188 p = buf + sizeof(buf) - 1;
189 *p = '\0';
190 do {
191 *--p = (n % 10) + '0';
192 n /= 10;
193 } while (n);
194 fmt_str( p);
195}
196
197
198static void fmt_02u( unsigned n)
199{
200
201 dst[0] = (n / 10) + '0';
202 dst[1] = (n % 10) + '0';
203 dst += 2;
204}
205
206
207static void fmt_04u( unsigned n)
208{
209
210 fmt_02u(n / 100);
211 fmt_02u(n % 100);
212}
213
214int main(int argc, char *argv[])
215{
216 dir_list_t *dir_list;
217 dir_list_t *cdir;
218 unsigned dir_list_count;
219 unsigned count_dirs;
220 unsigned count_files;
221 unsigned long long size_total;
222 DIR *dirp;
223 char *location;
224
225 location = getenv("REQUEST_URI");
226 if (!location)
227 return 1;
228
229
230 strchrnul(location, '?')[0] = '\0';
231
232 if (location[0] != '/'
233 || strstr(location, "//")
234 || strstr(location, "/../")
235 || strcmp(strrchr(location, '/'), "/..") == 0
236 ) {
237 return 1;
238 }
239
240 if (chdir("..")
241 || (location[1] && chdir(location + 1))
242 ) {
243 return 1;
244 }
245
246 dirp = opendir(".");
247 if (!dirp)
248 return 1;
249 dir_list = NULL;
250 dir_list_count = 0;
251 while (1) {
252 struct dirent *dp;
253 struct stat sb;
254
255 dp = readdir(dirp);
256 if (!dp)
257 break;
258 if (dp->d_name[0] == '.' && !dp->d_name[1])
259 continue;
260 if (stat(dp->d_name, &sb) != 0)
261 continue;
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;
267 dir_list_count++;
268 }
269 closedir(dirp);
270
271 qsort(dir_list, dir_list_count, sizeof(dir_list[0]), (void*)compare_dl);
272
273 fmt_str(
274 ""
275 "\r\n"
276 "<html><head><title>Index of ");
277
278 fmt_html(location);
279 fmt_str(
280 "</title>\n"
281 STYLE_STR
282 "</head>" "\n"
283 "<body>" "\n"
284 "<h1>Index of ");
285 fmt_html(location);
286 fmt_str(
287 "</h1>" "\n"
288 "<table>" "\n"
289 "<col class=nm><col class=sz><col class=dt>" "\n"
290 "<tr class=hdr><th class=cnt>Name<th class=sz>Size<th class=dt>Last modified" "\n");
291
292 count_dirs = 0;
293 count_files = 0;
294 size_total = 0;
295 cdir = dir_list;
296 while (dir_list_count--) {
297 struct tm *ptm;
298
299 if (S_ISDIR(cdir->dl_mode)) {
300 count_dirs++;
301 } else if (S_ISREG(cdir->dl_mode)) {
302 count_files++;
303 size_total += cdir->dl_size;
304 } else
305 goto next;
306
307 fmt_str("<tr><td class=nm><a href='");
308 fmt_url(cdir->dl_name);
309 if (S_ISDIR(cdir->dl_mode))
310 *dst++ = '/';
311 fmt_str("'>");
312 fmt_html(cdir->dl_name);
313 if (S_ISDIR(cdir->dl_mode))
314 *dst++ = '/';
315 fmt_str("</a><td class=sz>");
316 if (S_ISREG(cdir->dl_mode))
317 fmt_ull(cdir->dl_size);
318 fmt_str("<td class=dt>");
319 ptm = gmtime(&cdir->dl_mtime);
320 fmt_04u(1900 + ptm->tm_year); *dst++ = '-';
321 fmt_02u(ptm->tm_mon + 1); *dst++ = '-';
322 fmt_02u(ptm->tm_mday); *dst++ = ' ';
323 fmt_02u(ptm->tm_hour); *dst++ = ':';
324 fmt_02u(ptm->tm_min); *dst++ = ':';
325 fmt_02u(ptm->tm_sec);
326 *dst++ = '\n';
327
328 next:
329 cdir++;
330 }
331
332 fmt_str("<tr class=foot><th class=cnt>Files: ");
333 fmt_ull(count_files);
334
335 fmt_str(", directories: ");
336 fmt_ull(count_dirs - 1);
337 fmt_str("<th class=sz>");
338 fmt_ull(size_total);
339 fmt_str("<th class=dt>\n");
340
341 guarantee(BUFFER_SIZE * 2);
342
343 return 0;
344}
345