File: fnmatch.c

package info (click to toggle)
swish++ 6.1.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,256 kB
  • ctags: 1,759
  • sloc: ansic: 11,931; lisp: 804; sh: 629; perl: 366; makefile: 80
file content (152 lines) | stat: -rw-r--r-- 3,075 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */

/* Added this C-style comment since the C++-style comment alone isn't good
 * enough to comment out a C preprocessor directive. -- pjl
//#include <libc/stubs.h>
*/

#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "fnmatch.h"		/* changed to use "" rather than <> -- pjl */

using namespace std;

#define	EOS	'\0'

static const char *rangematch(const char *pattern, char test, int nocase);

#define isslash(c) ((c) == '\\' || (c) == '/')

static const char *
find_slash(const char *s)
{
  while (*s)
  {
    if (isslash(*s))
      return s;
    s++;
  }
  return 0;
}

static const char *
rangematch(const char *pattern, char test, int nocase)
{
  char c, c2;
  int negate, ok;

  if ((negate = (*pattern == '!')))
    ++pattern;

  for (ok = 0; (c = *pattern++) != ']';)
  {
    if (c == 0)
      return 0;			/* illegal pattern */
    if (*pattern == '-' && (c2 = pattern[1]) != 0 && c2 != ']')
    {
      if (c <= test && test <= c2)
	ok = 1;
      if (nocase && toupper(c) <= toupper(test) && toupper(test) <= toupper(c2))
	ok = 1;
      pattern += 2;
    }
    else if (c == test)
      ok = 1;
    else if (nocase && (toupper(c) == toupper(test)))
      ok = 1;
  }
  return ok == negate ? NULL : pattern;
}

int
fnmatch(const char *pattern, const char *string, int flags)
{
  char c;
  char test;

  for (;;)
    switch ((c = *pattern++))
    {
    case 0:
      return *string == 0 ? 0 : FNM_NOMATCH;

    case '?':
      if ((test = *string++) == 0 ||
	  (isslash(test) && (flags & FNM_PATHNAME)))
	return(FNM_NOMATCH);
      break;

    case '*':
      c = *pattern;
      /* collapse multiple stars */
      while (c == '*')
	c = *++pattern;

      /* optimize for pattern with * at end or before / */
      if (c == 0)
      {
	if (flags & FNM_PATHNAME)
	  return find_slash(string) ? FNM_NOMATCH : 0;
	else
	  return 0;
      }
      else if (isslash(c) && flags & FNM_PATHNAME)
      {
	if ((string = find_slash(string)) == NULL)
	  return FNM_NOMATCH;
	break;
      }

      /* general case, use recursion */
      while ((test = *string) != 0)
      {
	if (fnmatch(pattern, string, flags) == 0)
	  return(0);
	if (isslash(test) && flags & FNM_PATHNAME)
	  break;
	++string;
      }
      return FNM_NOMATCH;

    case '[':
      if ((test = *string++) == 0 ||
	  (isslash(test) && flags & FNM_PATHNAME))
	return FNM_NOMATCH;
      if ((pattern = rangematch(pattern, test, flags & FNM_NOCASE)) == NULL)
	return FNM_NOMATCH;
      break;

    case '\\':
      if (!(flags & FNM_NOESCAPE) && pattern[1] && strchr("*?[\\", pattern[1]))
      {
	if ((c = *pattern++) == 0)
	{
	  c = '\\';
	  --pattern;
	}
	if (c != *string++)
	  return FNM_NOMATCH;
	break;
      }
      /* FALLTHROUGH */

    default:
      if (isslash(c) && isslash(*string))
      {
	string++;
	break;
      }
      if (flags & FNM_NOCASE)
      {
	if (toupper(c) != toupper(*string++))
	  return FNM_NOMATCH;
      }
      else
      {
	if (c != *string++)
	  return FNM_NOMATCH;
      }
      break;
    }
}