2 Copyright (c) 2003 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 fts_open(char * const *path_argv
, int options
,
129 int (*compar
)(const FTSENT
**, const FTSENT
**))
136 if(options
!= FTS_LOGICAL
|| compar
!= NULL
|| path_argv
[1] != NULL
) {
141 dir
= opendir(path_argv
[0]);
145 fts
= calloc(sizeof(FTS
), 1);
162 rc
= fchdir(dirfd(dir
));
175 fts
->cwd
= strdup(path_argv
[0]);
185 if(fts
->ftsent
.fts_path
) {
186 free(fts
->ftsent
.fts_path
);
187 fts
->ftsent
.fts_path
= NULL
;
195 rc
= chdir(fts
->cwd0
);
199 while(fts
->depth
>= 0) {
200 closedir(fts
->dir
[fts
->depth
]);
205 if(fts
->cwd
) free(fts
->cwd
);
218 struct dirent
*dirent
;
223 if(fts
->ftsent
.fts_path
) {
224 free(fts
->ftsent
.fts_path
);
225 fts
->ftsent
.fts_path
= NULL
;
234 dirent
= readdir(fts
->dir
[fts
->depth
]);
237 closedir(fts
->dir
[fts
->depth
]);
238 fts
->dir
[fts
->depth
] = NULL
;
240 if(fts
->depth
>= 0) {
241 fts
->dname
= basename_a(fts
->cwd
);
242 if(fts
->dname
== NULL
)
244 newcwd
= dirname_a(fts
->cwd
);
248 if(fts
->cwd
) free(fts
->cwd
);
252 rc
= fchdir(dirfd(fts
->dir
[fts
->depth
]));
259 fts
->ftsent
.fts_info
= FTS_DP
;
263 name
= dirent
->d_name
;
266 rc
= stat(name
, &fts
->stat
);
268 fts
->ftsent
.fts_info
= FTS_NS
;
272 if(S_ISDIR(fts
->stat
.st_mode
)) {
276 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0)
279 if(fts
->depth
>= FTS_MAX_DEPTH
) {
283 dir
= opendir(dirent
->d_name
);
285 if(errno
== EACCES
) {
286 fts
->ftsent
.fts_info
= FTS_DNR
;
291 newcwd
= mkfilename(fts
->cwd
, dirent
->d_name
);
292 rc
= fchdir(dirfd(dir
));
299 fts
->ftsent
.fts_info
= FTS_D
;
301 fts
->dir
[fts
->depth
] = dir
;
303 } else if(S_ISREG(fts
->stat
.st_mode
)) {
304 fts
->ftsent
.fts_info
= FTS_F
;
306 } else if(S_ISLNK(fts
->stat
.st_mode
)) {
308 rc
= readlink(name
, buf
, 1024);
312 errno
= ENAMETOOLONG
;
317 if(access(buf
, F_OK
) >= 0)
319 fts
->ftsent
.fts_info
= FTS_SLNONE
;
322 fts
->ftsent
.fts_info
= FTS_DEFAULT
;
328 fts
->cwd
= getcwd_a();
329 if(fts
->cwd
== NULL
) goto error
;
330 fts
->ftsent
.fts_path
= mkfilename(fts
->cwd
, name
);
331 if(fts
->ftsent
.fts_path
== NULL
) goto error
;
332 fts
->ftsent
.fts_accpath
= name
;
333 fts
->ftsent
.fts_statp
= &fts
->stat
;
337 fts
->ftsent
.fts_info
= FTS_ERR
;
339 fts
->ftsent
.fts_errno
= errno
;