-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex_dir.c
355 lines (308 loc) · 9.48 KB
/
index_dir.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (C) 1997-2002 Jon Nelson <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* $Id: index_dir.c,v 1.32 2002/01/30 03:41:45 jnelson Exp $*/
#include <stdio.h>
#include <sys/stat.h>
#include <limits.h> /* for PATH_MAX */
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "compat.h"
#define MAX_FILE_LENGTH MAXNAMLEN
#define MAX_PATH_LENGTH PATH_MAX
#define INT_TO_HEX(x) \
((((x)-10)>=0)?('A'+((x)-10)):('0'+(x)))
#include "escape.h"
char *html_escape_string(char *inp, char *buf, const int len);
char *http_escape_string(char *inp, char *buf, const int len);
#if defined __GNUC__ && \
(__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 0))
#define CONST const
#else
#define CONST
#endif
int select_files(CONST struct dirent *d);
int index_directory(char *dir, char *title);
/*
* Name: html_escape_string
*/
char *html_escape_string(char *inp, char *dest, const int len)
{
int max;
char *buf;
unsigned char c;
max = len * 5;
if (dest == NULL && max)
dest = malloc(sizeof (unsigned char) * (max + 1));
if (dest == NULL)
return NULL;
buf = dest;
while ((c = *inp++)) {
switch (c) {
case '>':
*dest++ = '&';
*dest++ = 'g';
*dest++ = 't';
*dest++ = ';';
break;
case '<':
*dest++ = '&';
*dest++ = 'l';
*dest++ = 't';
*dest++ = ';';
break;
case '&':
*dest++ = '&';
*dest++ = 'a';
*dest++ = 'm';
*dest++ = 'p';
*dest++ = ';';
break;
default:
*dest++ = c;
}
}
*dest = '\0';
return buf;
}
/*
* Name: escape_string
*
* Description: escapes the string inp. Uses variable buf. If buf is
* NULL when the program starts, it will attempt to dynamically allocate
* the space that it needs, otherwise it will assume that the user
* has already allocated enough space for the variable buf, which
* could be up to 3 times the size of inp. If the routine dynamically
* allocates the space, the user is responsible for freeing it afterwords
* Returns: NULL on error, pointer to string otherwise.
*/
char *http_escape_string(char *inp, char *buf, const int len)
{
int max;
char *index;
unsigned char c;
max = len * 3;
if (buf == NULL && max)
buf = malloc(sizeof (unsigned char) * (max + 1));
if (buf == NULL)
return NULL;
index = buf;
while ((c = *inp++)) {
if (needs_escape((unsigned int) c)) {
*index++ = '%';
*index++ = INT_TO_HEX(c >> 4);
*index++ = INT_TO_HEX(c & 15);
} else
*index++ = c;
}
*index = '\0';
return buf;
}
void send_error(int error)
{
char *the_error;
switch (error) {
case 1:
the_error = "Not enough arguments were passed to the indexer.";
break;
case 2:
the_error = "The Directory Sorter ran out of Memory";
break;
case 3:
the_error =
"The was a problem changing to the appropriate directory.";
break;
case 4:
the_error = "There was an error escaping a string.";
case 5:
the_error = "Too many arguments were passed to the indexer.";
break;
case 6:
the_error = "No files in this directory.";
break;
default:
the_error = "An unknown error occurred producing the directory.";
break;
}
printf("<html>\n<head>\n<title>\n%s\n</title>\n"
"<body>\n%s\n</body>\n</html>\n", the_error, the_error);
}
int select_files(CONST struct dirent *dirbuf)
{
if (dirbuf->d_name[0] == '.')
return 0;
else
return 1;
}
/*
* Name: index_directory
* Description: Called from get_dir_mapping if a directory html
* has to be generated on the fly
* If no_slash is true, prepend slashes to hrefs
* returns -1 for problem, else 0
*/
int index_directory(char *dir, char *title)
{
struct dirent *dirbuf;
int numdir;
struct dirent **array;
struct stat statbuf;
char http_filename[MAX_FILE_LENGTH * 3];
char html_filename[MAX_FILE_LENGTH * 4];
int i;
if (chdir(dir) == -1) {
send_error(3);
return -1;
}
numdir = scandir(".", &array, select_files, alphasort);
if (numdir == -1) {
send_error(2);
return -1;
} else if (numdir == -2) {
send_error(6);
return -1;
}
printf("<html>\n"
"<head>\n<title>Index of %s</title>\n</head>\n\n"
"<body bgcolor=\"#ffffff\">\n"
"<H2>Index of %s</H2>\n"
"<table>\n%s",
title, title,
(strcmp(title, "/") == 0 ? "" :
"<tr><td colspan=3><h3>Directories</h3></td></tr>"
"<tr><td colspan=3><a href=\"../\">Parent Directory</a></td></tr>\n"));
for (i = 0; i < numdir; ++i) {
dirbuf = array[i];
if (stat(dirbuf->d_name, &statbuf) == -1)
continue;
if (!S_ISDIR(statbuf.st_mode))
continue;
if (html_escape_string(dirbuf->d_name, html_filename,
NAMLEN(dirbuf)) == NULL) {
send_error(4);
return -1;
}
if (http_escape_string(dirbuf->d_name, http_filename,
NAMLEN(dirbuf)) == NULL) {
send_error(4);
return -1;
}
printf("<tr>"
"<td width=\"40%%\"><a href=\"%s/\">%s/</a></td>"
"<td align=right>%s</td>"
"<td align=right>%ld bytes</td>"
"</tr>\n",
http_filename, html_filename,
ctime(&statbuf.st_mtime), (long) statbuf.st_size);
}
printf
("<tr><td colspan=3> </td></tr>\n<tr><td colspan=3><h3>Files</h3></td></tr>\n");
for (i = 0; i < numdir; ++i) {
int len;
dirbuf = array[i];
if (stat(dirbuf->d_name, &statbuf) == -1)
continue;
if (S_ISDIR(statbuf.st_mode))
continue;
if (html_escape_string(dirbuf->d_name, html_filename,
NAMLEN(dirbuf)) == NULL) {
send_error(4);
return -1;
}
if (http_escape_string(dirbuf->d_name, http_filename,
NAMLEN(dirbuf)) == NULL) {
send_error(4);
return -1;
}
len = strlen(http_filename);
#ifdef GUNZIP
if (len > 3 && !memcmp(http_filename + len - 3, ".gz", 3)) {
http_filename[len - 3] = '\0';
html_filename[strlen(html_filename) - 3] = '\0';
printf("<tr>"
"<td width=\"40%%\"><a href=\"%s\">%s</a> "
"<a href=\"%s.gz\">(.gz)</a></td>"
"<td align=right>%s</td>"
"<td align=right>%ld bytes</td>"
"</tr>\n",
http_filename, html_filename, http_filename,
ctime(&statbuf.st_mtime), (long) statbuf.st_size);
} else {
#endif
printf("<tr>"
"<td width=\"40%%\"><a href=\"%s\">%s</a></td>"
"<td align=right>%s</td>"
"<td align=right>%ld bytes</td>"
"</tr>\n",
http_filename, html_filename,
ctime(&statbuf.st_mtime), (long) statbuf.st_size);
#ifdef GUNZIP
}
#endif
}
/* hey -- even though this is a one-shot deal, we should
* still free memory we ought to free
* You never know -- this code might get used elsewhere!
*/
for (i = 0; i < numdir; ++i) {
free(array[i]);
array[i] = NULL;
}
free(array);
array = NULL;
return 0; /* success */
}
int main(int argc, char *argv[])
{
time_t timep;
struct tm *timeptr;
char *now;
if (argc < 3) {
send_error(1);
return -1;
} else if (argc > 3) {
send_error(5);
return -1;
}
build_needs_escape();
if (argv[2] == NULL)
index_directory(argv[1], argv[1]);
else
index_directory(argv[1], argv[2]);
time(&timep);
#ifdef USE_LOCALTIME
timeptr = localtime(&timep);
#else
timeptr = gmtime(&timep);
#endif
now = strdup(asctime(timeptr));
now[strlen(now) - 1] = '\0';
#ifdef USE_LOCALTIME
printf("</table>\n<hr noshade>\nIndex generated %s %s\n"
"<!-- This program is part of the Boa Webserver Copyright (C) 1991-2002 http://www.boa.org -->\n"
"</body>\n</html>\n", now, TIMEZONE(timeptr));
#else
printf("</table>\n<hr noshade>\nIndex generated %s UTC\n"
"<!-- This program is part of the Boa Webserver Copyright (C) 1991-2002 http://www.boa.org -->\n"
"</body>\n</html>\n", now);
#endif
return 0;
}