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

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include "l_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)
int rglob_set_sort_dir (int dir)
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 g_sort_dir = 1
int warn = 0

Define Documentation

#define __P a       a
 

Definition at line 115 of file l_mcw_glob.c.

#define ALTNOT   '^'
 

Definition at line 140 of file l_mcw_glob.c.

Referenced by glob().

#define CHAR c       ((c)&M_ASCII)
 

Definition at line 155 of file l_mcw_glob.c.

#define DOLLAR   '$'
 

Definition at line 135 of file l_mcw_glob.c.

#define DOT   '.'
 

Definition at line 136 of file l_mcw_glob.c.

Referenced by glob3().

#define EOS   '\0'
 

Definition at line 137 of file l_mcw_glob.c.

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

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

Definition at line 164 of file l_mcw_glob.c.

#define LBRACKET   '['
 

Definition at line 138 of file l_mcw_glob.c.

Referenced by glob().

#define Lstat   Stat
 

Definition at line 222 of file l_mcw_glob.c.

Referenced by glob2().

#define lstat   stat
 

Definition at line 109 of file l_mcw_glob.c.

#define M_ALL   META('*')
 

Definition at line 157 of file l_mcw_glob.c.

Referenced by glob(), and match().

#define M_ALTNOT   META('^')
 

Definition at line 160 of file l_mcw_glob.c.

Referenced by glob(), and glob3().

#define M_ASCII   0x00ff
 

Definition at line 153 of file l_mcw_glob.c.

#define M_END   META(']')
 

Definition at line 158 of file l_mcw_glob.c.

Referenced by glob(), and match().

#define M_MASK   0xffff
 

Definition at line 152 of file l_mcw_glob.c.

Referenced by match().

#define M_META   0x8000
 

Definition at line 150 of file l_mcw_glob.c.

#define M_NOT   META('!')
 

Definition at line 159 of file l_mcw_glob.c.

Referenced by glob(), and glob3().

#define M_ONE   META('?')
 

Definition at line 161 of file l_mcw_glob.c.

Referenced by glob(), and match().

#define M_PROTECT   0x4000
 

Definition at line 151 of file l_mcw_glob.c.

Referenced by glob().

#define M_RNG   META('-')
 

Definition at line 162 of file l_mcw_glob.c.

Referenced by glob(), and match().

#define M_SET   META('[')
 

Definition at line 163 of file l_mcw_glob.c.

Referenced by glob(), and match().

#define META c       ((c)|M_META)
 

Definition at line 156 of file l_mcw_glob.c.

#define NOT   '!'
 

Definition at line 139 of file l_mcw_glob.c.

Referenced by glob().

#define QUESTION   '?'
 

Definition at line 141 of file l_mcw_glob.c.

Referenced by glob().

#define QUOTE   '\\'
 

Definition at line 142 of file l_mcw_glob.c.

Referenced by glob().

#define RANGE   '-'
 

Definition at line 143 of file l_mcw_glob.c.

Referenced by glob().

#define RBRACKET   ']'
 

Definition at line 144 of file l_mcw_glob.c.

Referenced by glob().

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

Definition at line 101 of file l_mcw_glob.c.

#define SEP   '/'
 

Definition at line 145 of file l_mcw_glob.c.

Referenced by glob2().

#define STAR   '*'
 

Definition at line 146 of file l_mcw_glob.c.

Referenced by glob().

#define TILDE   '~'
 

Definition at line 147 of file l_mcw_glob.c.

#define UNDERSCORE   '_'
 

Definition at line 148 of file l_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 l_mcw_glob.c.

#define xmalloc   malloc
 

Definition at line 67 of file l_mcw_glob.c.

#define xrealloc   realloc
 

Definition at line 68 of file l_mcw_glob.c.


Typedef Documentation

typedef unsigned short Char
 

Definition at line 112 of file l_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 l_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 276 of file l_mcw_glob.c.

References g_sort_dir, p, ptr_t, and q.

Referenced by glob(), LST_Sort(), and main().

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 }

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

Definition at line 298 of file l_mcw_glob.c.

References __P, ALTNOT, c, CHAR, Char, compare(), EOS, flags, glob_t::gl_errfunc, glob_t::gl_flags, glob_t::gl_matchc, glob_t::gl_offs, glob_t::gl_pathc, glob_t::gl_pathv, glob1(), globextend(), LBRACKET, M_ALL, M_ALTNOT, M_END, M_NOT, m_not, M_ONE, M_PROTECT, M_RNG, M_SET, MAXPATHLEN, no_match, NOT, pglob, QUESTION, QUOTE, RANGE, RBRACKET, STAR, and Strchr().

Referenced by MCW_file_expand().

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 }

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

Definition at line 452 of file l_mcw_glob.c.

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

Referenced by glob().

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 }

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

Definition at line 470 of file l_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().

Referenced by glob1(), and glob3().

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 }

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

Definition at line 527 of file l_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.

Referenced by glob2().

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 }

int globextend Char   path,
glob_t   pglob
[static]
 

Definition at line 629 of file l_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.

Referenced by glob(), and glob2().

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 }

void globfree glob_t   pglob
 

Definition at line 722 of file l_mcw_glob.c.

References glob_t::gl_offs, glob_t::gl_pathc, glob_t::gl_pathv, i, pglob, ptr_t, and xfree.

Referenced by MCW_file_expand().

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 }

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

Definition at line 672 of file l_mcw_glob.c.

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

Referenced by glob3(), match(), and match_table().

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 }

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 765 of file l_mcw_glob.c.

References fout, glob_t::gl_pathc, glob_t::gl_pathv, glob(), globfree(), ilen, malloc, and realloc.

Referenced by AFNI_read_inputs(), main(), MCW_wildcards(), NIH_glob(), read_ge_files(), T3D_read_images(), THD_get_all_filenames(), THD_get_wildcard_filenames(), and THD_init_session().

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 }

void MCW_free_expand int    gnum,
char **    gout
 

make space for output now *

Definition at line 922 of file l_mcw_glob.c.

References free.

Referenced by AFNI_read_inputs(), main(), NIH_glob_free(), read_ge_files(), T3D_read_images(), THD_get_all_filenames(), THD_get_wildcard_filenames(), and THD_init_session().

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 }

void MCW_warn_expand int    www
 

Definition at line 754 of file l_mcw_glob.c.

References warn.

Referenced by AFNI_read_inputs(), main(), T3D_read_images(), THD_get_all_filenames(), and THD_get_wildcard_filenames().

00754 { 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 891 of file l_mcw_glob.c.

References fout, free, MCW_file_expand(), and realloc.

Referenced by AFNI_find_jpegs(), and THD_init_session().

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 }

DIR* Opendir Char   str [static]
 

Definition at line 187 of file l_mcw_glob.c.

References Char, and MAXPATHLEN.

Referenced by glob3().

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 }

int rglob_set_sort_dir int    dir
 

set the direction of the sort (either small to large, or the reverse)

Definition at line 743 of file l_mcw_glob.c.

References g_sort_dir.

Referenced by init_options().

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 }

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

Definition at line 226 of file l_mcw_glob.c.

References Char, MAXPATHLEN, S_ISDIR, and sb.

Referenced by glob2().

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 }

Char* Strchr Char   str,
int    ch
[static]
 

Definition at line 248 of file l_mcw_glob.c.

References Char.

Referenced by glob().

00249 {
00250     do
00251         if (*str == ch)
00252             return (str);
00253     while (*str++);
00254     return (NULL);
00255 }

Variable Documentation

int g_sort_dir = 1 [static]
 

don't use sh.h any more *

Definition at line 98 of file l_mcw_glob.c.

Referenced by compare(), and rglob_set_sort_dir().

int warn = 0 [static]
 

Definition at line 753 of file l_mcw_glob.c.

Referenced by MCW_warn_expand().

 

Powered by Plone

This site conforms to the following standards: