25#define MATCH_INVERT '^'
29#define ESCAPE_CHAR '\\'
30#define SET_MATCH_INVERT '^'
51 int len, at_least, invert_set_match, invert_match;
54 printf(
"wild_match(%s, %s)\n",
string,
template);
60 if (*t == MATCH_INVERT) {
68 return (!invert_match);
76 printf(
"s = %s, t = %s\n", s, t);
82 return (invert_match);
90 else if (*t == MATCH_ONE) {
97 return (invert_match);
99 if (*t == 0 && *(t - 1) == MATCH_MANY) {
101 printf(
"return(1)\n");
103 return (!invert_match);
109 printf(
"return(2)\n");
111 return (!invert_match);
116 while ((ptr = strchr(ptr, *t))) {
119 printf(
"return(3)\n");
121 return (!invert_match);
126 return (invert_match);
134 if (!*(t + 1) || !(ptr = strchr(t + 1, MATCH_SET2))) {
136 return (invert_match);
142 invert_set_match = 0;
143 if (*ptr == SET_MATCH_INVERT && strlen(ptr) != 1) {
144 invert_set_match = 1;
147 if (strchr(ptr, *s)) {
148 *(t - 1) = MATCH_SET2;
149 if (invert_set_match)
150 return (invert_match);
154 *(t - 1) = MATCH_SET2;
155 if (!invert_set_match)
156 return (invert_match);
164 return (invert_match);
170 printf(
"s = %s, t = %s\n", s, t);
174 printf(
"return(5)\n");
176 return (!invert_match);
178 if (!*s && *t == MATCH_MANY && !*(t + 1)) {
180 printf(
"return(4)\n");
182 return (!invert_match);
186 printf(
"return(6)\n");
188 return (invert_match);
192 if (*t != MATCH_MANY) {
194 printf(
"return(7)\n");
196 return (invert_match);
201 printf(
"return(8)\n");
203 return !invert_match;
205 bomb(
"the impossible has happened (wild_match)", NULL);
222 int len, at_least, invert_set_match, invert_match;
227 if (*t == MATCH_INVERT) {
235 return (!invert_match);
243 printf(
"s = %s, t = %s\n", s, t);
248 if (tolower(*t) != tolower(*s))
249 return (invert_match);
257 if (*t == MATCH_MANY)
259 else if (*t == MATCH_ONE) {
266 return (invert_match);
268 if (*t == 0 && *(t - 1) == MATCH_MANY) {
270 printf(
"return(1)\n");
272 return (!invert_match);
278 printf(
"return(2)\n");
280 return (!invert_match);
285 while ((ptr = strchr_ci(ptr, *t))) {
288 printf(
"return(3)\n");
290 return (!invert_match);
295 return (invert_match);
303 if (!*(t + 1) || !(ptr = strchr_ci(t + 1, MATCH_SET2))) {
304 if (tolower(*t) != tolower(*s))
305 return (invert_match);
313 invert_set_match = 0;
314 if (*ptr == SET_MATCH_INVERT && strlen(ptr) != 1) {
315 invert_set_match = 1;
318 if (strchr_ci(ptr, *s)) {
319 *(t - 1) = MATCH_SET2;
320 if (invert_set_match)
321 return (invert_match);
325 *(t - 1) = MATCH_SET2;
326 if (!invert_set_match)
327 return (invert_match);
334 if (tolower(*s) != tolower(*t))
335 return (invert_match);
343 printf(
"s = %s, t = %s\n", s, t);
347 printf(
"return(5)\n");
349 return (!invert_match);
351 if (!*s && *t == MATCH_MANY && !*(t + 1)) {
353 printf(
"return(4)\n");
355 return (!invert_match);
359 printf(
"return(6)\n");
361 return (invert_match);
365 if (*t != MATCH_MANY) {
367 printf(
"return(7)\n");
369 return (invert_match);
374 printf(
"return(8)\n");
376 return !invert_match;
378 bomb(
"the impossible has happened (wild_match_ci)", NULL);
399 if ((sc = tolower(*s)) < (tc = tolower(*t)))
409char *strchr_ci(
char *s,
char c) {
412 if (tolower(*s) == c)
430 char *
new, *ptr, *ptr1, *ptr2, *end_new;
433 end_new =
new =
tmalloc(
sizeof(*
new) * (strlen(
template) + 1));
435 ptr =
tmalloc(
sizeof(*
new) * (strlen(
template) + 1));
436 strcpy(ptr,
template);
438 if (*ptr == ESCAPE_CHAR) {
443 }
else if (*ptr == MATCH_SET1) {
445 if ((ptr1 = strchr(ptr, MATCH_SET2))) {
449 *end_new++ = *ptr2++;
452 if (*(ptr2 - 1) == ESCAPE_CHAR) {
453 *(end_new - 1) =
'-';
458 n_in_range = (*ptr2) - (*(ptr2 - 2));
459 if (n_in_range <= 0) {
460 fprintf(stderr,
"error: bad range syntax: %s\n", ptr - 2);
463 new =
trealloc(
new,
sizeof(*
new) * (strlen(
new) + n_in_range + strlen(ptr1 + 1) + 2));
464 end_new =
new + strlen(
new);
466 *end_new++ = *ptr2 - n_in_range;
472 *end_new++ = *ptr1 = MATCH_SET2;
476 *end_new++ = *(ptr - 1);
502 while ((ptr = strchr(ptr, MATCH_MANY))) {
503 if (ptr ==
template || *(ptr - 1) != ESCAPE_CHAR)
509 while ((ptr = strchr(ptr, MATCH_ONE))) {
510 if (ptr ==
template || *(ptr - 1) != ESCAPE_CHAR)
516 while ((ptr = strchr(ptr, MATCH_SET1))) {
517 if (ptr ==
template || *(ptr - 1) != ESCAPE_CHAR)
538 while ((ptr = strchr(ptr, MATCH_MANY))) {
539 if (ptr !=
template && *(ptr - 1) == ESCAPE_CHAR) {
545 while ((ptr = strchr(ptr, MATCH_ONE))) {
546 if (ptr !=
template && *(ptr - 1) == ESCAPE_CHAR) {
552 while ((ptr = strchr(ptr, MATCH_SET1))) {
553 if (ptr !=
template && *(ptr - 1) == ESCAPE_CHAR) {
559 while ((ptr = strchr(ptr, MATCH_SET2))) {
560 if (ptr !=
template && *(ptr - 1) == ESCAPE_CHAR) {
586 for (; *s1 && *s2; s1++, s2++) {
587 if (((*s1 >=
'0') && (*s1 <=
'9')))
591 if (((*s2 >=
'0') && (*s2 <=
'9')))
595 if ((n1 == 1) && (n2 == 1)) {
597 while ((*(s1 + i)) && (*(s2 + i))) {
598 if (((*(s1 + i) >=
'0') && (*(s1 + i) <=
'9')))
602 if (((*(s2 + i) >=
'0') && (*(s2 + i) <=
'9')))
606 if ((n3 == 1) && (n4 == 0))
608 else if ((n3 == 0) && (n4 == 1))
610 else if ((n3 == 0) && (n4 == 0))
614 if (((*(s1 + i)) == 0) && (*(s2 + i))) {
615 if (((*(s2 + i) >=
'0') && (*(s2 + i) <=
'9')))
617 }
else if (((*(s2 + i)) == 0) && (*(s1 + i))) {
618 if (((*(s1 + i) >=
'0') && (*(s1 + i) <=
'9')))
628 }
else if (n1 == 0) {
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
int has_wildcards(char *template)
Check if a template string contains any wildcard characters.
int wild_match_ci(char *string, char *template)
Determine whether one string is a case-insensitive wildcard match for another.
int strcmp_ci(const char *s, const char *t)
Compare two strings case-insensitively.
char * expand_ranges(char *template)
Expand range specifiers in a wildcard template into explicit character lists.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
char * unescape_wildcards(char *template)
Remove escape characters from wildcard characters in a template string.
int strcmp_nh(const char *s1, const char *s2)
Compare two strings with a custom non-hierarchical ranking.