Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include "fnmatch.h"
  2.  
  3. int char_match(const char       **pattern,
  4.            const char       *string)
  5. {
  6.   int   bang = 0;
  7.   int   ret = bang;
  8.  
  9.   if (**pattern != '[')
  10.     return (**pattern == *string);
  11.   if ((*(*pattern + 1) == '!') && ++(*pattern))
  12.     ret = bang = 1;
  13.   while (++(*pattern) && **pattern != ']')
  14.     if ((*(*pattern + 1) == '-' && *(*pattern + 2) != ']'
  15.      && (*string >= *((*pattern++)++) && *string <= **pattern))
  16.         || (**pattern == *string))
  17.       ret = !bang;
  18.   return (ret);
  19. }
  20.  
  21. int     my_fnmatch(const char           *pattern,
  22.                    const char           *string)
  23. {
  24.   const char  *spoint;
  25.   const char  *ppoint;
  26.  
  27.   while ((*string) && (*pattern != '*'))
  28.   {                       /* let's work without stars beforehand */
  29.     if ((*pattern != '?') && (!char_match(&pattern, string)))
  30.       return (1);         /* we found a mismatching char, test is false */
  31.     pattern++;            /* success, we try one byte further */
  32.     string++;
  33.   }
  34.   while (*string)         /* until the end of the string */
  35.     if (*pattern == '*')
  36.     {
  37.       if (!*(++pattern))  /* the pattern ends in a star, the test is true */
  38.         return (0);
  39.       ppoint = pattern;   /* we set a marker right after the star */
  40.       spoint = string+1;
  41.     }
  42.     else  if ((*pattern == '?') || (char_match(&pattern, string)))
  43.     {
  44.       pattern++;      /* we found a matching char, let's try further */
  45.       string++;
  46.     }
  47.     else
  48.     {
  49.       pattern = ppoint;   /* we found a mismatching char, we search again */
  50.       string = spoint++;  /* we look for a matching sequence one byte further*/
  51.     }
  52.   while (*pattern == '*')
  53.     pattern++;            /* if several * at the end  */
  54.   return (*pattern && 1); /* if something remains, test is false */
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement