No empty .Rs/.Re
[netbsd-mini2440.git] / libexec / httpd / dir-index-bozo.c
blob5afaa24333d90532acb49c375e7ef19bffe73927
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 $ */
5 /*
6 * Copyright (c) 1997-2009 Matthew R. Green
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
29 * SUCH DAMAGE.
33 /* this code implements directory index generation for bozohttpd */
35 #ifndef NO_DIRINDEX_SUPPORT
37 #include <sys/param.h>
39 #include <dirent.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <assert.h>
46 #include "bozohttpd.h"
48 int Xflag; /* do directory indexing */
49 int Hflag; /* hide .* */
51 static void
52 directory_hr(void)
55 bozoprintf("<hr noshade align=\"left\" width=\"80%%\">\r\n\r\n");
59 * output a directory index. return 1 if it actually did something..
61 int
62 directory_index(http_req *request, const char *dirname, int isindex)
64 struct stat sb;
65 struct dirent *de;
66 struct tm *tm;
67 DIR *dp;
68 char buf[MAXPATHLEN];
69 char spacebuf[48];
70 char *file = NULL;
71 int l, i;
73 if (!isindex || !Xflag)
74 return 0;
76 if (strlen(dirname) <= strlen(index_html))
77 dirname = ".";
78 else {
79 file = bozostrdup(dirname);
81 file[strlen(file) - strlen(index_html)] = '\0';
82 dirname = file;
84 debug((DEBUG_FAT, "directory_index: dirname ``%s''", dirname));
85 if (stat(dirname, &sb) < 0 ||
86 (dp = opendir(dirname)) == NULL) {
87 if (errno == EPERM)
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");
92 else
93 (void)http_error(500, request, "open directory");
94 goto done;
95 /* NOTREACHED */
98 bozoprintf("%s 200 OK\r\n", request->hr_proto);
100 if (request->hr_proto != http_09) {
101 print_header(request, NULL, "text/html", "");
102 bozoprintf("\r\n");
104 bozoflush(stdout);
106 if (request->hr_method == HTTP_HEAD) {
107 closedir(dp);
108 goto done;
111 bozoprintf("<html><head><title>Index of %s</title></head>\r\n",
112 request->hr_file);
113 bozoprintf("<body><h1>Index of %s</h1>\r\n", request->hr_file);
114 bozoprintf("<pre>\r\n");
115 #define NAMELEN 40
116 #define LMODLEN 19
117 bozoprintf("Name "
118 "Last modified "
119 "Size\n");
120 bozoprintf("</pre>");
121 directory_hr();
122 bozoprintf("<pre>");
124 while ((de = readdir(dp)) != NULL) {
125 int nostat = 0;
126 char *name = de->d_name;
128 if (strcmp(name, ".") == 0 ||
129 (strcmp(name, "..") != 0 && Hflag && name[0] == '.'))
130 continue;
132 snprintf(buf, sizeof buf, "%s/%s", dirname, name);
133 if (stat(buf, &sb))
134 nostat = 1;
136 l = 0;
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);
144 } else {
145 bozoprintf("<a href=\"%s\">", name);
146 l += bozoprintf("%s", name);
148 bozoprintf("</a>");
150 /* NAMELEN spaces */
151 assert(sizeof(spacebuf) > NAMELEN);
152 i = (l < NAMELEN) ? (NAMELEN - l) : 0;
153 i++;
154 memset(spacebuf, ' ', i);
155 spacebuf[i] = '\0';
156 bozoprintf(spacebuf);
157 l += i;
159 if (nostat)
160 bozoprintf("? ?");
161 else {
162 tm = gmtime(&sb.st_mtime);
163 strftime(buf, sizeof buf, "%d-%b-%Y %R", tm);
164 l += bozoprintf("%s", buf);
166 /* LMODLEN spaces */
167 assert(sizeof(spacebuf) > LMODLEN);
168 i = (l < (LMODLEN+NAMELEN+1)) ? ((LMODLEN+NAMELEN+1) - l) : 0;
169 i++;
170 memset(spacebuf, ' ', i);
171 spacebuf[i] = '\0';
172 bozoprintf(spacebuf);
174 bozoprintf("%7ukB",
175 ((unsigned int)(sb.st_size >> 10)));
177 bozoprintf("\r\n");
180 closedir(dp);
181 bozoprintf("</pre>");
182 directory_hr();
183 bozoprintf("</body></html>\r\n\r\n");
184 bozoflush(stdout);
186 done:
187 if (file)
188 free(file);
189 return 1;
191 #endif /* NO_DIRINDEX_SUPPORT */