Changeset 621 in kBuild
- Timestamp:
- Nov 26, 2006 8:14:00 AM (18 years ago)
- Location:
- trunk/src/gmake/kmkbuiltin
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/kmkbuiltin/fts.c
r619 r621 34 34 #endif 35 35 36 #include <sys/cdefs.h> 36 /*#include <sys/cdefs.h>*/ 37 37 #if defined(LIBC_SCCS) && !defined(lint) 38 38 #if 0 … … 43 43 #endif /* LIBC_SCCS and not lint */ 44 44 45 #include "namespace.h" 45 /*#include "namespace.h"*/ 46 #ifndef _MSC_VER 46 47 #include <sys/param.h> 48 #endif 47 49 #include <sys/stat.h> 48 50 … … 51 53 #include <errno.h> 52 54 #include <fcntl.h> 53 #include <fts.h>55 #include "ftsfake.h" 54 56 #include <stdlib.h> 55 57 #include <string.h> 58 #ifndef _MSC_VER 56 59 #include <unistd.h> 60 #else 61 #include "mscfakes.h" 62 #define dirfd(dir) -1 63 #endif 64 #include "ftsfake.h" 57 65 58 66 #if ! HAVE_NBTOOL_CONFIG_H … … 60 68 #endif 61 69 70 #if 0 62 71 #ifdef __weak_alias 63 72 #ifdef __LIBC12_SOURCE__ … … 69 78 #endif /* __LIBC12_SOURCE__ */ 70 79 #endif /* __weak_alias */ 80 #endif 71 81 72 82 #ifdef __LIBC12_SOURCE__ … … 94 104 #endif 95 105 96 static FTSENT *fts_alloc __P((FTS *, const char *, size_t)); 97 static FTSENT *fts_build __P((FTS *, int)); 98 static void fts_lfree __P((FTSENT *)); 99 static void fts_load __P((FTS *, FTSENT *)); 100 static size_t fts_maxarglen __P((char * const *)); 101 static size_t fts_pow2 __P((size_t)); 102 static int fts_palloc __P((FTS *, size_t)); 103 static void fts_padjust __P((FTS *, FTSENT *)); 104 static FTSENT *fts_sort __P((FTS *, FTSENT *, size_t)); 105 static u_short fts_stat __P((FTS *, FTSENT *, int)); 106 static int fts_safe_changedir __P((const FTS *, const FTSENT *, int, 107 const char *)); 106 static FTSENT *fts_alloc(FTS *, const char *, size_t); 107 static FTSENT *fts_build(FTS *, int); 108 static void fts_lfree(FTSENT *); 109 static void fts_load(FTS *, FTSENT *); 110 static size_t fts_maxarglen(char * const *); 111 static size_t fts_pow2(size_t); 112 static int fts_palloc(FTS *, size_t); 113 static void fts_padjust(FTS *, FTSENT *); 114 static FTSENT *fts_sort(FTS *, FTSENT *, size_t); 115 static u_short fts_stat(FTS *, FTSENT *, int); 116 static int fts_safe_changedir(const FTS *, const FTSENT *, int, 117 const char *); 118 119 #ifdef _MSC_VER 120 #undef HAVE_STRUCT_DIRENT_D_NAMLEN 121 #undef HAVE_FCHDIR 122 #endif 123 124 #if defined(__EMX__) || defined(_MSC_VER) 125 # define NEED_STRRSLASH 126 # define IS_SLASH(ch) ( (ch) == '/' || (ch) == '\\' ) 127 #else 128 # define HAVE_FCHDIR 129 # define IS_SLASH(ch) ( (ch) == '/' ) 130 #endif 108 131 109 132 #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) … … 114 137 115 138 #define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path)) 139 #ifdef HAVE_FCHDIR 116 140 #define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) 141 #else 142 #define FCHDIR(sp, rdir) CHDIR(sp, rdir) 143 #endif 144 117 145 118 146 /* fts_build flags */ … … 125 153 #endif 126 154 155 #ifndef _DIAGASSERT 156 #define _DIAGASSERT assert 157 #endif 158 159 127 160 FTS * 128 161 fts_open(argv, options, compar) 129 162 char * const *argv; 130 163 int options; 131 int (*compar) __P((const FTSENT **, const FTSENT **));164 int (*compar)(const FTSENT **, const FTSENT **); 132 165 { 133 166 FTS *sp; … … 225 258 */ 226 259 if (!ISSET(FTS_NOCHDIR)) { 260 #ifdef HAVE_FCHDIR 227 261 if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1) 228 262 SET(FTS_NOCHDIR); … … 231 265 SET(FTS_NOCHDIR); 232 266 } 267 #else 268 if ((sp->fts_rdir = getcwd(NULL, 0)) != NULL) 269 SET(FTS_NOCHDIR); 270 #endif 233 271 } 234 272 … … 242 280 } 243 281 282 #ifdef NEED_STRRSLASH 283 static char *strrslash(register char *psz) 284 { 285 register char ch; 286 char *pszLast = NULL; 287 for (; (ch = *psz); psz++) 288 switch (ch) 289 { 290 case '/': 291 case '\\': 292 case ':': 293 pszLast = psz; 294 break; 295 } 296 return pszLast; 297 } 298 #endif 299 244 300 static void 245 301 fts_load(sp, p) … … 262 318 len = p->fts_pathlen = p->fts_namelen; 263 319 memmove(sp->fts_path, p->fts_name, len + 1); 320 #ifdef NEED_STRRSLASH 321 if ((cp = strrslash(p->fts_name)) && (cp != p->fts_name || cp[1])) { 322 #else 264 323 if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { 324 #endif 265 325 len = strlen(++cp); 266 326 memmove(p->fts_name, cp, len + 1); … … 286 346 */ 287 347 if (sp->fts_cur) { 348 #ifndef _MSC_VER 288 349 if (ISSET(FTS_SYMFOLLOW)) 289 350 (void)close(sp->fts_cur->fts_symfd); 351 #endif 290 352 for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { 291 353 freep = p; … … 305 367 /* Return to original directory, save errno if necessary. */ 306 368 if (!ISSET(FTS_NOCHDIR)) { 369 #ifdef HAVE_FCHDIR 307 370 if (fchdir(sp->fts_rfd)) 308 371 saved_errno = errno; 309 372 (void)close(sp->fts_rfd); 373 #else 374 if (chdir(sp->fts_rdir)) 375 saved_errno = errno; 376 free(sp->fts_rdir); 377 sp->fts_rdir = NULL; 378 #endif 310 379 } 311 380 … … 368 437 p->fts_info = fts_stat(sp, p, 1); 369 438 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 439 #ifdef HAVE_FCHDIR 370 440 if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) { 371 441 p->fts_errno = errno; … … 377 447 } else 378 448 p->fts_flags |= FTS_SYMFOLLOW; 449 #endif 379 450 } 380 451 return (p); … … 386 457 if (instr == FTS_SKIP || 387 458 (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { 459 #ifdef HAVE_FCHDIR 388 460 if (p->fts_flags & FTS_SYMFOLLOW) 389 461 (void)close(p->fts_symfd); 462 #endif 390 463 if (sp->fts_child) { 391 464 fts_lfree(sp->fts_child); … … 443 516 */ 444 517 if (p->fts_level == FTS_ROOTLEVEL) { 518 #ifdef HAVE_FCHDIR 445 519 if (FCHDIR(sp, sp->fts_rfd)) { 520 #else 521 if (CHDIR(sp, sp->fts_rdir)) { 522 #endif 446 523 SET(FTS_STOP); 447 524 return (NULL); … … 461 538 p->fts_info = fts_stat(sp, p, 1); 462 539 if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { 540 #ifdef HAVE_FCHDIR 463 541 if ((p->fts_symfd = 464 542 open(".", O_RDONLY, 0)) == -1) { … … 471 549 } else 472 550 p->fts_flags |= FTS_SYMFOLLOW; 551 #endif 473 552 } 474 553 p->fts_instr = FTS_NOINSTR; … … 504 583 */ 505 584 if (p->fts_level == FTS_ROOTLEVEL) { 585 #ifdef HAVE_FCHDIR 506 586 if (FCHDIR(sp, sp->fts_rfd)) { 587 #else 588 if (CHDIR(sp, sp->fts_rdir)) { 589 #endif 507 590 SET(FTS_STOP); 508 591 return (NULL); 509 592 } 593 #ifdef HAVE_FCHDIR 510 594 } else if (p->fts_flags & FTS_SYMFOLLOW) { 511 595 if (FCHDIR(sp, p->fts_symfd)) { … … 517 601 } 518 602 (void)close(p->fts_symfd); 603 #else 604 (void)saved_errno; 605 #endif 519 606 } else if (!(p->fts_flags & FTS_DONTCHDIR) && 520 607 fts_safe_changedir(sp, p->fts_parent, -1, "..")) { … … 558 645 { 559 646 FTSENT *p; 647 #ifdef HAVE_FCHDIR 560 648 int fd; 649 #else 650 char *pszRoot; 651 int rc; 652 #endif 561 653 562 654 _DIAGASSERT(sp != NULL); … … 609 701 * fts_read will work. 610 702 */ 611 if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/'||703 if (p->fts_level != FTS_ROOTLEVEL || IS_SLASH(p->fts_accpath[0]) || 612 704 ISSET(FTS_NOCHDIR)) 613 705 return (sp->fts_child = fts_build(sp, instr)); 614 706 707 #ifdef HAVE_FCHDIR 615 708 if ((fd = open(".", O_RDONLY, 0)) == -1) 709 #else 710 if ((pszRoot = getcwd(NULL, 0)) == NULL) 711 #endif 616 712 return (sp->fts_child = NULL); 617 713 sp->fts_child = fts_build(sp, instr); 714 #ifdef HAVE_FCHDIR 618 715 if (fchdir(fd)) { 619 716 (void)close(fd); … … 621 718 } 622 719 (void)close(fd); 720 #else 721 rc = chdir(pszRoot); 722 free(pszRoot); 723 if (rc) 724 return (NULL); 725 #endif 726 623 727 return (sp->fts_child); 624 728 } … … 718 822 cderrno = 0; 719 823 if (nlinks || type == BREAD) { 824 #ifdef HAVE_FCHDIR 720 825 if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { 826 #else 827 if (fts_safe_changedir(sp, cur, dirfd(dirp), cur->fts_accpath)) { 828 #endif 721 829 if (nlinks && type == BREAD) 722 830 cur->fts_errno = errno; … … 867 975 if (descend && (type == BCHILD || !nitems) && 868 976 (cur->fts_level == FTS_ROOTLEVEL ? 977 #ifdef HAVE_FCHDIR 869 978 FCHDIR(sp, sp->fts_rfd) : 979 #else 980 CHDIR(sp, sp->fts_rdir) : 981 #endif 870 982 fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { 871 983 cur->fts_info = FTS_ERR; … … 1003 1115 *ap++ = p; 1004 1116 qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), 1005 (int (*) __P((const void *, const void *)))sp->fts_compar);1117 (int (*)(const void *, const void *))sp->fts_compar); 1006 1118 for (head = *(ap = sp->fts_array); --nitems; ++ap) 1007 1119 ap[0]->fts_link = ap[1]; … … 1041 1153 (struct STAT *)ALIGN((u_long)(p->fts_name + namelen + 2)); 1042 1154 #else 1155 (void)len; 1043 1156 if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL) 1044 1157 return (NULL); … … 1202 1315 return 0; 1203 1316 1317 #ifdef HAVE_FCHDIR 1204 1318 if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1) 1205 1319 return -1; … … 1207 1321 if (fstat(fd, &sb) == -1) 1208 1322 goto bail; 1323 #else 1324 if (stat(path, &sb)) 1325 goto bail; 1326 #endif 1209 1327 1210 1328 if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) { … … 1213 1331 } 1214 1332 1333 #ifdef HAVE_FCHDIR 1215 1334 ret = fchdir(fd); 1335 #else 1336 ret = chdir(path); 1337 #endif 1216 1338 1217 1339 bail: 1340 #ifdef HAVE_FCHDIR 1218 1341 if (oldfd < 0) { 1219 1342 int save_errno = errno; … … 1221 1344 errno = save_errno; 1222 1345 } 1346 #endif 1223 1347 return ret; 1224 1348 } -
trunk/src/gmake/kmkbuiltin/ftsfake.h
r620 r621 41 41 dev_t fts_dev; /* starting device # */ 42 42 char *fts_path; /* path for this descent */ 43 #ifdef _MSC_VER 44 char *fts_rdir; /* path of root */ 45 #else 43 46 int fts_rfd; /* fd for root */ 47 #endif 44 48 u_int fts_pathlen; /* sizeof(path) */ 45 49 u_int fts_nitems; /* elements in the sort array */ … … 54 58 #define FTS_SEEDOT 0x020 /* return dot and dot-dot */ 55 59 #define FTS_XDEV 0x040 /* don't cross devices */ 60 #ifndef _MSC_VER 56 61 #define FTS_WHITEOUT 0x080 /* return whiteout information */ 62 #endif 57 63 #define FTS_OPTIONMASK 0x0ff /* valid user option mask */ 58 64 … … 71 77 char *fts_path; /* root path */ 72 78 int fts_errno; /* errno for this node */ 79 #ifndef _MSC_VER 73 80 int fts_symfd; /* fd for symlink */ 81 #endif 74 82 u_short fts_pathlen; /* strlen(fts_path) */ 75 83 u_short fts_namelen; /* strlen(fts_name) */ … … 80 88 u_int16_t fts_nlink; /* link count */ 81 89 #else 90 #ifndef _MSC_VER 82 91 nlink_t fts_nlink; /* link count */ 92 #else 93 int fts_nlink; /* link count */ 94 #endif 83 95 #endif 84 96 … … 100 112 #define FTS_SL 12 /* symbolic link */ 101 113 #define FTS_SLNONE 13 /* symbolic link without target */ 114 #ifndef _MSC_VER 102 115 #define FTS_W 14 /* whiteout object */ 116 #endif 103 117 u_short fts_info; /* user flags for FTSENT structure */ 104 118 105 119 #define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ 106 120 #define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ 121 #ifndef _MSC_VER 107 122 #define FTS_ISW 0x04 /* this is a whiteout object */ 123 #endif 108 124 u_short fts_flags; /* private flags for FTSENT structure */ 109 125 … … 122 138 } FTSENT; 123 139 140 #ifndef _MSC_VER 124 141 #include <sys/cdefs.h> 142 __BEGIN_DECLS 143 #else 144 #define __RENAME(a) 145 #endif 125 146 126 __BEGIN_DECLS127 147 #ifdef __LIBC12_SOURCE__ 128 148 FTSENT *fts_children(FTS *, int); … … 141 161 int fts_set(FTS *, FTSENT *, int) __RENAME(__fts_set13); 142 162 #endif 163 164 #ifndef _MSC_VER 143 165 __END_DECLS 166 #endif 144 167 145 168 #endif /* !_FTS_H_ */
Note:
See TracChangeset
for help on using the changeset viewer.