Advertisement
Guest User

Untitled

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