1 /* $NetBSD: dir-index-bozo.c,v 1.6 2009/04/18 07:28:24 mrg Exp $ */
3 /* $eterna: dir-index-bozo.c,v 1.14 2009/04/18 01:48:18 mrg Exp $ */
6 * Copyright (c) 1997-2009 Matthew R. Green
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer and
16 * dedication in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 /* this code implements directory index generation for bozohttpd */
35 #ifndef NO_DIRINDEX_SUPPORT
37 #include <sys/param.h>
46 #include "bozohttpd.h"
48 int Xflag
; /* do directory indexing */
49 int Hflag
; /* hide .* */
55 bozoprintf("<hr noshade align=\"left\" width=\"80%%\">\r\n\r\n");
59 * output a directory index. return 1 if it actually did something..
62 directory_index(http_req
*request
, const char *dirname
, int isindex
)
73 if (!isindex
|| !Xflag
)
76 if (strlen(dirname
) <= strlen(index_html
))
79 file
= bozostrdup(dirname
);
81 file
[strlen(file
) - strlen(index_html
)] = '\0';
84 debug((DEBUG_FAT
, "directory_index: dirname ``%s''", dirname
));
85 if (stat(dirname
, &sb
) < 0 ||
86 (dp
= opendir(dirname
)) == NULL
) {
88 (void)http_error(403, request
,
89 "no permission to open directory");
90 else if (errno
== ENOENT
)
91 (void)http_error(404, request
, "no file");
93 (void)http_error(500, request
, "open directory");
98 bozoprintf("%s 200 OK\r\n", request
->hr_proto
);
100 if (request
->hr_proto
!= http_09
) {
101 print_header(request
, NULL
, "text/html", "");
106 if (request
->hr_method
== HTTP_HEAD
) {
111 bozoprintf("<html><head><title>Index of %s</title></head>\r\n",
113 bozoprintf("<body><h1>Index of %s</h1>\r\n", request
->hr_file
);
114 bozoprintf("<pre>\r\n");
120 bozoprintf("</pre>");
124 while ((de
= readdir(dp
)) != NULL
) {
126 char *name
= de
->d_name
;
128 if (strcmp(name
, ".") == 0 ||
129 (strcmp(name
, "..") != 0 && Hflag
&& name
[0] == '.'))
132 snprintf(buf
, sizeof buf
, "%s/%s", dirname
, name
);
138 if (strcmp(name
, "..") == 0) {
139 bozoprintf("<a href=\"../\">");
140 l
+= bozoprintf("Parent Directory");
141 } else if (S_ISDIR(sb
.st_mode
)) {
142 bozoprintf("<a href=\"%s/\">", name
);
143 l
+= bozoprintf("%s/", name
);
145 bozoprintf("<a href=\"%s\">", name
);
146 l
+= bozoprintf("%s", name
);
151 assert(sizeof(spacebuf
) > NAMELEN
);
152 i
= (l
< NAMELEN
) ? (NAMELEN
- l
) : 0;
154 memset(spacebuf
, ' ', i
);
156 bozoprintf(spacebuf
);
162 tm
= gmtime(&sb
.st_mtime
);
163 strftime(buf
, sizeof buf
, "%d-%b-%Y %R", tm
);
164 l
+= bozoprintf("%s", buf
);
167 assert(sizeof(spacebuf
) > LMODLEN
);
168 i
= (l
< (LMODLEN
+NAMELEN
+1)) ? ((LMODLEN
+NAMELEN
+1) - l
) : 0;
170 memset(spacebuf
, ' ', i
);
172 bozoprintf(spacebuf
);
175 ((unsigned int)(sb
.st_size
>> 10)));
181 bozoprintf("</pre>");
183 bozoprintf("</body></html>\r\n\r\n");
191 #endif /* NO_DIRINDEX_SUPPORT */