summaryrefslogtreecommitdiff
path: root/app/fvwm/libs/wild.c
blob: 71805cd1e61419165036ce489352e90c42cf9657 (plain)
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
#include <stdio.h>

#ifndef TRUE                    
#define TRUE	1
#define FALSE	0
#endif

/*****************************************************************************
 *	Does `string' match `pattern'? '*' in pattern matches any sub-string
 *	(including the null string) '?' matches any single char. For use
 *	by filenameforall. Note that '*' matches across directory boundaries
 *
 *      This code donated by  Paul Hudson <paulh@harlequin.co.uk>    
 *      It is public domain, no strings attached. No guarantees either.
 *
 *****************************************************************************/
int matchWildcards(char *pattern, char *string)
{
  if(string == NULL)
    {
      if(pattern == NULL)
	return TRUE;
      else if(strcmp(pattern,"*")==0)
	return TRUE;
      else
	return FALSE;
    }
  if(pattern == NULL)
    return TRUE;

  while (*string && *pattern)
    {
      if (*pattern == '?')
	{
	  /* match any character */
	  pattern += 1;
	  string += 1;
	}
      else if (*pattern == '*')
	{
	  /* see if the rest of the pattern matches any trailing substring
	     of the string. */
	  pattern += 1;
	  if (*pattern == 0)
	    {
	      return TRUE; /* trailing * must match rest */
	    }
	  while (*string)
	    {
	      if (matchWildcards(pattern,string))
		{
		  return TRUE;
		}
	      string++;
	    }
	  return FALSE;
	}
      else
	{
	  if (*pattern == '\\')
	    pattern ++;	   /* has strange, but harmless effects if the last
			      character is a '\\' */
	  if  (*pattern++ != *string++) 
	    {
	      return FALSE;
	    }
	}
    }
  if((*pattern == 0)&&(*string == 0))
    return TRUE;
  if((*string == 0)&&(strcmp(pattern,"*")==0))
    return TRUE;
  return FALSE;
}