@@ -58,10 +58,46 @@ static inline int foldcase(int ch, int flags)
5858 return ch ;
5959}
6060
61+ /**
62+ * @brief Function to check for FNM_PERIOD condition
63+ *
64+ * @param string string whose first character is checked if it is a period
65+ * @param flags passed flags to check for FNM_PERIOD
66+ * @return int
67+ * @retval 1 if first character is period and FNM_PERIOD flag is set
68+ * @retval 0 otherwise
69+ */
70+ static int check_fnm_period (const char * string , const int flags )
71+ {
72+ return * string == '.' && (flags & FNM_PERIOD );
73+ }
74+
75+ /**
76+ * @brief Function to check for FNM_PATHNAME condition
77+ *
78+ * @param letter letter to be checked if it is a '/'
79+ * @param flags flags passed to check for FNM_PATHNAME
80+ * @return int
81+ * @retval 1 if letter is '/' and FNM_PATHNAME flag is set
82+ * @retval 0 otherwise
83+ */
84+ static int check_for_pathname (const char letter , const int flags )
85+ {
86+ return letter == '/' && (flags & FNM_PATHNAME );
87+ }
88+
6189#define FOLDCASE (ch , flags ) foldcase((unsigned char)(ch), (flags))
6290
91+ /**
92+ * @brief Function to match POSIX character classes
93+ *
94+ * @param pattern start of the pattern, should point to the character after '['
95+ * @param test next character in the string to be tested against the class
96+ * @return true
97+ * @return false
98+ */
6399static bool match_posix_class (const char * * pattern , int test ) {
64- static const struct {
100+ const struct {
65101 const char * name ;
66102 int (* func )(int );
67103 } classes [] = {
@@ -118,7 +154,7 @@ static const char *rangematch(const char *pattern, int test, int flags)
118154 c = FOLDCASE (* pattern ++ , flags )) {
119155 need = false;
120156
121- if (c == '/' && ( flags & FNM_PATHNAME )) {
157+ if (check_for_pathname ( c , flags )) {
122158 return (void * )-1 ;
123159 }
124160
@@ -195,13 +231,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
195231 return FNM_NOMATCH ;
196232 }
197233
198- if (* string == '/' && ( flags & FNM_PATHNAME )) {
234+ if (check_for_pathname ( * string , flags )) {
199235 return FNM_NOMATCH ;
200236 }
201237
202- if (* string == '.' && ( flags & FNM_PERIOD ) &&
238+ if (check_fnm_period ( string , flags ) &&
203239 (string == stringstart ||
204- (( flags & FNM_PATHNAME ) && * (string - 1 ) == '/' ))) {
240+ check_for_pathname ( * (string - 1 ), flags ))) {
205241 return FNM_NOMATCH ;
206242 }
207243
@@ -214,23 +250,22 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
214250 c = FOLDCASE (* ++ pattern , flags );
215251 }
216252
217- if (* string == '.' && ( flags & FNM_PERIOD ) &&
253+ if (check_fnm_period ( string , flags ) &&
218254 (string == stringstart ||
219- (( flags & FNM_PATHNAME ) && * (string - 1 ) == '/' ))) {
255+ check_for_pathname ( * (string - 1 ), flags ))) {
220256 return FNM_NOMATCH ;
221257 }
222258
223259 /* Optimize for pattern with * at end or before /. */
224260 if (c == EOS ) {
225- if (flags & FNM_PATHNAME ) {
226- return (flags & FNM_LEADING_DIR ) ||
227- strchr (string , '/' ) == NULL
228- ? 0
229- : FNM_NOMATCH ;
230- } else {
261+ if (!(flags & FNM_PATHNAME )) {
231262 return 0 ;
232263 }
233- } else if (c == '/' && flags & FNM_PATHNAME ) {
264+ return (flags & FNM_LEADING_DIR ) ||
265+ strchr (string , '/' ) == NULL
266+ ? 0
267+ : FNM_NOMATCH ;
268+ } else if (check_for_pathname (c , flags )) {
234269 string = strchr (string , '/' );
235270 if (string == NULL ) {
236271 return FNM_NOMATCH ;
@@ -252,7 +287,7 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
252287 return e ;
253288 }
254289
255- if (test == '/' && flags & FNM_PATHNAME ) {
290+ if (check_for_pathname ( test , flags ) ) {
256291 break ;
257292 }
258293
@@ -265,13 +300,13 @@ static int fnmatchx(const char *pattern, const char *string, int flags, size_t r
265300 return FNM_NOMATCH ;
266301 }
267302
268- if (* string == '/' && flags & FNM_PATHNAME ) {
303+ if (check_for_pathname ( * string , flags ) ) {
269304 return FNM_NOMATCH ;
270305 }
271306
272- if (* string == '.' && ( flags & FNM_PERIOD ) &&
307+ if (check_fnm_period ( string , flags ) &&
273308 (string == stringstart ||
274- (( flags & FNM_PATHNAME ) && * (string - 1 ) == '/' ))) {
309+ check_for_pathname ( * (string - 1 ), flags ))) {
275310 return FNM_NOMATCH ;
276311 }
277312
0 commit comments