Whamcloud - gitweb
LU-9680 net: Netlink improvements
[fs/lustre-release.git] / libcfs / libcfs / linux / glob.c
1 #ifndef HAVE_GLOB
2 #include <linux/module.h>
3 #include "libcfs/linux/glob.h"
4
5 /**
6  * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)
7  * @pat: Shell-style pattern to match, e.g. "*.[ch]".
8  * @str: String to match.  The pattern must match the entire string.
9  *
10  * Perform shell-style glob matching, returning true (1) if the match
11  * succeeds, or false (0) if it fails.  Equivalent to !fnmatch(@pat, @str, 0).
12  *
13  * Pattern metacharacters are ?, *, [ and \.
14  * (And, inside character classes, !, - and ].)
15  *
16  * This is small and simple implementation intended for device blacklists
17  * where a string is matched against a number of patterns.  Thus, it
18  * does not preprocess the patterns.  It is non-recursive, and run-time
19  * is at most quadratic: strlen(@str)*strlen(@pat).
20  *
21  * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa");
22  * it takes 6 passes over the pattern before matching the string.
23  *
24  * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT
25  * treat / or leading . specially; it isn't actually used for pathnames.
26  *
27  * Note that according to glob(7) (and unlike bash), character classes
28  * are complemented by a leading !; this does not support the regex-style
29  * [^a-z] syntax.
30  *
31  * An opening bracket without a matching close is matched literally.
32  */
33 bool __pure glob_match(char const *pat, char const *str)
34 {
35         /*
36          * Backtrack to previous * on mismatch and retry starting one
37          * character later in the string.  Because * matches all characters
38          * (no exception for /), it can be easily proved that there's
39          * never a need to backtrack multiple levels.
40          */
41         char const *back_pat = NULL, *back_str = back_str;
42
43         /*
44          * Loop over each token (character or class) in pat, matching
45          * it against the remaining unmatched tail of str.  Return false
46          * on mismatch, or true after matching the trailing nul bytes.
47          */
48         for (;;) {
49                 unsigned char c = *str++;
50                 unsigned char d = *pat++;
51
52                 switch (d) {
53                 case '?':       /* Wildcard: anything but nul */
54                         if (c == '\0')
55                                 return false;
56                         break;
57                 case '*':       /* Any-length wildcard */
58                         if (*pat == '\0')       /* Optimize trailing * case */
59                                 return true;
60                         back_pat = pat;
61                         back_str = --str;       /* Allow zero-length match */
62                         break;
63                 case '[': {     /* Character class */
64                         bool match = false, inverted = (*pat == '!');
65                         char const *class = pat + inverted;
66                         unsigned char a = *class++;
67
68                         /*
69                          * Iterate over each span in the character class.
70                          * A span is either a single character a, or a
71                          * range a-b.  The first span may begin with ']'.
72                          */
73                         do {
74                                 unsigned char b = a;
75
76                                 if (a == '\0')  /* Malformed */
77                                         goto literal;
78
79                                 if (class[0] == '-' && class[1] != ']') {
80                                         b = class[1];
81
82                                         if (b == '\0')
83                                                 goto literal;
84
85                                         class += 2;
86                                         /* Any special action if a > b? */
87                                 }
88                                 match |= (a <= c && c <= b);
89                         } while ((a = *class++) != ']');
90
91                         if (match == inverted)
92                                 goto backtrack;
93                         pat = class;
94                         }
95                         break;
96                 case '\\':
97                         d = *pat++;
98                         /*FALLTHROUGH*/
99                 default:        /* Literal character */
100 literal:
101                         if (c == d) {
102                                 if (d == '\0')
103                                         return true;
104                                 break;
105                         }
106 backtrack:
107                         if (c == '\0' || !back_pat)
108                                 return false;   /* No point continuing */
109                         /* Try again from last *, one character later in str. */
110                         pat = back_pat;
111                         str = ++back_str;
112                         break;
113                 }
114         }
115 }
116 EXPORT_SYMBOL(glob_match);
117 #endif /* ! HAVE_GLOB */