Restructure how we look for Read files slightly.
[fvwm.git] / libs / wild.c
blobdfa0f5bb2c35588a26d43f2eb2e522a9b48a91f4
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "config.h"
19 #include <stdio.h>
21 #include "wild.h"
24 * Does `string' match `pattern'? '*' in pattern matches any sub-string
25 * (including the null string) '?' matches any single char. For use
26 * by filenameforall. Note that '*' matches across directory boundaries
28 * This code donated by Paul Hudson <paulh@harlequin.co.uk>
29 * It is public domain, no strings attached. No guarantees either.
32 int matchWildcards(const char *pattern, const char *string)
34 if(string == NULL)
36 if(pattern == NULL)
37 return 1;
38 else if(strcmp(pattern,"*")==0)
39 return 1;
40 else
41 return 0;
43 if(pattern == NULL)
44 return 1;
46 while (*string && *pattern)
48 if (*pattern == '?')
50 /* match any character */
51 pattern += 1;
52 string += 1;
54 else if (*pattern == '*')
56 /* see if the rest of the pattern matches any trailing
57 * substring of the string. */
58 pattern += 1;
59 if (*pattern == 0)
61 return 1; /* trailing * must match rest */
63 while (*string)
65 if (matchWildcards(pattern,string))
67 return 1;
69 string++;
71 return 0;
73 else
75 if (*pattern == '\\')
77 /* has strange, but harmless effects if the
78 * last character is a '\\' */
79 pattern ++;
81 if (*pattern++ != *string++)
83 return 0;
87 if((*pattern == 0)&&(*string == 0))
88 return 1;
89 if((*string == 0)&&(strcmp(pattern,"*")==0))
90 return 1;
92 return 0;