Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "fnmatch.h"
- int char_match(const char **pattern,
- const char *string)
- {
- int bang = 0;
- int ret = bang;
- if (**pattern != '[')
- return (**pattern == *string);
- if ((*(*pattern + 1) == '!') && ++(*pattern))
- ret = bang = 1;
- while (++(*pattern) && **pattern != ']')
- if ((*(*pattern + 1) == '-' && *(*pattern + 2) != ']'
- && (*string >= *((*pattern++)++) && *string <= **pattern))
- || (**pattern == *string))
- ret = !bang;
- return (ret);
- }
- int my_fnmatch(const char *pattern,
- const char *string)
- {
- const char *spoint;
- const char *ppoint;
- while ((*string) && (*pattern != '*'))
- { /* let's work without stars beforehand */
- if ((*pattern != '?') && (!char_match(&pattern, string)))
- return (1); /* we found a mismatching char, test is false */
- pattern++; /* success, we try one byte further */
- string++;
- }
- while (*string) /* until the end of the string */
- if (*pattern == '*')
- {
- if (!*(++pattern)) /* the pattern ends in a star, the test is true */
- return (0);
- ppoint = pattern; /* we set a marker right after the star */
- spoint = string+1;
- }
- else if ((*pattern == '?') || (char_match(&pattern, string)))
- {
- pattern++; /* we found a matching char, let's try further */
- string++;
- }
- else
- {
- pattern = ppoint; /* we found a mismatching char, we search again */
- string = spoint++; /* we look for a matching sequence one byte further*/
- }
- while (*pattern == '*')
- pattern++; /* if several * at the end */
- return (*pattern && 1); /* if something remains, test is false */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement