Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

l_mcw_glob.c

Go to the documentation of this file.
00001 /**************************************************************************
00002   mcw_glob.c -- slightly adapted from glob.c in tcsh-6.05
00003                 (made to compile without support files besides mcw_glob.h)
00004              -- added routines MCW_*_expand at end
00005 ***************************************************************************/
00006 
00007 /*
00008  * Copyright (c) 1989 The Regents of the University of California.
00009  * All rights reserved.
00010  *
00011  * This code is derived from software contributed to Berkeley by
00012  * Guido van Rossum.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions
00016  * are met:
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  * 2. Redistributions in binary form must reproduce the above copyright
00020  *    notice, this list of conditions and the following disclaimer in the
00021  *    documentation and/or other materials provided with the distribution.
00022  * 3. All advertising materials mentioning features or use of this software
00023  *    must display the following acknowledgement:
00024  *      This product includes software developed by the University of
00025  *      California, Berkeley and its contributors.
00026  * 4. Neither the name of the University nor the names of its contributors
00027  *    may be used to endorse or promote products derived from this software
00028  *    without specific prior written permission.
00029  *
00030  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00031  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00032  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00033  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00034  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00035  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00036  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00037  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00039  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00040  * SUCH DAMAGE.
00041  */
00042 
00043 /*
00044  * Glob: the interface is a superset of the one defined in POSIX 1003.2,
00045  * draft 9.
00046  *
00047  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
00048  *
00049  * Optional extra services, controlled by flags not defined by POSIX:
00050  *
00051  * GLOB_QUOTE:
00052  *      Escaping convention: \ inhibits any special meaning the following
00053  *      character might have (except \ at end of string is retained).
00054  * GLOB_MAGCHAR:
00055  *      Set in gl_flags if pattern contained a globbing character.
00056  * GLOB_ALTNOT:
00057  *      Use ^ instead of ! for "not".
00058  * gl_matchc:
00059  *      Number of matches in the current invocation of glob.
00060  */
00061 
00062 
00063 /** the following were in "sh.h",
00064     but I put them here to get rid of the need for that file -- RWCox **/
00065 
00066 #define xfree     free
00067 #define xmalloc   malloc
00068 #define xrealloc  realloc
00069 
00070 #ifdef SPARKY
00071 #undef _POSIX_SOURCE
00072 #endif
00073 
00074 #include <sys/types.h>
00075 #include <sys/param.h>
00076 #include <sys/stat.h>
00077 #include <dirent.h>
00078 #include <ctype.h>
00079 typedef void * ptr_t;
00080 
00081 /** don't use sh.h any more **/
00082 
00083 #if 0
00084 #  define Char __Char
00085 #  include "sh.h"
00086 #  undef Char
00087 #  undef QUOTE
00088 #  undef TILDE
00089 #  undef META
00090 #  undef CHAR
00091 #  undef ismeta
00092 #  undef Strchr
00093 #endif
00094 
00095 #include "l_mcw_glob.h"
00096 
00097 /* added for direction control on sorting                14 Feb 2005 [rickr] */
00098 static int g_sort_dir = 1 ;       /* 1 = small to large, -1 = large to small */
00099 
00100 #ifndef S_ISDIR
00101 #define S_ISDIR(a)      (((a) & S_IFMT) == S_IFDIR)
00102 #endif
00103 
00104 #if !defined(S_ISLNK) && defined(S_IFLNK)
00105 #define S_ISLNK(a)      (((a) & S_IFMT) == S_IFLNK)
00106 #endif
00107 
00108 #if !defined(S_ISLNK) && !defined(lstat)
00109 #define lstat stat
00110 #endif
00111 
00112 typedef unsigned short Char;
00113 
00114 #undef  __P
00115 #define __P(a) a
00116 
00117 static  int      glob1          __P((Char *, glob_t *, int));
00118 static  int      glob2          __P((Char *, Char *, Char *, glob_t *, int));
00119 static  int      glob3          __P((Char *, Char *, Char *, Char *,
00120                                      glob_t *, int));
00121 static  int      globextend     __P((Char *, glob_t *));
00122 static  int      match          __P((Char *, Char *, Char *, int));
00123 #ifndef __clipper__
00124 static  int      compare        __P((const ptr_t, const ptr_t));
00125 #endif
00126 static  DIR     *Opendir        __P((Char *));
00127 #ifdef S_IFLNK
00128 static  int      Lstat          __P((Char *, struct stat *));
00129 #endif
00130 static  Char    *Strchr         __P((Char *, int));
00131 #ifdef DEBUG
00132 static  void     qprintf        __P((Char *));
00133 #endif
00134 
00135 #define DOLLAR          '$'
00136 #define DOT             '.'
00137 #define EOS             '\0'
00138 #define LBRACKET        '['
00139 #define NOT             '!'
00140 #define ALTNOT          '^'
00141 #define QUESTION        '?'
00142 #define QUOTE           '\\'
00143 #define RANGE           '-'
00144 #define RBRACKET        ']'
00145 #define SEP             '/'
00146 #define STAR            '*'
00147 #define TILDE           '~'
00148 #define UNDERSCORE      '_'
00149 
00150 #define M_META          0x8000
00151 #define M_PROTECT       0x4000
00152 #define M_MASK          0xffff
00153 #define M_ASCII         0x00ff
00154 
00155 #define CHAR(c)         ((c)&M_ASCII)
00156 #define META(c)         ((c)|M_META)
00157 #define M_ALL           META('*')
00158 #define M_END           META(']')
00159 #define M_NOT           META('!')
00160 #define M_ALTNOT        META('^')
00161 #define M_ONE           META('?')
00162 #define M_RNG           META('-')
00163 #define M_SET           META('[')
00164 #define ismeta(c)       (((c)&M_META) != 0)
00165 
00166 #if defined(SOLARIS_DIRENT_ZERO) && !defined(SOLARIS_DIRENT_PATCH)
00167 #  define SOLARIS_DIRENT_PATCH
00168 #endif
00169 
00170 #ifdef SOLARIS_DIRENT_PATCH
00171 struct  dirent {
00172      ino_t            d_ino;
00173      off_t            d_off;
00174      unsigned short        d_reclen;
00175      char             d_name[1];
00176 };
00177 #endif
00178 
00179 /*
00180  * Need to dodge two kernel bugs:
00181  * opendir("") != opendir(".")
00182  * NAMEI_BUG: on plain files trailing slashes are ignored in some kernels.
00183  *            POSIX specifies that they should be ignored in directories.
00184  */
00185 
00186 static DIR *
00187 Opendir(Char *str)
00188 {
00189     char    buf[MAXPATHLEN];
00190     register char *dc = buf;
00191 
00192     if (!*str)
00193         return (opendir("."));
00194     while ((*dc++ = *str++) != '\0')
00195         continue;
00196     return (opendir(buf));
00197 }
00198 
00199 #ifdef S_IFLNK
00200 static int
00201 Lstat(Char *fn, struct stat *sb)
00202 {
00203     char    buf[MAXPATHLEN];
00204     register char *dc = buf;
00205 
00206     while ((*dc++ = *fn++) != '\0')
00207         continue;
00208 # ifdef NAMEI_BUG
00209     {
00210         int     st;
00211 
00212         st = lstat(buf, sb);
00213         if (*buf)
00214             dc--;
00215         return (*--dc == '/' && !S_ISDIR(sb->st_mode) ? -1 : st);
00216     }
00217 # else
00218     return (lstat(buf, sb));
00219 # endif /* NAMEI_BUG */
00220 }
00221 #else
00222 #define Lstat Stat
00223 #endif /* S_IFLNK */
00224 
00225 static int
00226 Stat(Char *fn, struct stat *sb)
00227 {
00228     char    buf[MAXPATHLEN];
00229     register char *dc = buf;
00230 
00231     while ((*dc++ = *fn++) != '\0')
00232         continue;
00233 #ifdef NAMEI_BUG
00234     {
00235         int     st;
00236 
00237         st = stat(buf, sb);
00238         if (*buf)
00239             dc--;
00240         return (*--dc == '/' && !S_ISDIR(sb->st_mode) ? -1 : st);
00241     }
00242 #else
00243     return (stat(buf, sb));
00244 #endif /* NAMEI_BUG */
00245 }
00246 
00247 static Char *
00248 Strchr(Char *str, int ch)
00249 {
00250     do
00251         if (*str == ch)
00252             return (str);
00253     while (*str++);
00254     return (NULL);
00255 }
00256 
00257 #ifdef DEBUG
00258 static void
00259 qprintf(Char *s)
00260 {
00261     Char *p;
00262 
00263     for (p = s; *p; p++)
00264         printf("%c", *p & 0xff);
00265     printf("\n");
00266     for (p = s; *p; p++)
00267         printf("%c", *p & M_PROTECT ? '"' : ' ');
00268     printf("\n");
00269     for (p = s; *p; p++)
00270         printf("%c", *p & M_META ? '_' : ' ');
00271     printf("\n");
00272 }
00273 #endif /* DEBUG */
00274 
00275 static int
00276 compare(const ptr_t p, const ptr_t q)
00277 {
00278 #if defined(NLS) && !defined(NOSTRCOLL)
00279 
00280 #if 0
00281     errno = 0;  /* strcoll sets errno, another brain-damage */
00282 #endif
00283 
00284     return (g_sort_dir * strcoll(*(char **) p, *(char **) q));
00285 #else
00286     return (g_sort_dir * strcmp(*(char **) p, *(char **) q));
00287 #endif /* NLS && !NOSTRCOLL */
00288 }
00289 
00290 /*
00291  * The main glob() routine: compiles the pattern (optionally processing
00292  * quotes), calls glob1() to do the real pattern matching, and finally
00293  * sorts the list (unless unsorted operation is requested).  Returns 0
00294  * if things went well, nonzero if errors occurred.  It is not an error
00295  * to find no matches.
00296  */
00297 int
00298 glob(const char *pattern, int flags, int(*errfunc)(char *,int), glob_t *pglob)
00299 {
00300     int     err, oldpathc;
00301     Char *bufnext, *bufend, *compilebuf, m_not;
00302     const unsigned char *compilepat, *patnext;
00303     int     c, nnot;
00304     Char patbuf[MAXPATHLEN + 1], *qpatnext;
00305     int     no_match;
00306 
00307     patnext = (unsigned char *) pattern;
00308     if (!(flags & GLOB_APPEND)) {
00309         pglob->gl_pathc = 0;
00310         pglob->gl_pathv = NULL;
00311         if (!(flags & GLOB_DOOFFS))
00312             pglob->gl_offs = 0;
00313     }
00314     pglob->gl_flags = flags & ~GLOB_MAGCHAR;
00315     pglob->gl_errfunc = errfunc;
00316     oldpathc = pglob->gl_pathc;
00317     pglob->gl_matchc = 0;
00318 
00319     if (pglob->gl_flags & GLOB_ALTNOT) {
00320         nnot = ALTNOT;
00321         m_not = M_ALTNOT;
00322     }
00323     else {
00324         nnot = NOT;
00325         m_not = M_NOT;
00326     }
00327 
00328     bufnext = patbuf;
00329     bufend = bufnext + MAXPATHLEN;
00330     compilebuf = bufnext;
00331     compilepat = patnext;
00332 
00333     no_match = *patnext == nnot;
00334     if (no_match)
00335         patnext++;
00336 
00337     if (flags & GLOB_QUOTE) {
00338         /* Protect the quoted characters */
00339         while (bufnext < bufend && (c = *patnext++) != EOS)
00340             if (c == QUOTE) {
00341                 if ((c = *patnext++) == EOS) {
00342                     c = QUOTE;
00343                     --patnext;
00344                 }
00345                 *bufnext++ = (Char) (c | M_PROTECT);
00346             }
00347             else
00348                 *bufnext++ = (Char) c;
00349     }
00350     else
00351         while (bufnext < bufend && (c = *patnext++) != EOS)
00352             *bufnext++ = (Char) c;
00353     *bufnext = EOS;
00354 
00355     bufnext = patbuf;
00356     qpatnext = patbuf;
00357     /* we don't need to check for buffer overflow any more */
00358     while ((c = *qpatnext++) != EOS) {
00359         switch (c) {
00360         case LBRACKET:
00361             c = *qpatnext;
00362             if (c == nnot)
00363                 ++qpatnext;
00364             if (*qpatnext == EOS ||
00365                 Strchr(qpatnext + 1, RBRACKET) == NULL) {
00366                 *bufnext++ = LBRACKET;
00367                 if (c == nnot)
00368                     --qpatnext;
00369                 break;
00370             }
00371             pglob->gl_flags |= GLOB_MAGCHAR;
00372             *bufnext++ = M_SET;
00373             if (c == nnot)
00374                 *bufnext++ = m_not;
00375             c = *qpatnext++;
00376             do {
00377                 *bufnext++ = CHAR(c);
00378                 if (*qpatnext == RANGE &&
00379                     (c = qpatnext[1]) != RBRACKET) {
00380                     *bufnext++ = M_RNG;
00381                     *bufnext++ = CHAR(c);
00382                     qpatnext += 2;
00383                 }
00384             } while ((c = *qpatnext++) != RBRACKET);
00385             *bufnext++ = M_END;
00386             break;
00387         case QUESTION:
00388             pglob->gl_flags |= GLOB_MAGCHAR;
00389             *bufnext++ = M_ONE;
00390             break;
00391         case STAR:
00392             pglob->gl_flags |= GLOB_MAGCHAR;
00393             /* collapse adjacent stars to one, to avoid
00394              * exponential behavior
00395              */
00396             if (bufnext == patbuf || bufnext[-1] != M_ALL)
00397                 *bufnext++ = M_ALL;
00398             break;
00399         default:
00400             *bufnext++ = CHAR(c);
00401             break;
00402         }
00403     }
00404     *bufnext = EOS;
00405 #ifdef DEBUG
00406     qprintf(patbuf);
00407 #endif
00408 
00409     if ((err = glob1(patbuf, pglob, no_match)) != 0)
00410         return (err);
00411 
00412     /*
00413      * If there was no match we are going to append the pattern
00414      * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
00415      * and the pattern did not contain any magic characters
00416      * GLOB_NOMAGIC is there just for compatibility with csh.
00417      */
00418     if (pglob->gl_pathc == oldpathc &&
00419         ((flags & GLOB_NOCHECK) ||
00420          ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
00421         if (!(flags & GLOB_QUOTE)) {
00422             Char *dp = compilebuf;
00423             const unsigned char *sp = compilepat;
00424 
00425             while ((*dp++ = *sp++) != '\0')
00426                 continue;
00427         }
00428         else {
00429             /*
00430              * copy pattern, interpreting quotes; this is slightly different
00431              * than the interpretation of quotes above -- which should prevail?
00432              */
00433             while (*compilepat != EOS) {
00434                 if (*compilepat == QUOTE) {
00435                     if (*++compilepat == EOS)
00436                         --compilepat;
00437                 }
00438                 *compilebuf++ = (unsigned char) *compilepat++;
00439             }
00440             *compilebuf = EOS;
00441         }
00442         return (globextend(patbuf, pglob));
00443     }
00444     else if (!(flags & GLOB_NOSORT))
00445         qsort((char *) (pglob->gl_pathv + pglob->gl_offs + oldpathc),
00446               pglob->gl_pathc - oldpathc, sizeof(char *),
00447               (int (*) __P((const void *, const void *))) compare);
00448     return (0);
00449 }
00450 
00451 static int
00452 glob1(Char *pattern, glob_t *pglob, int no_match)
00453 {
00454     Char pathbuf[MAXPATHLEN + 1];
00455 
00456     /*
00457      * a null pathname is invalid -- POSIX 1003.1 sect. 2.4.
00458      */
00459     if (*pattern == EOS)
00460         return (0);
00461     return (glob2(pathbuf, pathbuf, pattern, pglob, no_match));
00462 }
00463 
00464 /*
00465  * functions glob2 and glob3 are mutually recursive; there is one level
00466  * of recursion for each segment in the pattern that contains one or
00467  * more meta characters.
00468  */
00469 static int
00470 glob2( Char *pathbuf,Char *pathend, Char *pattern, glob_t *pglob, int no_match)
00471 {
00472     struct stat sbuf;
00473     int anymeta;
00474     Char *p, *q;
00475 
00476     /*
00477      * loop over pattern segments until end of pattern or until segment with
00478      * meta character found.
00479      */
00480     anymeta = 0;
00481     for (;;) {
00482         if (*pattern == EOS) {  /* end of pattern? */
00483             *pathend = EOS;
00484 
00485             if (Lstat(pathbuf, &sbuf))
00486                 return (0);
00487 
00488             if (((pglob->gl_flags & GLOB_MARK) &&
00489                  pathend[-1] != SEP) &&
00490                 (S_ISDIR(sbuf.st_mode)
00491 #ifdef S_IFLNK
00492                  || (S_ISLNK(sbuf.st_mode) &&
00493                      (Stat(pathbuf, &sbuf) == 0) &&
00494                      S_ISDIR(sbuf.st_mode))
00495 #endif
00496                  )) {
00497                 *pathend++ = SEP;
00498                 *pathend = EOS;
00499             }
00500             ++pglob->gl_matchc;
00501             return (globextend(pathbuf, pglob));
00502         }
00503 
00504         /* find end of next segment, copy tentatively to pathend */
00505         q = pathend;
00506         p = pattern;
00507         while (*p != EOS && *p != SEP) {
00508             if (ismeta(*p))
00509                 anymeta = 1;
00510             *q++ = *p++;
00511         }
00512 
00513         if (!anymeta) {         /* no expansion, do next segment */
00514             pathend = q;
00515             pattern = p;
00516             while (*pattern == SEP)
00517                 *pathend++ = *pattern++;
00518         }
00519         else                    /* need expansion, recurse */
00520             return (glob3(pathbuf, pathend, pattern, p, pglob, no_match));
00521     }
00522     /* NOTREACHED */
00523 }
00524 
00525 
00526 static int
00527 glob3(Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob, int no_match)
00528 {
00529 #if 0
00530     extern int errno;
00531 #endif
00532     DIR    *dirp;
00533     struct dirent *dp;
00534     int     err;
00535     Char m_not = (pglob->gl_flags & GLOB_ALTNOT) ? M_ALTNOT : M_NOT;
00536     char cpathbuf[MAXPATHLEN], *ptr;
00537 #ifdef SOLARIS_DIRENT_PATCH
00538     /* declaration of vars used in the solaris-patch */
00539     char dname[255];
00540     int ii;
00541 #endif
00542 
00543     *pathend = EOS;
00544 
00545 #if 0
00546     errno = 0;
00547 #endif
00548 
00549     if (!(dirp = Opendir(pathbuf))) {
00550         /* todo: don't call for ENOENT or ENOTDIR? */
00551         for (ptr = cpathbuf; (*ptr++ = (char) *pathbuf++) != EOS;)
00552             continue;
00553 #if 0
00554         if ((pglob->gl_errfunc && (*pglob->gl_errfunc) (cpathbuf, errno)) ||
00555             (pglob->gl_flags & GLOB_ERR))
00556 #else
00557         if ( (pglob->gl_flags & GLOB_ERR))
00558 #endif
00559             return (GLOB_ABEND);
00560         else
00561             return (0);
00562     }
00563 
00564     err = 0;
00565 
00566     /* search directory for matching names */
00567     while ((dp = readdir(dirp)) != NULL) {
00568         register unsigned char *sc;
00569         register Char *dc;
00570 
00571 #ifdef SOLARIS_DIRENT_PATCH
00572         /**********
00573         begin patch
00574         **********/
00575 
00576 #ifndef SOLARIS_DIRENT_ZERO
00577         for (ii = -2 ; dp->d_name[ii] != '\0' ; ++ii) {
00578           dname[ii+2] = dp->d_name[ii];
00579         }
00580         dname[ii+2] = '\0';
00581 #else
00582         strcpy(dname, dp->d_name); /* John Koger, March 1999 */
00583 #endif
00584         /**********
00585         end patch
00586         now use dname for dp->d_name
00587         **********/
00588 
00589         /* initial DOT must be matched literally */
00590         if (dname[0] == DOT && *pattern != DOT)
00591             continue;
00592         for (sc = (unsigned char *) dname, dc = pathend;
00593 #else
00594         if (dp->d_name[0] == DOT && *pattern != DOT)
00595             continue;
00596         for (sc = (unsigned char *) dp->d_name, dc = pathend;
00597 #endif
00598              (*dc++ = *sc++) != '\0';)
00599             continue;
00600         if (match(pathend, pattern, restpattern, (int) m_not) == no_match) {
00601             *pathend = EOS;
00602             continue;
00603         }
00604         err = glob2(pathbuf, --dc, restpattern, pglob, no_match);
00605         if (err)
00606             break;
00607     }
00608     /* todo: check error from readdir? */
00609     (void) closedir(dirp);
00610     return (err);
00611 }
00612 
00613 
00614 /*
00615  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
00616  * add the new item, and update gl_pathc.
00617  *
00618  * This assumes the BSD realloc, which only copies the block when its size
00619  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
00620  * behavior.
00621  *
00622  * Return 0 if new item added, error code if memory couldn't be allocated.
00623  *
00624  * Invariant of the glob_t structure:
00625  *      Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
00626  *       gl_pathv points to (gl_offs + gl_pathc + 1) items.
00627  */
00628 static int
00629 globextend(Char *path, glob_t *pglob)
00630 {
00631     register char **pathv;
00632     register int i;
00633     unsigned int newsize;
00634     char   *copy;
00635     Char *p;
00636 
00637     newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
00638     pathv = (char **) (pglob->gl_pathv ?
00639                        xrealloc((ptr_t) pglob->gl_pathv, (size_t) newsize) :
00640                        xmalloc((size_t) newsize));
00641     if (pathv == NULL)
00642         return (GLOB_NOSPACE);
00643 
00644     if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
00645         /* first time around -- clear initial gl_offs items */
00646         pathv += pglob->gl_offs;
00647         for (i = pglob->gl_offs; --i >= 0;)
00648             *--pathv = NULL;
00649     }
00650     pglob->gl_pathv = pathv;
00651 
00652     for (p = path; *p++;)
00653         continue;
00654     if ((copy = (char *) xmalloc((size_t) (p - path))) != NULL) {
00655         register char *dc = copy;
00656         register Char *sc = path;
00657 
00658         while ((*dc++ = *sc++) != '\0')
00659             continue;
00660         pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
00661     }
00662     pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
00663     return ((copy == NULL) ? GLOB_NOSPACE : 0);
00664 }
00665 
00666 
00667 /*
00668  * pattern matching function for filenames.  Each occurrence of the *
00669  * pattern causes a recursion level.
00670  */
00671 static  int
00672 match(Char *name, Char *pat, Char *patend, int m_not)
00673 {
00674     int ok, negate_range;
00675     Char c, k;
00676 
00677     while (pat < patend) {
00678         c = *pat++;
00679         switch (c & M_MASK) {
00680         case M_ALL:
00681             if (pat == patend)
00682                 return (1);
00683             do
00684                 if (match(name, pat, patend, m_not))
00685                     return (1);
00686             while (*name++ != EOS);
00687             return (0);
00688         case M_ONE:
00689             if (*name++ == EOS)
00690                 return (0);
00691             break;
00692         case M_SET:
00693             ok = 0;
00694             if ((k = *name++) == EOS)
00695                 return (0);
00696             if ((negate_range = ((*pat & M_MASK) == m_not)) != 0)
00697                 ++pat;
00698             while (((c = *pat++) & M_MASK) != M_END) {
00699                 if ((*pat & M_MASK) == M_RNG) {
00700                     if (c <= k && k <= pat[1])
00701                         ok = 1;
00702                     pat += 2;
00703                 }
00704                 else if (c == k)
00705                     ok = 1;
00706             }
00707             if (ok == negate_range)
00708                 return (0);
00709             break;
00710         default:
00711             k = *name++;
00712             if (k != c)
00713                 return (0);
00714             break;
00715         }
00716     }
00717     return (*name == EOS);
00718 }
00719 
00720 /* free allocated data belonging to a glob_t structure */
00721 void
00722 globfree(glob_t *pglob)
00723 {
00724     register int i;
00725     register char **pp;
00726 
00727     if (pglob->gl_pathv != NULL) {
00728         pp = pglob->gl_pathv + pglob->gl_offs;
00729         for (i = pglob->gl_pathc; i--; ++pp)
00730             if (*pp)
00731                 xfree((ptr_t) *pp), *pp = NULL;
00732         xfree((ptr_t) pglob->gl_pathv), pglob->gl_pathv = NULL;
00733     }
00734 }
00735 
00736 /*****************************************************************************
00737    Major portions of this software are copyrighted by the Medical College
00738    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00739    License, Version 2.  See the file README.Copyright for details.
00740 ******************************************************************************/
00741 
00742 /*! set the direction of the sort (either small to large, or the reverse)    */
00743 int rglob_set_sort_dir( int dir )                     /* 14 Feb 2005 [rickr] */
00744 {
00745    if ( dir == 1 )       g_sort_dir =  1;
00746    else if ( dir == -1 ) g_sort_dir = -1;
00747    else                  return 1;          /* else, ignore and signal error */
00748                                                                                 
00749    return 0;
00750 }
00751 
00752 
00753 static int warn = 0 ;
00754 void MCW_warn_expand( int www ){ warn = www; return; }  /* 13 Jul 2001 */
00755 
00756 /*------------------------------------------------------------------------*/
00757 /*! Routines that allows filename wildcarding to be handled inside
00758     to3d.  The advantage: limitations of shell command line lengths.
00759      - 29 July 1996:  Incorporated "glob" functions from tcsh-6.05, rather
00760                        than rely on system supplying a library.
00761      - 30 July 1996:  Extended routine to allow for 3D: type prefixes.
00762      - 10 Feb  2000:  and for 3A: prefixes.
00763 --------------------------------------------------------------------------*/
00764 
00765 void MCW_file_expand( int nin , char ** fin , int * nout , char *** fout )
00766 {
00767    glob_t gl ;
00768    int    ii , gnum, gold , ilen ;
00769    char ** gout ;
00770    char *  fn ;
00771    char prefix[4] , fpre[128] , fname[256] ;
00772    int  b1,b2,b3,b4,b5 , ib,ig , lpre=0 ;
00773 
00774    if( nin <= 0 ){ *nout = 0 ; return ; }
00775 
00776    gnum = 0 ;
00777    gout = NULL ;
00778 
00779    for( ii=0 ; ii < nin ; ii++ ){
00780       fn = fin[ii] ;
00781 
00782       ig = 0 ;
00783 
00784       /** look for 3D: prefix **/
00785 
00786       if( strlen(fn) > 9 && fn[0] == '3' && fn[1] == 'D' ){
00787          ib = 0 ;
00788          prefix[ib++] = '3' ;
00789          prefix[ib++] = 'D' ;
00790          if( fn[2] == ':' ){ prefix[ib++] = '\0' ; }
00791          else              { prefix[ib++] = fn[2] ; prefix[ib++] = '\0' ; }
00792 
00793          ig = sscanf( fn+ib , "%d:%d:%d:%d:%d:%s" ,     /* must scan all */
00794                       &b1,&b2,&b3,&b4,&b5 , fname ) ;   /* six items OK  */
00795 
00796          /** if have all 6 3D: items, then make a 3D: prefix for output **/
00797 
00798          if( ig == 6 ){
00799             sprintf(fpre , "%s:%d:%d:%d:%d:%d:" , prefix,b1,b2,b3,b4,b5) ;
00800             lpre = strlen(fpre) ;
00801          } else {
00802             ig = 0 ;
00803          }
00804       }
00805 
00806       if( strlen(fn) > 9 && fn[0] == '3' && fn[1] == 'A' && fn[3] == ':' ){
00807          ib = 0 ;
00808          prefix[ib++] = '3' ;
00809          prefix[ib++] = 'A' ;
00810          prefix[ib++] = fn[2] ;
00811          prefix[ib++] = '\0' ;
00812 
00813          ig = sscanf( fn+ib , "%d:%d:%d:%s" ,  /* must scan all */
00814                       &b1,&b2,&b3, fname ) ;   /* four items OK */
00815 
00816          /** if have all 4 3A: items, then make a 3A: prefix for output **/
00817 
00818          if( ig == 4 ){
00819             sprintf(fpre , "%s:%d:%d:%d:" , prefix,b1,b2,b3) ;
00820             lpre = strlen(fpre) ;
00821          } else {
00822             ig = 0 ;
00823          }
00824       }
00825 
00826       if( ig > 0 ) (void) glob( fname , 0 , NULL ,  &gl ) ;  /* 3D: was OK */
00827       else         (void) glob( fn    , 0 , NULL ,  &gl ) ;  /*     not OK */
00828 
00829       /** put each matched string into the output array **/
00830 
00831       if( gl.gl_pathc > 0 ){
00832 
00833          /** make space for output now **/
00834          gold  = gnum ;
00835          gnum += gl.gl_pathc ;
00836          if( gout == NULL ) gout = (char **) malloc (      sizeof(char *)*gnum);
00837          else               gout = (char **) realloc(gout, sizeof(char *)*gnum);
00838 
00839          for( ib=0 ; ib < gl.gl_pathc ; ib++ ){
00840             ilen = strlen( gl.gl_pathv[ib] ) + 1 ;  /* length of this name */
00841             if( ig > 0 ) ilen += lpre ;             /* plus 3D: prefix?    */
00842 
00843             gout[ib+gold] = (char *) malloc( sizeof(char) * ilen ) ; /* output! */
00844 
00845             if( ig > 0 ){
00846                strcpy( gout[ib+gold] , fpre ) ;             /* 3D: prefix */
00847                strcat( gout[ib+gold] , gl.gl_pathv[ib] ) ;  /* then name  */
00848             }
00849             else {
00850                strcpy( gout[ib+gold] , gl.gl_pathv[ib] ) ;  /* just name */
00851             }
00852          }
00853 
00854       } else if( ig == 6 && strcmp(fname,"ALLZERO") == 0 ){ /* 06 Mar 2001 */
00855 
00856          gold = gnum ; gnum++ ;
00857          if( gout == NULL ) gout = (char **) malloc (      sizeof(char *)*gnum);
00858          else               gout = (char **) realloc(gout, sizeof(char *)*gnum);
00859 
00860          ilen = lpre + strlen(fname) + 1 ;
00861          gout[gold] = (char *) malloc( sizeof(char) * ilen ) ; /* output! */
00862          strcpy( gout[gold] , fpre ) ;
00863          strcat( gout[gold] , fname ) ;
00864 
00865       } else {  /* 30 Apr 2001 */
00866 
00867          if( warn )  /* 13 Jul 2001 - print only if told to do so */
00868            fprintf(stderr,"** Can't find file %s\n", (ig>0) ? fname : fn ) ;
00869       }
00870 
00871       globfree( &gl ) ;
00872    }
00873 
00874    *nout = gnum ; *fout = gout ; return ;
00875 }
00876 
00877 /*-----------------------------------------------------------------------*/
00878 /*! Simpler interface to MCW_file_expand().
00879       - fnam = string of form "*.zork fred*.* ?a?b"; e.g., 1 or more
00880                wildcards
00881       - nout = pointer to output count
00882       - fout = pointer to output list of strings.
00883 
00884     Sample usage:
00885       int nfile ; char **flist ;
00886       MCW_wildcards( "*.jpg *.JPG" , &nfile , &flist ) ;
00887        ... do something with flist[0]..flist[nfile-1] if nfile > 0 ...
00888       MCW_free_wildcards( nfile , flist ) ;
00889 -------------------------------------------------------------------------*/
00890 
00891 void MCW_wildcards( char *fnam , int *nout , char ***fout )  /* 01 Dec 2003 */
00892 {
00893    char **fin=NULL, *fcop ;
00894    int ii , nin , lf , ls ;
00895 
00896    if( fnam == NULL || *fnam == '\0' ){ *nout = 0 ; return ; }
00897    fcop = strdup(fnam) ; lf = strlen(fcop) ;
00898    ls = 1 ;
00899    for( nin=ii=0 ; ii < lf ; ii++ ){
00900      if( isspace(fcop[ii]) ){   /* This is a blank, so next */
00901        ls = 1 ;                 /*  non-blank is a new word. */
00902        fcop[ii] = '\0' ;        /* Set this char to NUL.      */
00903 
00904      } else {                   /* Not a blank. */
00905 
00906        if( ls ){                /* If last was a blank, is new name. */
00907          fin = (char **) realloc( fin , sizeof(char *)*(nin+1) ) ;
00908          fin[nin++] = fcop+ii ;
00909        }
00910        ls = 0 ;
00911      }
00912    }
00913 
00914    if( nin == 0 ){ *nout = 0 ; free(fcop) ; return ; }
00915 
00916    MCW_file_expand( nin , fin , nout , fout ) ;
00917    free(fin) ; free(fcop) ; return ;
00918 }
00919 
00920 /*-----------------------------------------------------------------------*/
00921 
00922 void MCW_free_expand( int gnum , char ** gout )
00923 {
00924    int ii ;
00925 
00926    if( gout == NULL ) return ;
00927 
00928    for( ii=0 ; ii < gnum ; ii++ ) free( gout[ii] ) ;
00929    free( gout ) ;
00930    return ;
00931 }
 

Powered by Plone

This site conforms to the following standards: