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 File Reference

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include "mcw_glob.h"

Go to the source code of this file.


Defines

#define xfree   free
#define xmalloc   malloc
#define xrealloc   realloc
#define S_ISDIR(a)   (((a) & S_IFMT) == S_IFDIR)
#define lstat   stat
#define __P(a)   a
#define DOLLAR   '$'
#define DOT   '.'
#define EOS   '\0'
#define LBRACKET   '['
#define NOT   '!'
#define ALTNOT   '^'
#define QUESTION   '?'
#define QUOTE   '\\'
#define RANGE   '-'
#define RBRACKET   ']'
#define SEP   '/'
#define STAR   '*'
#define TILDE   '~'
#define UNDERSCORE   '_'
#define M_META   0x8000
#define M_PROTECT   0x4000
#define M_MASK   0xffff
#define M_ASCII   0x00ff
#define CHAR(c)   ((c)&M_ASCII)
#define META(c)   ((c)|M_META)
#define M_ALL   META('*')
#define M_END   META(']')
#define M_NOT   META('!')
#define M_ALTNOT   META('^')
#define M_ONE   META('?')
#define M_RNG   META('-')
#define M_SET   META('[')
#define ismeta(c)   (((c)&M_META) != 0)
#define Lstat   Stat

Typedefs

typedef void * ptr_t
typedef unsigned short Char

Functions

int glob1 __P ((Char *, glob_t *, int))
int glob2 __P ((Char *, Char *, Char *, glob_t *, int))
int glob3 __P ((Char *, Char *, Char *, Char *, glob_t *, int))
int globextend __P ((Char *, glob_t *))
int match __P ((Char *, Char *, Char *, int))
int compare __P ((const ptr_t, const ptr_t))
DIR *Opendir __P ((Char *))
Char *Strchr __P ((Char *, int))
DIR * Opendir (Char *str)
int Stat (Char *fn, struct stat *sb)
CharStrchr (Char *str, int ch)
int compare (const ptr_t p, const ptr_t q)
int glob (const char *pattern, int flags, int(*errfunc)(char *, int), glob_t *pglob)
int glob1 (Char *pattern, glob_t *pglob, int no_match)
int glob2 (Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob, int no_match)
int glob3 (Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob, int no_match)
int globextend (Char *path, glob_t *pglob)
int match (Char *name, Char *pat, Char *patend, int m_not)
void globfree (glob_t *pglob)
void MCW_warn_expand (int www)
void MCW_file_expand (int nin, char **fin, int *nout, char ***fout)
void MCW_wildcards (char *fnam, int *nout, char ***fout)
void MCW_free_expand (int gnum, char **gout)

Variables

int warn = 0

Define Documentation

#define __P a       a
 

Definition at line 112 of file mcw_glob.c.

#define ALTNOT   '^'
 

Definition at line 137 of file mcw_glob.c.

Referenced by glob().

#define CHAR c       ((c)&M_ASCII)
 

Definition at line 152 of file mcw_glob.c.

#define DOLLAR   '$'
 

Definition at line 132 of file mcw_glob.c.

#define DOT   '.'
 

Definition at line 133 of file mcw_glob.c.

Referenced by glob3().

#define EOS   '\0'
 

Definition at line 134 of file mcw_glob.c.

Referenced by glob(), glob1(), glob2(), glob3(), and match().

#define ismeta c       (((c)&M_META) != 0)
 

Definition at line 161 of file mcw_glob.c.

#define LBRACKET   '['
 

Definition at line 135 of file mcw_glob.c.

Referenced by glob().

#define Lstat   Stat
 

Definition at line 219 of file mcw_glob.c.

Referenced by glob2().

#define lstat   stat
 

Definition at line 106 of file mcw_glob.c.

#define M_ALL   META('*')
 

Definition at line 154 of file mcw_glob.c.

Referenced by glob(), and match().

#define M_ALTNOT   META('^')
 

Definition at line 157 of file mcw_glob.c.

Referenced by glob(), and glob3().

#define M_ASCII   0x00ff
 

Definition at line 150 of file mcw_glob.c.

#define M_END   META(']')
 

Definition at line 155 of file mcw_glob.c.

Referenced by glob(), and match().

#define M_MASK   0xffff
 

Definition at line 149 of file mcw_glob.c.

Referenced by match().

#define M_META   0x8000
 

Definition at line 147 of file mcw_glob.c.

#define M_NOT   META('!')
 

Definition at line 156 of file mcw_glob.c.

Referenced by glob(), and glob3().

#define M_ONE   META('?')
 

Definition at line 158 of file mcw_glob.c.

Referenced by glob(), and match().

#define M_PROTECT   0x4000
 

Definition at line 148 of file mcw_glob.c.

Referenced by glob().

#define M_RNG   META('-')
 

Definition at line 159 of file mcw_glob.c.

Referenced by glob(), and match().

#define M_SET   META('[')
 

Definition at line 160 of file mcw_glob.c.

Referenced by glob(), and match().

#define META c       ((c)|M_META)
 

Definition at line 153 of file mcw_glob.c.

#define NOT   '!'
 

Definition at line 136 of file mcw_glob.c.

Referenced by glob().

#define QUESTION   '?'
 

Definition at line 138 of file mcw_glob.c.

Referenced by glob().

#define QUOTE   '\\'
 

Definition at line 139 of file mcw_glob.c.

Referenced by glob().

#define RANGE   '-'
 

Definition at line 140 of file mcw_glob.c.

Referenced by glob().

#define RBRACKET   ']'
 

Definition at line 141 of file mcw_glob.c.

Referenced by glob().

#define S_ISDIR a       (((a) & S_IFMT) == S_IFDIR)
 

don't use sh.h any more *

Definition at line 98 of file mcw_glob.c.

#define SEP   '/'
 

Definition at line 142 of file mcw_glob.c.

Referenced by glob2().

#define STAR   '*'
 

Definition at line 143 of file mcw_glob.c.

Referenced by glob().

#define TILDE   '~'
 

Definition at line 144 of file mcw_glob.c.

#define UNDERSCORE   '_'
 

Definition at line 145 of file mcw_glob.c.

#define xfree   free
 

the following were in "sh.h", but I put them here to get rid of the need for that file -- RWCox *

Definition at line 66 of file mcw_glob.c.

#define xmalloc   malloc
 

Definition at line 67 of file mcw_glob.c.

#define xrealloc   realloc
 

Definition at line 68 of file mcw_glob.c.


Typedef Documentation

typedef unsigned short Char
 

Definition at line 109 of file mcw_glob.c.

Referenced by glob(), glob1(), glob2(), glob3(), globextend(), match(), Opendir(), Stat(), and Strchr().

typedef void* ptr_t
 

Definition at line 79 of file mcw_glob.c.

Referenced by compare(), globextend(), and globfree().


Function Documentation

Char* Strchr __P (Char *, int)    [static]
 

DIR* Opendir __P (Char *)    [static]
 

int compare __P (const ptr_t, const ptr_t   [static]
 

int match __P (Char *, Char *, Char *, int)    [static]
 

int globextend __P (Char *, glob_t *)    [static]
 

int glob3 __P (Char *, Char *, Char *, Char *, glob_t *, int)    [static]
 

int glob2 __P (Char *, Char *, Char *, glob_t *, int)    [static]
 

int glob1 __P (Char *, glob_t *, int)    [static]
 

int compare const ptr_t    p,
const ptr_t    q
[static]
 

Definition at line 273 of file mcw_glob.c.

References p, ptr_t, and q.

Referenced by glob().

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 }

int glob const char *    pattern,
int    flags,
int(*    errfunc)(char *, int),
glob_t   pglob
 

Definition at line 295 of file mcw_glob.c.

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 }

int glob1 Char   pattern,
glob_t   pglob,
int    no_match
[static]
 

Definition at line 449 of file mcw_glob.c.

References Char, EOS, glob2(), MAXPATHLEN, no_match, and pglob.

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 }

int glob2 Char   pathbuf,
Char   pathend,
Char   pattern,
glob_t   pglob,
int    no_match
[static]
 

Definition at line 467 of file mcw_glob.c.

References Char, EOS, glob_t::gl_flags, glob_t::gl_matchc, glob3(), globextend(), ismeta, Lstat, no_match, p, pathend, pglob, q, S_ISDIR, SEP, and Stat().

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 }

int glob3 Char   pathbuf,
Char   pathend,
Char   pattern,
Char   restpattern,
glob_t   pglob,
int    no_match
[static]
 

Definition at line 524 of file mcw_glob.c.

References Char, DIR, DOT, EOS, glob_t::gl_errfunc, glob_t::gl_flags, glob2(), M_ALTNOT, M_NOT, m_not, match(), MAXPATHLEN, no_match, Opendir(), pathend, pglob, and restpattern.

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 }

int globextend Char   path,
glob_t   pglob
[static]
 

Definition at line 626 of file mcw_glob.c.

References Char, glob_t::gl_offs, glob_t::gl_pathc, glob_t::gl_pathv, i, p, pglob, ptr_t, xmalloc, and xrealloc.

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 }

void globfree glob_t   pglob
 

Definition at line 719 of file mcw_glob.c.

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 }

int match Char   name,
Char   pat,
Char   patend,
int    m_not
[static]
 

Definition at line 669 of file mcw_glob.c.

References c, Char, EOS, M_ALL, M_END, M_MASK, m_not, M_ONE, M_RNG, M_SET, match(), name, pat, and patend.

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 }

void MCW_file_expand int    nin,
char **    fin,
int *    nout,
char ***    fout
 

Routines that allows filename wildcarding to be handled inside to3d. The advantage: limitations of shell command line lengths.

  • 29 July 1996: Incorporated "glob" functions from tcsh-6.05, rather than rely on system supplying a library.
  • 30 July 1996: Extended routine to allow for 3D: type prefixes.
  • 10 Feb 2000: and for 3A: prefixes. --------------------------------------------------------------------------

Definition at line 751 of file mcw_glob.c.

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 }

void MCW_free_expand int    gnum,
char **    gout
 

make space for output now *

Definition at line 908 of file mcw_glob.c.

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 }

void MCW_warn_expand int    www
 

Definition at line 740 of file mcw_glob.c.

00740 { warn = www; return; }  /* 13 Jul 2001 */

void MCW_wildcards char *    fnam,
int *    nout,
char ***    fout
 

Simpler interface to MCW_file_expand().

  • fnam = string of form "*.zork fred*.* ?a?b"; e.g., 1 or more wildcards
  • nout = pointer to output count
  • fout = pointer to output list of strings.
Sample usage: int nfile ; char **flist ; MCW_wildcards( "*.jpg *.JPG" , &nfile , &flist ) ; ... do something with flist[0]..flist[nfile-1] if nfile > 0 ... MCW_free_wildcards( nfile , flist ) ; -------------------------------------------------------------------------

Definition at line 877 of file mcw_glob.c.

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 }

DIR* Opendir Char   str [static]
 

Definition at line 184 of file mcw_glob.c.

References Char, and MAXPATHLEN.

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 }

int Stat Char   fn,
struct stat *    sb
[static]
 

Definition at line 223 of file mcw_glob.c.

References Char, MAXPATHLEN, S_ISDIR, and 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 }

Char* Strchr Char   str,
int    ch
[static]
 

Definition at line 245 of file mcw_glob.c.

References Char.

00246 {
00247     do
00248         if (*str == ch)
00249             return (str);
00250     while (*str++);
00251     return (NULL);
00252 }

Variable Documentation

int warn = 0 [static]
 

Definition at line 739 of file mcw_glob.c.

Referenced by MCW_warn_expand().

 

Powered by Plone

This site conforms to the following standards: