-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwildcard.c
136 lines (118 loc) · 3.36 KB
/
wildcard.c
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
/*
* wildcard.c - wildcard test matching routines
*/
#include <string.h>
#include "global.h"
#include "wildcard.h"
/*
* Recursive wildcard matching function implemented from scratch - let me
* know if you can find an example that breaks it ... - GeK 4/96
*/
Bool
wildcard_match (const char *wildcard, const char *match_string)
{
if (wildcard == NULL || match_string == NULL)
return (FALSE);
if (*match_string == '\0')
/* Nothing matches an empty string */
return (FALSE);
for (; *wildcard != '\0'; wildcard++, match_string++)
{
if (*wildcard == '?')
{
/* ? matches a single character */
if (*match_string == '\0')
return (FALSE);
}
else if (*wildcard == '*')
{
/* '*' matches zero or more chars */
if (*(++wildcard) == '\0')
/* Trailing '*' matches anything */
return (TRUE);
/* Recursively try rest of wildcard successively along string */
while (wildcard_match (wildcard, match_string) == FALSE)
{
if (*match_string == '\0')
/* reached end of string without a match so must fail */
return (FALSE);
else
/* Try further along string .. */
match_string++;
}
/* If we reach here, must have matched wildcard */
return (TRUE);
}
else
{
/* Not a recognised wildcard char, must match string */
if (*match_string != *wildcard)
/*
* Note end of matchstring will fall out here
* since *wildcard can't be \0 here
*/
return (FALSE);
}
}
/* Fails if any unmatched string remaining here .. */
return (*match_string == '\0' ? TRUE : FALSE);
}
/*
* Makes a destination filename from a given source filename and a wildcard
* returns NULL on error else a malloc'd string
*/
char
*make_dest_filename (const char *source_filename, const char *dest_wildmask)
{
char buf[256];
char *bufptr = buf;
buf[0] = '\0';
/* No source filename fails .. */
if (source_filename == NULL || *source_filename == '\0')
return (NULL);
/* No destination wildmask returns the source filename .. */
if (dest_wildmask == NULL || *dest_wildmask == '\0')
return (strdup (source_filename));
for (; *dest_wildmask != '\0'; dest_wildmask++)
{
#ifdef notdef
printf ("*dst = '%c' *src = '%c'\n", *dest_wildmask != '\0' ? *dest_wildmask : '0', *source_filename != '\0' ? *source_filename : '0' );
#endif
if (*dest_wildmask == '?')
{
if (*source_filename != '\0' && *source_filename != '.')
*(bufptr++) = *(source_filename++);
}
else if (*dest_wildmask == '*')
{
while (*source_filename != '\0' && *source_filename != '.')
*(bufptr++) = *(source_filename++);
}
else
{
*(bufptr++) = *(dest_wildmask);
if (*dest_wildmask == '.')
{
/* Skip to next dot */
while (*source_filename != '\0'
&& *source_filename != '.')
source_filename++;
if (*source_filename == '.')
source_filename++;
}
else
{
/* Don't skip past a dot */
if (*source_filename != '\0'
&& *source_filename != '.')
source_filename++;
}
}
#ifdef notdef
*bufptr = '\0';
printf ("after: *dst = '%c' *src = '%c' buf = '%s'\n", *dest_wildmask != '\0' ? *dest_wildmask : '0', *source_filename != '\0' ? *source_filename : '0', buf);
#endif
}
*bufptr = '\0';
return (strdup (buf));
}