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  

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

Powered by Plone

This site conforms to the following standards: