xref: /openbsd-src/gnu/usr.bin/cvs/lib/fnmatch.c (revision c71bc7e269286e43816004eb0fcd7a55f036cd69)
11e72d8d2Sderaadt /* Copyright (C) 1992 Free Software Foundation, Inc.
21e72d8d2Sderaadt This file is part of the GNU C Library.
31e72d8d2Sderaadt 
41e72d8d2Sderaadt The GNU C Library is free software; you can redistribute it and/or
51e72d8d2Sderaadt modify it under the terms of the GNU Library General Public License as
61e72d8d2Sderaadt published by the Free Software Foundation; either version 2 of the
71e72d8d2Sderaadt License, or (at your option) any later version.
81e72d8d2Sderaadt 
91e72d8d2Sderaadt The GNU C Library is distributed in the hope that it will be useful,
101e72d8d2Sderaadt but WITHOUT ANY WARRANTY; without even the implied warranty of
111e72d8d2Sderaadt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12461cc63eStholo Library General Public License for more details.  */
131e72d8d2Sderaadt 
141e72d8d2Sderaadt /* Modified slightly by Brian Berliner <berliner@sun.com> and
151e72d8d2Sderaadt    Jim Blandy <jimb@cyclic.com> for CVS use */
161e72d8d2Sderaadt 
171e72d8d2Sderaadt #ifdef HAVE_CONFIG_H
181e72d8d2Sderaadt #include "config.h"
191e72d8d2Sderaadt #endif
201e72d8d2Sderaadt 
21*c71bc7e2Stholo #include "system.h"
221e72d8d2Sderaadt 
231e72d8d2Sderaadt /* IGNORE(@ */
241e72d8d2Sderaadt /* #include <ansidecl.h> */
251e72d8d2Sderaadt /* @) */
261e72d8d2Sderaadt #include <errno.h>
271e72d8d2Sderaadt #include <fnmatch.h>
281e72d8d2Sderaadt 
291e72d8d2Sderaadt #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
301e72d8d2Sderaadt extern int errno;
311e72d8d2Sderaadt #endif
321e72d8d2Sderaadt 
331e72d8d2Sderaadt /* Match STRING against the filename pattern PATTERN, returning zero if
341e72d8d2Sderaadt    it matches, nonzero if not.  */
351e72d8d2Sderaadt int
361e72d8d2Sderaadt #if __STDC__
fnmatch(const char * pattern,const char * string,int flags)371e72d8d2Sderaadt fnmatch (const char *pattern, const char *string, int flags)
381e72d8d2Sderaadt #else
391e72d8d2Sderaadt fnmatch (pattern, string, flags)
401e72d8d2Sderaadt     char *pattern;
411e72d8d2Sderaadt     char *string;
421e72d8d2Sderaadt     int flags;
431e72d8d2Sderaadt #endif
441e72d8d2Sderaadt {
451e72d8d2Sderaadt   register const char *p = pattern, *n = string;
461e72d8d2Sderaadt   register char c;
471e72d8d2Sderaadt 
481e72d8d2Sderaadt   if ((flags & ~__FNM_FLAGS) != 0)
491e72d8d2Sderaadt     {
501e72d8d2Sderaadt       errno = EINVAL;
511e72d8d2Sderaadt       return -1;
521e72d8d2Sderaadt     }
531e72d8d2Sderaadt 
541e72d8d2Sderaadt   while ((c = *p++) != '\0')
551e72d8d2Sderaadt     {
561e72d8d2Sderaadt       switch (c)
571e72d8d2Sderaadt 	{
581e72d8d2Sderaadt 	case '?':
591e72d8d2Sderaadt 	  if (*n == '\0')
601e72d8d2Sderaadt 	    return FNM_NOMATCH;
611e72d8d2Sderaadt 	  else if ((flags & FNM_PATHNAME) && *n == '/')
621e72d8d2Sderaadt 	    return FNM_NOMATCH;
631e72d8d2Sderaadt 	  else if ((flags & FNM_PERIOD) && *n == '.' &&
641e72d8d2Sderaadt 		   (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
651e72d8d2Sderaadt 	    return FNM_NOMATCH;
661e72d8d2Sderaadt 	  break;
671e72d8d2Sderaadt 
681e72d8d2Sderaadt 	case '\\':
691e72d8d2Sderaadt 	  if (!(flags & FNM_NOESCAPE))
701e72d8d2Sderaadt 	    c = *p++;
71*c71bc7e2Stholo 	  if (FOLD_FN_CHAR (*n) != FOLD_FN_CHAR (c))
721e72d8d2Sderaadt 	    return FNM_NOMATCH;
731e72d8d2Sderaadt 	  break;
741e72d8d2Sderaadt 
751e72d8d2Sderaadt 	case '*':
761e72d8d2Sderaadt 	  if ((flags & FNM_PERIOD) && *n == '.' &&
771e72d8d2Sderaadt 	      (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
781e72d8d2Sderaadt 	    return FNM_NOMATCH;
791e72d8d2Sderaadt 
801e72d8d2Sderaadt 	  for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
811e72d8d2Sderaadt 	    if (((flags & FNM_PATHNAME) && *n == '/') ||
821e72d8d2Sderaadt 		(c == '?' && *n == '\0'))
831e72d8d2Sderaadt 	      return FNM_NOMATCH;
841e72d8d2Sderaadt 
851e72d8d2Sderaadt 	  if (c == '\0')
861e72d8d2Sderaadt 	    return 0;
871e72d8d2Sderaadt 
881e72d8d2Sderaadt 	  {
891e72d8d2Sderaadt 	    char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
901e72d8d2Sderaadt 	    for (--p; *n != '\0'; ++n)
91*c71bc7e2Stholo 	      if ((c == '[' || FOLD_FN_CHAR (*n) == FOLD_FN_CHAR (c1)) &&
921e72d8d2Sderaadt 		  fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
931e72d8d2Sderaadt 		return 0;
941e72d8d2Sderaadt 	    return FNM_NOMATCH;
951e72d8d2Sderaadt 	  }
961e72d8d2Sderaadt 
971e72d8d2Sderaadt 	case '[':
981e72d8d2Sderaadt 	  {
991e72d8d2Sderaadt 	    /* Nonzero if the sense of the character class is inverted.  */
1001e72d8d2Sderaadt 	    register int not;
1011e72d8d2Sderaadt 
1021e72d8d2Sderaadt 	    if (*n == '\0')
1031e72d8d2Sderaadt 	      return FNM_NOMATCH;
1041e72d8d2Sderaadt 
1051e72d8d2Sderaadt 	    if ((flags & FNM_PERIOD) && *n == '.' &&
1061e72d8d2Sderaadt 		(n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
1071e72d8d2Sderaadt 	      return FNM_NOMATCH;
1081e72d8d2Sderaadt 
1091e72d8d2Sderaadt 	    not = (*p == '!' || *p == '^');
1101e72d8d2Sderaadt 	    if (not)
1111e72d8d2Sderaadt 	      ++p;
1121e72d8d2Sderaadt 
1131e72d8d2Sderaadt 	    c = *p++;
1141e72d8d2Sderaadt 	    for (;;)
1151e72d8d2Sderaadt 	      {
1161e72d8d2Sderaadt 		register char cstart = c, cend = c;
1171e72d8d2Sderaadt 
1181e72d8d2Sderaadt 		if (!(flags & FNM_NOESCAPE) && c == '\\')
1191e72d8d2Sderaadt 		  cstart = cend = *p++;
1201e72d8d2Sderaadt 
1211e72d8d2Sderaadt 		if (c == '\0')
1221e72d8d2Sderaadt 		  /* [ (unterminated) loses.  */
1231e72d8d2Sderaadt 		  return FNM_NOMATCH;
1241e72d8d2Sderaadt 
1251e72d8d2Sderaadt 		c = *p++;
1261e72d8d2Sderaadt 
1271e72d8d2Sderaadt 		if ((flags & FNM_PATHNAME) && c == '/')
1281e72d8d2Sderaadt 		  /* [/] can never match.  */
1291e72d8d2Sderaadt 		  return FNM_NOMATCH;
1301e72d8d2Sderaadt 
1311e72d8d2Sderaadt 		if (c == '-' && *p != ']')
1321e72d8d2Sderaadt 		  {
1331e72d8d2Sderaadt 		    cend = *p++;
1341e72d8d2Sderaadt 		    if (!(flags & FNM_NOESCAPE) && cend == '\\')
1351e72d8d2Sderaadt 		      cend = *p++;
1361e72d8d2Sderaadt 		    if (cend == '\0')
1371e72d8d2Sderaadt 		      return FNM_NOMATCH;
1381e72d8d2Sderaadt 		    c = *p++;
1391e72d8d2Sderaadt 		  }
1401e72d8d2Sderaadt 
1411e72d8d2Sderaadt 		if (*n >= cstart && *n <= cend)
1421e72d8d2Sderaadt 		  goto matched;
1431e72d8d2Sderaadt 
1441e72d8d2Sderaadt 		if (c == ']')
1451e72d8d2Sderaadt 		  break;
1461e72d8d2Sderaadt 	      }
1471e72d8d2Sderaadt 	    if (!not)
1481e72d8d2Sderaadt 	      return FNM_NOMATCH;
1491e72d8d2Sderaadt 	    break;
1501e72d8d2Sderaadt 
1511e72d8d2Sderaadt 	  matched:;
1521e72d8d2Sderaadt 	    /* Skip the rest of the [...] that already matched.  */
1531e72d8d2Sderaadt 	    while (c != ']')
1541e72d8d2Sderaadt 	      {
1551e72d8d2Sderaadt 		if (c == '\0')
1561e72d8d2Sderaadt 		  /* [... (unterminated) loses.  */
1571e72d8d2Sderaadt 		  return FNM_NOMATCH;
1581e72d8d2Sderaadt 
1591e72d8d2Sderaadt 		c = *p++;
1601e72d8d2Sderaadt 		if (!(flags & FNM_NOESCAPE) && c == '\\')
1611e72d8d2Sderaadt 		  /* 1003.2d11 is unclear if this is right.  %%% */
1621e72d8d2Sderaadt 		  ++p;
1631e72d8d2Sderaadt 	      }
1641e72d8d2Sderaadt 	    if (not)
1651e72d8d2Sderaadt 	      return FNM_NOMATCH;
1661e72d8d2Sderaadt 	  }
1671e72d8d2Sderaadt 	  break;
1681e72d8d2Sderaadt 
1691e72d8d2Sderaadt 	default:
1701e72d8d2Sderaadt 	  if (FOLD_FN_CHAR (c) != FOLD_FN_CHAR (*n))
1711e72d8d2Sderaadt 	    return FNM_NOMATCH;
1721e72d8d2Sderaadt 	}
1731e72d8d2Sderaadt 
1741e72d8d2Sderaadt       ++n;
1751e72d8d2Sderaadt     }
1761e72d8d2Sderaadt 
1771e72d8d2Sderaadt   if (*n == '\0')
1781e72d8d2Sderaadt     return 0;
1791e72d8d2Sderaadt 
1801e72d8d2Sderaadt   return FNM_NOMATCH;
1811e72d8d2Sderaadt }
182