/
dlist.c
304 lines (246 loc) · 6.15 KB
/
dlist.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
#include <sys/stat.h>
#include <sys/types.h>
#include <bsd/string.h>
#include <bsd/stdlib.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "global.h"
#include "dlist.h"
void
genDirList(struct http_request *req)
{
char dir[PATH_MAX];
int file_count, i;
struct file_st *file_array;
struct stat buf;
if ( (req->path[0]!='.') && (req->path[0]!='/') ) {
strcpy(dir,"./");
} else if (req->path[0]=='/') {
strcpy(dir,".");
}
strcat(dir,req->path);
if (lstat(dir,&buf)<0) {
err(EXIT_FAILURE, "cannot access %s", dir);
}
if((file_count = getList(dir, &file_array))<1) {
return;
}
sortList(dir, &file_array, file_count);
genListings(&file_array, file_count);
printListings(&file_array, file_count, req);
/* free memory and potentially make a recursive call */
for (i=0; i<file_count; i++) {
free(file_array[i].file_name);
free(file_array[i].full_path);
free(file_array[i].listing);
}
free(file_array);
return;
}
int
fileFilter(char *file_name)
{
if (strncmp(file_name, ".", 1)==0) {
return 0; /* fail - hidden files start with '.' */
} else {
return 1;
}
}
void
genListings(struct file_st **file_array_ptr, int count)
{
struct file_st *farr = *file_array_ptr;
char **file_listing, tempstr[PATH_MAX];
int i, j;
if ((file_listing = malloc(count*sizeof(char *)))==NULL) {
err(EXIT_FAILURE, "out of memory");
}
for (i=0; i<count; i++)
{
if ((file_listing[i]=malloc(sizeof(char)*PATH_MAX))==NULL) {
err(EXIT_FAILURE, "out of memory");
}
strcpy(file_listing[i],"\0");
}
for (i=0; i<count; i++)
{
strcpy(tempstr, farr[i].file_name);
for (j=0; j<strlen(tempstr); j++)
{
if ( tempstr[j]<32 || tempstr[j]>126 ) {
tempstr[j] = '?';
}
}
strcat(file_listing[i],tempstr);
}
/* Finally, allocate memory and add the listing to the actual
* file structure.
*/
for (i=0; i<count; i++)
{
if ((farr[i].listing
=malloc((strlen(file_listing[i])+1)*sizeof(char)))
==NULL) {
err(EXIT_FAILURE, "out of memory");
}
strcpy(farr[i].listing, file_listing[i]);
free(file_listing[i]);
}
return;
}
int
getList(char *dir_name, struct file_st **file_array_ptr)
{
int first_count=0;
int count=0;
DIR *dp;
struct dirent *dirp;
struct file_st *file_array;
if ((dp = opendir(dir_name)) == NULL) {
warn("cannot open directory %s",dir_name);
return -1;
}
while ( (dirp=readdir(dp)) != NULL) {
if ((fileFilter(dirp->d_name))==0) {
continue;
}
first_count++;
}
if ((closedir(dp))<0) {
err(EXIT_FAILURE,"cannot close directory %s",dir_name);
}
if ((*file_array_ptr=malloc(first_count*sizeof(struct file_st)))
==NULL) {
err(EXIT_FAILURE, "out of memory");
}
file_array = *file_array_ptr;
if ((dp = opendir(dir_name)) == NULL) {
warn("cannot open directory %s",dir_name);
return -1;
}
while ( (dirp=readdir(dp)) != NULL) {
if ((fileFilter(dirp->d_name))==0) {
continue;
}
if ((file_array[count].file_name
=malloc((strlen(dirp->d_name)+1)*sizeof(char)))==NULL) {
err(EXIT_FAILURE, "out of memory");
}
strcpy(file_array[count].file_name,dirp->d_name);
if ( (count+1) > first_count ) {
break;
}
count++;
}
if ((closedir(dp))<0) {
err(EXIT_FAILURE,"cannot close directory %s",dir_name);
}
return count;
}
int
masterCmp(const void *fs1, const void *fs2)
{
int return_val;
struct file_st *filest1 = (struct file_st *) fs1;
struct file_st *filest2 = (struct file_st *) fs2;
return_val = strcasecmp(filest1->file_name, filest2->file_name);
return return_val;
}
void
printListings(struct file_st **file_array_ptr, int count, struct http_request *req)
{
struct file_st *farr = *file_array_ptr;
char write_buf[BUF_SIZE];
int i, bytes_sent;
req->content_size=0;
strcpy(write_buf,"<!DOCTYPE HTML>\n");
strcat(write_buf,"<html>\n");
strcat(write_buf,"<head>\n");
strcat(write_buf,"<title>ERROR</title>\n");
strcat(write_buf,"</head>\n");
strcat(write_buf,"<body>\n");
strcat(write_buf,"<h1>Index</h1>\n");
strcat(write_buf,"<hr />\n");
if ( (bytes_sent=write( req->pipefd[1],
write_buf,
strlen(write_buf))) < 0 ) {
err(EXIT_FAILURE,"cannot write to pipe");
}
req->content_size+=bytes_sent;
if ( strcmp(req->path,"/")!=0 ) {
bzero(write_buf,sizeof(write_buf));
sprintf(write_buf,"<p><a href = \"../\">parent</a></p>\n");
strcat(write_buf,"<hr />\n");
if ( (bytes_sent=write( req->pipefd[1],
write_buf,
strlen(write_buf))) < 0 ) {
err(EXIT_FAILURE,"cannot write to pipe");
}
req->content_size+=bytes_sent;
}
for (i=0; i<count; i++)
{
bzero(write_buf,sizeof(write_buf));
sprintf( write_buf,
"<p><a href = \"%s\">%s</a></p>\n",
farr[i].listing,
farr[i].listing );
if ( (bytes_sent=write( req->pipefd[1],
write_buf,
strlen(write_buf))) < 0 ) {
err(EXIT_FAILURE,"cannot write to pipe");
}
req->content_size+=bytes_sent;
}
bzero(write_buf,sizeof(write_buf));
strcpy(write_buf,"</body>\n");
strcat(write_buf,"</html>\n");
if ( (bytes_sent=write( req->pipefd[1],
write_buf,
strlen(write_buf))) < 0 ) {
err(EXIT_FAILURE,"cannot write to pipe");
}
req->content_size+=bytes_sent;
if ( close(req->pipefd[1]) < 0 ) {
err(EXIT_FAILURE,"cannot close pipe");
}
return;
}
void
sortList(char *dir, struct file_st **file_array_ptr, int count)
{
int i;
struct file_st *file_array = *file_array_ptr;
char temp[PATH_MAX];
for (i=0; i<count; i++)
{
if ( dir[strlen(dir)-1] == '/') {
strcpy(temp,dir);
strcat(temp,file_array[i].file_name);
} else {
strcpy(temp,dir);
strcat(temp, "/");
strcat(temp,file_array[i].file_name);
}
if ((file_array[i].full_path
=malloc((strlen(temp)+1)*sizeof(char)))
==NULL) {
err(EXIT_FAILURE, "out of memory");
}
strcpy(file_array[i].full_path,temp);
if ((lstat(file_array[i].full_path,
&file_array[i].file_stat))<0) {
warn("cannot stat %s",file_array[i].full_path);
}
}
qsort(file_array, count, sizeof(struct file_st), masterCmp);
return;
}