Back to index

cell-binutils  2.17cvs20070401
Defines | Functions | Variables
fnmatch.c File Reference
#include <errno.h>
#include <fnmatch.h>
#include <safe-ctype.h>

Go to the source code of this file.

Defines

#define _GNU_SOURCE
#define const
#define FOLD(c)   ((flags & FNM_CASEFOLD) ? TOLOWER (c) : (c))

Functions

int fnmatch (const char *pattern, const char *string, int flags)

Variables

int errno

Define Documentation

#define _GNU_SOURCE

Definition at line 34 of file fnmatch.c.

#define const

Definition at line 42 of file fnmatch.c.

#define FOLD (   c)    ((flags & FNM_CASEFOLD) ? TOLOWER (c) : (c))

Function Documentation

int fnmatch ( const char *  pattern,
const char *  string,
int  flags 
)

Definition at line 68 of file fnmatch.c.

{
  register const char *p = pattern, *n = string;
  register unsigned char c;

#define FOLD(c)      ((flags & FNM_CASEFOLD) ? TOLOWER (c) : (c))

  while ((c = *p++) != '\0')
    {
      c = FOLD (c);

      switch (c)
       {
       case '?':
         if (*n == '\0')
           return FNM_NOMATCH;
         else if ((flags & FNM_FILE_NAME) && *n == '/')
           return FNM_NOMATCH;
         else if ((flags & FNM_PERIOD) && *n == '.' &&
                 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
           return FNM_NOMATCH;
         break;

       case '\\':
         if (!(flags & FNM_NOESCAPE))
           {
             c = *p++;
             c = FOLD (c);
           }
         if (FOLD ((unsigned char)*n) != c)
           return FNM_NOMATCH;
         break;

       case '*':
         if ((flags & FNM_PERIOD) && *n == '.' &&
             (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
           return FNM_NOMATCH;

         for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
           if (((flags & FNM_FILE_NAME) && *n == '/') ||
              (c == '?' && *n == '\0'))
             return FNM_NOMATCH;

         if (c == '\0')
           return 0;

         {
           unsigned char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
           c1 = FOLD (c1);
           for (--p; *n != '\0'; ++n)
             if ((c == '[' || FOLD ((unsigned char)*n) == c1) &&
                fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
              return 0;
           return FNM_NOMATCH;
         }

       case '[':
         {
           /* Nonzero if the sense of the character class is inverted.  */
           register int negate;

           if (*n == '\0')
             return FNM_NOMATCH;

           if ((flags & FNM_PERIOD) && *n == '.' &&
              (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
             return FNM_NOMATCH;

           negate = (*p == '!' || *p == '^');
           if (negate)
             ++p;

           c = *p++;
           for (;;)
             {
              register unsigned char cstart = c, cend = c;

              if (!(flags & FNM_NOESCAPE) && c == '\\')
                cstart = cend = *p++;

              cstart = cend = FOLD (cstart);

              if (c == '\0')
                /* [ (unterminated) loses.  */
                return FNM_NOMATCH;

              c = *p++;
              c = FOLD (c);

              if ((flags & FNM_FILE_NAME) && c == '/')
                /* [/] can never match.  */
                return FNM_NOMATCH;

              if (c == '-' && *p != ']')
                {
                  cend = *p++;
                  if (!(flags & FNM_NOESCAPE) && cend == '\\')
                    cend = *p++;
                  if (cend == '\0')
                    return FNM_NOMATCH;
                  cend = FOLD (cend);

                  c = *p++;
                }

              if (FOLD ((unsigned char)*n) >= cstart
                  && FOLD ((unsigned char)*n) <= cend)
                goto matched;

              if (c == ']')
                break;
             }
           if (!negate)
             return FNM_NOMATCH;
           break;

         matched:;
           /* Skip the rest of the [...] that already matched.  */
           while (c != ']')
             {
              if (c == '\0')
                /* [... (unterminated) loses.  */
                return FNM_NOMATCH;

              c = *p++;
              if (!(flags & FNM_NOESCAPE) && c == '\\')
                /* XXX 1003.2d11 is unclear if this is right.  */
                ++p;
             }
           if (negate)
             return FNM_NOMATCH;
         }
         break;

       default:
         if (c != FOLD ((unsigned char)*n))
           return FNM_NOMATCH;
       }

      ++n;
    }

  if (*n == '\0')
    return 0;

  if ((flags & FNM_LEADING_DIR) && *n == '/')
    /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
    return 0;

  return FNM_NOMATCH;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation