2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 /* This file implements just enough of the fts family of functions
24 to make Polipo happy. */
29 #include <sys/types.h>
35 #include "fts_compat.h"
42 ret
= getcwd(buf
, 256);
49 mkfilename(const char *path
, char *filename
)
52 char *buf
= malloc(n
+ 1 + strlen(filename
) + 1);
58 strcpy(buf
+ n
, filename
);
63 split(const char *path
, int *slash_return
, int *dlen
, int *blen
)
67 while(len
> 0 && path
[len
- 1] == '/')
72 while(slash
>= 0 && path
[slash
] != '/')
75 if(slash_return
) *slash_return
= slash
;
76 if(dlen
) *dlen
= slash
+ 1;
77 if(blen
) *blen
= len
- slash
- 1;
82 basename_a(const char *path
)
88 rc
= split(path
, &slash
, NULL
, &blen
);
89 if(rc
< 0 || blen
== 0)
95 memcpy(b
, path
+ slash
+ 1, blen
);
101 dirname_a(const char *path
)
107 rc
= split(path
, NULL
, &dlen
, NULL
);
111 d
= malloc(dlen
+ 1);
114 memcpy(d
, path
, dlen
);
119 #if defined(__svr4__) || defined(SVR4)
128 * Make the directory identified by the argument the current directory.
132 change_to_dir(DIR *dir
)
139 change_to_dir(DIR *dir
)
141 return fchdir(dirfd(dir
));
146 fts_open(char * const *path_argv
, int options
,
147 int (*compar
)(const FTSENT
**, const FTSENT
**))
154 if(options
!= FTS_LOGICAL
|| compar
!= NULL
|| path_argv
[1] != NULL
) {
159 dir
= opendir(path_argv
[0]);
163 fts
= calloc(sizeof(FTS
), 1);
180 rc
= change_to_dir(dir
);
193 fts
->cwd
= strdup(path_argv
[0]);
203 if(fts
->ftsent
.fts_path
) {
204 free(fts
->ftsent
.fts_path
);
205 fts
->ftsent
.fts_path
= NULL
;
213 rc
= chdir(fts
->cwd0
);
217 while(fts
->depth
>= 0) {
218 closedir(fts
->dir
[fts
->depth
]);
223 if(fts
->cwd
) free(fts
->cwd
);
236 struct dirent
*dirent
;
241 if(fts
->ftsent
.fts_path
) {
242 free(fts
->ftsent
.fts_path
);
243 fts
->ftsent
.fts_path
= NULL
;
252 dirent
= readdir(fts
->dir
[fts
->depth
]);
255 closedir(fts
->dir
[fts
->depth
]);
256 fts
->dir
[fts
->depth
] = NULL
;
258 if(fts
->depth
>= 0) {
259 fts
->dname
= basename_a(fts
->cwd
);
260 if(fts
->dname
== NULL
)
262 newcwd
= dirname_a(fts
->cwd
);
266 if(fts
->cwd
) free(fts
->cwd
);
270 rc
= change_to_dir(fts
->dir
[fts
->depth
]);
277 fts
->ftsent
.fts_info
= FTS_DP
;
281 name
= dirent
->d_name
;
284 rc
= stat(name
, &fts
->stat
);
286 fts
->ftsent
.fts_info
= FTS_NS
;
290 if(S_ISDIR(fts
->stat
.st_mode
)) {
294 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0)
297 if(fts
->depth
>= FTS_MAX_DEPTH
) {
301 dir
= opendir(dirent
->d_name
);
303 if(errno
== EACCES
) {
304 fts
->ftsent
.fts_info
= FTS_DNR
;
309 newcwd
= mkfilename(fts
->cwd
, dirent
->d_name
);
310 rc
= change_to_dir(dir
);
317 fts
->ftsent
.fts_info
= FTS_D
;
319 fts
->dir
[fts
->depth
] = dir
;
321 } else if(S_ISREG(fts
->stat
.st_mode
)) {
322 fts
->ftsent
.fts_info
= FTS_F
;
325 } else if(S_ISLNK(fts
->stat
.st_mode
)) {
327 rc
= readlink(name
, buf
, 1024);
331 errno
= ENAMETOOLONG
;
336 if(access(buf
, F_OK
) >= 0)
338 fts
->ftsent
.fts_info
= FTS_SLNONE
;
342 fts
->ftsent
.fts_info
= FTS_DEFAULT
;
348 fts
->cwd
= getcwd_a();
349 if(fts
->cwd
== NULL
) goto error
;
350 fts
->ftsent
.fts_path
= mkfilename(fts
->cwd
, name
);
351 if(fts
->ftsent
.fts_path
== NULL
) goto error
;
352 fts
->ftsent
.fts_accpath
= name
;
353 fts
->ftsent
.fts_statp
= &fts
->stat
;
357 fts
->ftsent
.fts_info
= FTS_ERR
;
359 fts
->ftsent
.fts_errno
= errno
;