Integrate perlio:
[p5sagit/p5-mst-13.2.git] / ext / File / Glob / bsd_glob.c
index 47e96d6..d0d4a91 100644 (file)
@@ -32,6 +32,9 @@
 
 #if defined(LIBC_SCCS) && !defined(lint)
 static char sccsid[] = "@(#)glob.c     8.3 (Berkeley) 10/13/93";
+/* most changes between the version above and the one below have been ported:
+static char sscsid[]=  "$OpenBSD: glob.c,v 1.8.10.1 2001/04/10 jason Exp $";
+ */
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -70,7 +73,7 @@ static char sccsid[] = "@(#)glob.c    8.3 (Berkeley) 10/13/93";
 #ifdef I_PWD
 #      include <pwd.h>
 #else
-#ifdef HAS_PASSWD
+#if defined(HAS_PASSWD) && !defined(VMS)
        struct passwd *getpwnam(char *);
        struct passwd *getpwuid(Uid_t);
 #endif
@@ -92,16 +95,20 @@ static char sccsid[] = "@(#)glob.c  8.3 (Berkeley) 10/13/93";
 #endif
 
 #ifndef ARG_MAX
-#  ifdef _SC_ARG_MAX
-#    define    ARG_MAX         (sysconf(_SC_ARG_MAX))
+#  ifdef MACOS_TRADITIONAL
+#    define            ARG_MAX         65536   /* Mac OS is actually unlimited */
 #  else
-#    ifdef _POSIX_ARG_MAX
-#      define  ARG_MAX         _POSIX_ARG_MAX
+#    ifdef _SC_ARG_MAX
+#      define          ARG_MAX         (sysconf(_SC_ARG_MAX))
 #    else
-#      ifdef WIN32
-#        define        ARG_MAX         14500   /* from VC's limits.h */
+#      ifdef _POSIX_ARG_MAX
+#        define                ARG_MAX         _POSIX_ARG_MAX
 #      else
-#        define        ARG_MAX         4096    /* from POSIX, be conservative */
+#        ifdef WIN32
+#          define      ARG_MAX         14500   /* from VC's limits.h */
+#        else
+#          define      ARG_MAX         4096    /* from POSIX, be conservative */
+#        endif
 #      endif
 #    endif
 #  endif
@@ -199,6 +206,23 @@ my_readdir(DIR *d)
 #define        my_readdir      readdir
 #endif
 
+#ifdef MACOS_TRADITIONAL
+#include <Files.h>
+#include <Types.h>
+#include <string.h>
+
+#define NO_UPDIR_ERR 1 /* updir resolving failed */
+
+static Boolean g_matchVol; /* global variable */
+static short updir(char *path);
+static short resolve_updirs(char *new_pattern);
+static void remove_trColon(char *path);
+static short glob_mark_Mac(Char *pathbuf, Char *pathend, Char *pathend_last);
+static OSErr GetVolInfo(short volume, Boolean indexed, FSSpec *spec);
+static void name_f_FSSpec(StrFileName volname, FSSpec *spec);
+
+#endif
+
 int
 bsd_glob(const char *pattern, int flags,
         int (*errfunc)(const char *, int), glob_t *pglob)
@@ -207,7 +231,15 @@ bsd_glob(const char *pattern, int flags,
        int c;
        Char *bufnext, *bufend, patbuf[MAXPATHLEN];
 
+#ifdef MACOS_TRADITIONAL
+       char *new_pat, *p, *np;
+       int err;
+       size_t len;
+#endif
+
+#ifndef MACOS_TRADITIONAL
        patnext = (U8 *) pattern;
+#endif
        if (!(flags & GLOB_APPEND)) {
                pglob->gl_pathc = 0;
                pglob->gl_pathv = NULL;
@@ -227,7 +259,7 @@ bsd_glob(const char *pattern, int flags,
         * colon specially, so it looks for files beginning "C:" in
         * the current directory. To fix this, change the pattern to
         * add an explicit "./" at the start (just after the drive
-        * letter and colon - ie change to "C:./*").
+        * letter and colon - ie change to "C:./").
         */
        if (isalpha(pattern[0]) && pattern[1] == ':' &&
            pattern[2] != BG_SEP && pattern[2] != BG_SEP2 &&
@@ -239,6 +271,62 @@ bsd_glob(const char *pattern, int flags,
                patnext += 2;
        }
 #endif
+
+#ifdef MACOS_TRADITIONAL
+       /* Check if we need to match a volume name (e.g. '*HD:*') */
+       g_matchVol = false;
+       p = (char *) pattern;
+       if (*p != BG_SEP) {
+           p++;
+           while (*p != BG_EOS) {
+               if (*p == BG_SEP) {
+                   g_matchVol = true;
+                   break;
+               }
+               p++;
+           }
+       }
+
+       /* Transform the pattern:
+        * (a) Resolve updirs, e.g.
+        *     '*:t*p::'       -> '*:'
+        *         ':a*:tmp::::'   -> '::'
+        *         ':base::t*p:::' -> '::'
+        *     '*HD::'         -> return 0 (error, quit silently)
+        *
+        * (b) Remove a single trailing ':', unless it's a "match volume only"
+        *     pattern like '*HD:'; e.g.
+        *     '*:tmp:' -> '*:tmp'  but
+        *     '*HD:'   -> '*HD:'
+        *     (If we don't do that, even filenames will have a trailing ':' in
+        *     the result.)
+        */
+
+       /* We operate on a copy of the pattern */
+       len = strlen(pattern);
+       New(0, new_pat, len + 1, char);
+       if (new_pat == NULL)
+           return (GLOB_NOSPACE);
+
+       p = (char *) pattern;
+       np = new_pat;
+       while (*np++ = *p++) ;
+
+       /* Resolve updirs ... */
+       err = resolve_updirs(new_pat);
+       if (err) {
+           Safefree(new_pat);
+           /* The pattern is incorrect: tried to move
+              up above the volume root, see above.
+              We quit silently. */
+           return 0;
+       }
+       /* remove trailing colon ... */
+       remove_trColon(new_pat);
+       patnext = (U8 *) new_pat;
+
+#endif /* MACOS_TRADITIONAL */
+
        if (flags & GLOB_QUOTE) {
                /* Protect the quoted characters. */
                while (bufnext < bufend && (c = *patnext++) != BG_EOS)
@@ -266,10 +354,19 @@ bsd_glob(const char *pattern, int flags,
                        *bufnext++ = c;
        *bufnext = BG_EOS;
 
+#ifdef MACOS_TRADITIONAL
+       if (flags & GLOB_BRACE)
+           err = globexp1(patbuf, pglob);
+       else
+           err = glob0(patbuf, pglob);
+       Safefree(new_pat);
+       return err;
+#else
        if (flags & GLOB_BRACE)
            return globexp1(patbuf, pglob);
        else
            return glob0(patbuf, pglob);
+#endif
 }
 
 /*
@@ -407,19 +504,20 @@ globexp2(const Char *ptr, const Char *pattern,
  * expand tilde from the passwd file.
  */
 static const Char *
-globtilde(const Char *pattern, Char *patbuf, size_t limit, glob_t *pglob)
+globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
 {
        struct passwd *pwd;
        char *h;
        const Char *p;
-       Char *b;
+       Char *b, *eb;
 
        if (*pattern != BG_TILDE || !(pglob->gl_flags & GLOB_TILDE))
                return pattern;
 
        /* Copy up to the end of the string or / */
-       for (p = pattern + 1, h = (char *) patbuf; *p && *p != BG_SLASH;
-            *h++ = *p++)
+       eb = &patbuf[patbuf_len - 1];
+       for (p = pattern + 1, h = (char *) patbuf;
+            h < (char*)eb && *p && *p != BG_SLASH; *h++ = *p++)
                ;
 
        *h = BG_EOS;
@@ -459,12 +557,13 @@ globtilde(const Char *pattern, Char *patbuf, size_t limit, glob_t *pglob)
        }
 
        /* Copy the home directory */
-       for (b = patbuf; *h; *b++ = *h++)
+       for (b = patbuf; b < eb && *h; *b++ = *h++)
                ;
 
        /* Append the rest of the pattern */
-       while ((*b++ = *p++) != BG_EOS)
+       while (b < eb && (*b++ = *p++) != BG_EOS)
                ;
+       *b = BG_EOS;
 
        return patbuf;
 }
@@ -487,7 +586,7 @@ glob0(const Char *pattern, glob_t *pglob)
 
 #ifdef MACOS_TRADITIONAL
        if ( (*pattern == BG_TILDE) && (pglob->gl_flags & GLOB_TILDE) ) {
-               return(globextend(pattern, pglob));
+               return(globextend(pattern, pglob, &limit));
        }
 #endif
 
@@ -573,7 +672,7 @@ glob0(const Char *pattern, glob_t *pglob)
         }
        else if (!(pglob->gl_flags & GLOB_NOSORT))
                qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
-                   pglob->gl_pathc - oldpathc, sizeof(char *), 
+                   pglob->gl_pathc - oldpathc, sizeof(char *),
                    (pglob->gl_flags & (GLOB_ALPHASORT|GLOB_NOCASE))
                        ? ci_compare : compare);
        pglob->gl_flags = oldflags;
@@ -587,12 +686,12 @@ ci_compare(const void *p, const void *q)
        const char *qq = *(const char **)q;
        int ci;
        while (*pp && *qq) {
-               if (tolower(*pp) != tolower(*qq))
+               if (toLOWER(*pp) != toLOWER(*qq))
                        break;
                ++pp;
                ++qq;
        }
-       ci = tolower(*pp) - tolower(*qq);
+       ci = toLOWER(*pp) - toLOWER(*qq);
        if (ci == 0)
                return compare(p, q);
        return ci;
@@ -637,7 +736,6 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
        for (anymeta = 0;;) {
                if (*pattern == BG_EOS) {               /* End of pattern? */
                        *pathend = BG_EOS;
-
                        if (g_lstat(pathbuf, &sb, pglob))
                                return(0);
 
@@ -650,10 +748,17 @@ glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
                                  (S_ISLNK(sb.st_mode) &&
                            (g_stat(pathbuf, &sb, pglob) == 0) &&
                            S_ISDIR(sb.st_mode)))) {
+#ifdef MACOS_TRADITIONAL
+                               short err;
+                               err = glob_mark_Mac(pathbuf, pathend, pathend_last);
+                               if (err)
+                                       return (err);
+#else
                                if (pathend+1 > pathend_last)
                                        return (1);
                                *pathend++ = BG_SEP;
                                *pathend = BG_EOS;
+#endif
                        }
                        ++pglob->gl_matchc;
 #ifdef GLOB_DEBUG
@@ -724,19 +829,64 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
 
 #ifdef VMS
         {
-            Char *q = pathend;
-            if (q - pathbuf > 5) {
-                q -= 5;
-                if (q[0] == '.' && tolower(q[1]) == 'd' && tolower(q[2]) == 'i'
-                   && tolower(q[3]) == 'r' && q[4] == '/')
-               {
-                    q[0] = '/';
-                    q[1] = BG_EOS;
-                    pathend = q+1;
-                }
-            }
+               Char *q = pathend;
+               if (q - pathbuf > 5) {
+                       q -= 5;
+                       if (q[0] == '.' &&
+                           tolower(q[1]) == 'd' && tolower(q[2]) == 'i' &&
+                           tolower(q[3]) == 'r' && q[4] == '/')
+                       {
+                               q[0] = '/';
+                               q[1] = BG_EOS;
+                               pathend = q+1;
+                       }
+               }
         }
 #endif
+
+#ifdef MACOS_TRADITIONAL
+       if ((!*pathbuf) && (g_matchVol)) {
+           FSSpec spec;
+           short index;
+           StrFileName vol_name; /* unsigned char[64] on MacOS */
+
+           err = 0;
+           nocase = ((pglob->gl_flags & GLOB_NOCASE) != 0);
+
+           /* Get and match a list of volume names */
+           for (index = 0; !GetVolInfo(index+1, true, &spec); ++index) {
+               register U8 *sc;
+               register Char *dc;
+
+               name_f_FSSpec(vol_name, &spec);
+
+               /* Initial BG_DOT must be matched literally. */
+               if (*vol_name == BG_DOT && *pattern != BG_DOT)
+                   continue;
+               dc = pathend;
+               sc = (U8 *) vol_name;
+               while (dc < pathend_last && (*dc++ = *sc++) != BG_EOS)
+                   ;
+               if (dc >= pathend_last) {
+                   *dc = BG_EOS;
+                   err = 1;
+                   break;
+               }
+
+               if (!match(pathend, pattern, restpattern, nocase)) {
+                   *pathend = BG_EOS;
+                   continue;
+               }
+               err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
+                   restpattern, restpattern_last, pglob, limitp);
+               if (err)
+                   break;
+           }
+           return(err);
+
+       } else { /* open dir */
+#endif /* MACOS_TRADITIONAL */
+
        if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
                /* TODO: don't call for ENOENT or ENOTDIR? */
                if (pglob->gl_errfunc) {
@@ -789,6 +939,10 @@ glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
        else
                PerlDir_close(dirp);
        return(err);
+
+#ifdef MACOS_TRADITIONAL
+       }
+#endif
 }
 
 
@@ -846,6 +1000,7 @@ globextend(const Char *path, glob_t *pglob, size_t *limitp)
        for (p = path; *p++;)
                ;
        len = (STRLEN)(p - path);
+       *limitp += len;
        New(0, copy, p-path, char);
        if (copy != NULL) {
                if (g_Ctoc(path, copy, len)) {
@@ -861,6 +1016,7 @@ globextend(const Char *path, glob_t *pglob, size_t *limitp)
                errno = 0;
                return(GLOB_NOSPACE);
        }
+
        return(copy == NULL ? GLOB_NOSPACE : 0);
 }
 
@@ -957,8 +1113,8 @@ g_opendir(register Char *str, glob_t *pglob)
 
        if (pglob->gl_flags & GLOB_ALTDIRFUNC)
                return((*pglob->gl_opendir)(buf));
-       else
-           return(PerlDir_open(buf));
+
+       return(PerlDir_open(buf));
 }
 
 static int
@@ -1027,3 +1183,209 @@ qprintf(const char *str, register Char *s)
        (void)printf("\n");
 }
 #endif /* GLOB_DEBUG */
+
+
+#ifdef MACOS_TRADITIONAL
+
+/* Replace the last occurence of the pattern ":[^:]+::", e.g. ":lib::",
+   with a single ':', if possible. It is not an error, if the pattern
+   doesn't match (we return -1), but if there are two consecutive colons
+   '::', there must be a preceding ':[^:]+'. Hence,  a volume path like
+   "HD::" is considered to be an error (we return 1), that is, it can't
+   be resolved. We return 0 on success.
+*/
+
+static short
+updir(char *path)
+{
+       char *pb, *pe, *lastchar;
+       char *bgn_mark, *end_mark;
+       char *f, *m, *b; /* front, middle, back */
+       size_t len;
+
+       len = strlen(path);
+       lastchar = path + (len-1);
+       b = lastchar;
+       m = lastchar-1;
+       f = lastchar-2;
+
+       /* find a '[^:]::' (e.g. b::) pattern ... */
+       while ( !( (*f != BG_SEP) && (*m == BG_SEP) && (*b == BG_SEP) )
+               && (f >= path)) {
+               f--;
+               m--;
+               b--;
+       }
+
+       if (f < path) { /* no (more) match */
+               return -1;
+       }
+
+       end_mark = b;
+
+       /* ... and now find its preceding colon ':' */
+       while ((*f != BG_SEP) && (f >= path)) {
+               f--;
+       }
+       if (f < path) {
+               /* No preceding colon found, must be a
+                  volume path. We can't move up the
+                  tree and that's an error */
+               return 1;
+       }
+       bgn_mark = f;
+
+       /* Shrink path, i.e. exclude all characters between
+          bgn_mark and end_mark */
+
+       pb = bgn_mark;
+       pe = end_mark;
+       while (*pb++ = *pe++) ;
+       return 0;
+}
+
+
+/* Resolve all updirs in pattern. */
+
+static short
+resolve_updirs(char *new_pattern)
+{
+       short err;
+
+       do {
+               err = updir(new_pattern);
+       } while (!err);
+       if (err == 1) {
+               return NO_UPDIR_ERR;
+       }
+       return 0;
+}
+
+
+/* Remove a trailing colon from the path, but only if it's
+   not a volume path (e.g. HD:) and not a path consisting
+   solely of colons. */
+
+static void
+remove_trColon(char *path)
+{
+       char *lastchar, *lc;
+
+       /* if path matches the pattern /:[^:]+:$/, we can
+          remove the trailing ':' */
+
+       lc = lastchar = path + (strlen(path) - 1);
+       if (*lastchar == BG_SEP) {
+               /* there's a trailing ':', there must be at least
+                  one preceding char != ':' and a preceding ':' */
+               lc--;
+               if ((*lc != BG_SEP) && (lc >= path)) {
+                       lc--;
+               } else {
+                       return;
+               }
+               while ((*lc != BG_SEP) && (lc >= path)) {
+                       lc--;
+               }
+               if (lc >= path) {
+                       /* ... there's a preceding ':', we remove
+                          the trailing colon */
+                       *lastchar = BG_EOS;
+               }
+       }
+}
+
+
+/* With the GLOB_MARK flag on, we append a colon, if pathbuf
+   is a directory. If the directory name contains no colons,
+   e.g. 'lib', we can't simply append a ':', since this (e.g.
+   'lib:') is not a valid (relative) path on Mac OS. Instead,
+   we add a leading _and_ trailing ':'. */
+
+static short
+glob_mark_Mac(Char *pathbuf, Char *pathend, Char *pathend_last)
+{
+       Char *p, *pe;
+       Boolean is_file = true;
+
+       /* check if pathbuf contains a ':',
+          i.e. is not a file name */
+       p = pathbuf;
+       while (*p != BG_EOS) {
+               if (*p == BG_SEP) {
+                       is_file = false;
+                       break;
+               }
+               p++;
+       }
+
+       if (is_file) {
+               if (pathend+2 > pathend_last) {
+                       return (1);
+               }
+               /* right shift one char */
+               pe = p = pathend;
+               p--;
+               pathend++;
+               while (p >= pathbuf) {
+                       *pe-- = *p--;
+               }
+               /* first char becomes a colon */
+               *pathbuf = BG_SEP;
+               /* append a colon */
+               *pathend++ = BG_SEP;
+               *pathend = BG_EOS;
+
+       } else {
+               if (pathend+1 > pathend_last) {
+                       return (1);
+               }
+               *pathend++ = BG_SEP;
+               *pathend = BG_EOS;
+       }
+       return 0;
+}
+
+
+/* Return a FSSpec record for the specified volume
+   (borrowed from MacPerl.xs). */
+
+static OSErr
+GetVolInfo(short volume, Boolean indexed, FSSpec* spec)
+{
+       OSErr           err; /* OSErr: 16-bit integer */
+       HParamBlockRec  pb;
+
+       pb.volumeParam.ioNamePtr        = spec->name;
+       pb.volumeParam.ioVRefNum        = indexed ? 0 : volume;
+       pb.volumeParam.ioVolIndex       = indexed ? volume : 0;
+
+       if (err = PBHGetVInfoSync(&pb))
+               return err;
+
+       spec->vRefNum   = pb.volumeParam.ioVRefNum;
+       spec->parID     = 1;
+
+       return noErr; /* 0 */
+}
+
+/* Extract a C name from a FSSpec. Note that there are
+   no leading or trailing colons. */
+
+static void
+name_f_FSSpec(StrFileName name, FSSpec *spec)
+{
+       unsigned char *nc;
+       const short len = spec->name[0];
+       short i;
+
+       /* FSSpec.name is a Pascal string,
+          convert it to C ... */
+       nc = name;
+       for (i=1; i<=len; i++) {
+               *nc++ = spec->name[i];
+       }
+       *nc = BG_EOS;
+}
+
+#endif /* MACOS_TRADITIONAL */