Ignore:
Timestamp:
Nov 26, 2006, 9:14:00 AM (19 years ago)
Author:
bird
Message:

Ported the new fst code to MSC.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/kmkbuiltin/fts.c

    r619 r621  
    3434#endif
    3535
    36 #include <sys/cdefs.h>
     36/*#include <sys/cdefs.h>*/
    3737#if defined(LIBC_SCCS) && !defined(lint)
    3838#if 0
     
    4343#endif /* LIBC_SCCS and not lint */
    4444
    45 #include "namespace.h"
     45/*#include "namespace.h"*/
     46#ifndef _MSC_VER
    4647#include <sys/param.h>
     48#endif
    4749#include <sys/stat.h>
    4850
     
    5153#include <errno.h>
    5254#include <fcntl.h>
    53 #include <fts.h>
     55#include "ftsfake.h"
    5456#include <stdlib.h>
    5557#include <string.h>
     58#ifndef _MSC_VER
    5659#include <unistd.h>
     60#else
     61#include "mscfakes.h"
     62#define dirfd(dir) -1
     63#endif
     64#include "ftsfake.h"
    5765
    5866#if ! HAVE_NBTOOL_CONFIG_H
     
    6068#endif
    6169
     70#if 0
    6271#ifdef __weak_alias
    6372#ifdef __LIBC12_SOURCE__
     
    6978#endif /* __LIBC12_SOURCE__ */
    7079#endif /* __weak_alias */
     80#endif
    7181
    7282#ifdef __LIBC12_SOURCE__
     
    94104#endif
    95105
    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 *));
     106static FTSENT   *fts_alloc(FTS *, const char *, size_t);
     107static FTSENT   *fts_build(FTS *, int);
     108static void      fts_lfree(FTSENT *);
     109static void      fts_load(FTS *, FTSENT *);
     110static size_t    fts_maxarglen(char * const *);
     111static size_t    fts_pow2(size_t);
     112static int       fts_palloc(FTS *, size_t);
     113static void      fts_padjust(FTS *, FTSENT *);
     114static FTSENT   *fts_sort(FTS *, FTSENT *, size_t);
     115static u_short   fts_stat(FTS *, FTSENT *, int);
     116static 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
    108131
    109132#define ISDOT(a)        (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
     
    114137
    115138#define CHDIR(sp, path) (!ISSET(FTS_NOCHDIR) && chdir(path))
     139#ifdef HAVE_FCHDIR
    116140#define FCHDIR(sp, fd)  (!ISSET(FTS_NOCHDIR) && fchdir(fd))
     141#else
     142#define FCHDIR(sp, rdir) CHDIR(sp, rdir)
     143#endif
     144
    117145
    118146/* fts_build flags */
     
    125153#endif
    126154
     155#ifndef _DIAGASSERT
     156#define _DIAGASSERT assert
     157#endif
     158
     159
    127160FTS *
    128161fts_open(argv, options, compar)
    129162        char * const *argv;
    130163        int options;
    131         int (*compar) __P((const FTSENT **, const FTSENT **));
     164        int (*compar)(const FTSENT **, const FTSENT **);
    132165{
    133166        FTS *sp;
     
    225258         */
    226259        if (!ISSET(FTS_NOCHDIR)) {
     260#ifdef HAVE_FCHDIR
    227261                if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1)
    228262                        SET(FTS_NOCHDIR);
     
    231265                        SET(FTS_NOCHDIR);
    232266                }
     267#else
     268                if ((sp->fts_rdir = getcwd(NULL, 0)) != NULL)
     269                        SET(FTS_NOCHDIR);
     270#endif
    233271        }
    234272
     
    242280}
    243281
     282#ifdef NEED_STRRSLASH
     283static 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
    244300static void
    245301fts_load(sp, p)
     
    262318        len = p->fts_pathlen = p->fts_namelen;
    263319        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
    264323        if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
     324#endif
    265325                len = strlen(++cp);
    266326                memmove(p->fts_name, cp, len + 1);
     
    286346         */
    287347        if (sp->fts_cur) {
     348#ifndef _MSC_VER
    288349                if (ISSET(FTS_SYMFOLLOW))
    289350                        (void)close(sp->fts_cur->fts_symfd);
     351#endif
    290352                for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
    291353                        freep = p;
     
    305367        /* Return to original directory, save errno if necessary. */
    306368        if (!ISSET(FTS_NOCHDIR)) {
     369#ifdef HAVE_FCHDIR
    307370                if (fchdir(sp->fts_rfd))
    308371                        saved_errno = errno;
    309372                (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
    310379        }
    311380
     
    368437                p->fts_info = fts_stat(sp, p, 1);
    369438                if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
     439#ifdef HAVE_FCHDIR
    370440                        if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) {
    371441                                p->fts_errno = errno;
     
    377447                        } else
    378448                                p->fts_flags |= FTS_SYMFOLLOW;
     449#endif
    379450                }
    380451                return (p);
     
    386457                if (instr == FTS_SKIP ||
    387458                    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
     459#ifdef HAVE_FCHDIR
    388460                        if (p->fts_flags & FTS_SYMFOLLOW)
    389461                                (void)close(p->fts_symfd);
     462#endif
    390463                        if (sp->fts_child) {
    391464                                fts_lfree(sp->fts_child);
     
    443516                 */
    444517                if (p->fts_level == FTS_ROOTLEVEL) {
     518#ifdef HAVE_FCHDIR
    445519                        if (FCHDIR(sp, sp->fts_rfd)) {
     520#else
     521                        if (CHDIR(sp, sp->fts_rdir)) {
     522#endif
    446523                                SET(FTS_STOP);
    447524                                return (NULL);
     
    461538                        p->fts_info = fts_stat(sp, p, 1);
    462539                        if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
     540#ifdef HAVE_FCHDIR
    463541                                if ((p->fts_symfd =
    464542                                    open(".", O_RDONLY, 0)) == -1) {
     
    471549                                } else
    472550                                        p->fts_flags |= FTS_SYMFOLLOW;
     551#endif
    473552                        }
    474553                        p->fts_instr = FTS_NOINSTR;
     
    504583         */
    505584        if (p->fts_level == FTS_ROOTLEVEL) {
     585#ifdef HAVE_FCHDIR
    506586                if (FCHDIR(sp, sp->fts_rfd)) {
     587#else
     588                if (CHDIR(sp, sp->fts_rdir)) {
     589#endif
    507590                        SET(FTS_STOP);
    508591                        return (NULL);
    509592                }
     593#ifdef HAVE_FCHDIR
    510594        } else if (p->fts_flags & FTS_SYMFOLLOW) {
    511595                if (FCHDIR(sp, p->fts_symfd)) {
     
    517601                }
    518602                (void)close(p->fts_symfd);
     603#else
     604        (void)saved_errno;
     605#endif
    519606        } else if (!(p->fts_flags & FTS_DONTCHDIR) &&
    520607            fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
     
    558645{
    559646        FTSENT *p;
     647#ifdef HAVE_FCHDIR
    560648        int fd;
     649#else
     650        char *pszRoot;
     651        int rc;
     652#endif
    561653
    562654        _DIAGASSERT(sp != NULL);
     
    609701         * fts_read will work.
    610702         */
    611         if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
     703        if (p->fts_level != FTS_ROOTLEVEL || IS_SLASH(p->fts_accpath[0]) ||
    612704            ISSET(FTS_NOCHDIR))
    613705                return (sp->fts_child = fts_build(sp, instr));
    614706
     707#ifdef HAVE_FCHDIR
    615708        if ((fd = open(".", O_RDONLY, 0)) == -1)
     709#else
     710        if ((pszRoot = getcwd(NULL, 0)) == NULL)
     711#endif
    616712                return (sp->fts_child = NULL);
    617713        sp->fts_child = fts_build(sp, instr);
     714#ifdef HAVE_FCHDIR
    618715        if (fchdir(fd)) {
    619716                (void)close(fd);
     
    621718        }
    622719        (void)close(fd);
     720#else
     721        rc = chdir(pszRoot);
     722        free(pszRoot);
     723        if (rc)
     724                return (NULL);
     725#endif
     726
    623727        return (sp->fts_child);
    624728}
     
    718822        cderrno = 0;
    719823        if (nlinks || type == BREAD) {
     824#ifdef HAVE_FCHDIR
    720825                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
    721829                        if (nlinks && type == BREAD)
    722830                                cur->fts_errno = errno;
     
    867975        if (descend && (type == BCHILD || !nitems) &&
    868976            (cur->fts_level == FTS_ROOTLEVEL ?
     977#ifdef HAVE_FCHDIR
    869978            FCHDIR(sp, sp->fts_rfd) :
     979#else
     980            CHDIR(sp, sp->fts_rdir) :
     981#endif
    870982            fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
    871983                cur->fts_info = FTS_ERR;
     
    10031115                *ap++ = p;
    10041116        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);
    10061118        for (head = *(ap = sp->fts_array); --nitems; ++ap)
    10071119                ap[0]->fts_link = ap[1];
     
    10411153                    (struct STAT *)ALIGN((u_long)(p->fts_name + namelen + 2));
    10421154#else
     1155        (void)len;
    10431156        if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
    10441157                return (NULL);
     
    12021315                return 0;
    12031316
     1317#ifdef HAVE_FCHDIR
    12041318        if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1)
    12051319                return -1;
     
    12071321        if (fstat(fd, &sb) == -1)
    12081322                goto bail;
     1323#else
     1324        if (stat(path, &sb))
     1325                goto bail;
     1326#endif
    12091327
    12101328        if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
     
    12131331        }
    12141332
     1333#ifdef HAVE_FCHDIR
    12151334        ret = fchdir(fd);
     1335#else
     1336        ret = chdir(path);
     1337#endif
    12161338
    12171339bail:
     1340#ifdef HAVE_FCHDIR
    12181341        if (oldfd < 0) {
    12191342                int save_errno = errno;
     
    12211344                errno = save_errno;
    12221345        }
     1346#endif
    12231347        return ret;
    12241348}
Note: See TracChangeset for help on using the changeset viewer.