Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156// SPDX-License-Identifier: (GPL-2.0 OR MIT)#include <linux/module.h>#include <linux/glob.h>#include <linux/export.h>
/* * The only reason this code can be compiled as a module is because the * ATA code that depends on it can be as well. In practice, they're * both usually compiled in and the module overhead goes away. */MODULE_DESCRIPTION("glob(7) matching");MODULE_LICENSE("Dual MIT/GPL");
static bool __pure glob_match_str(char const *pat, char const *str, char const *str_end);
/** * glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0) * @pat: Shell-style pattern to match, e.g. "*.[ch]". * @str: String to match. The pattern must match the entire string. * * Perform shell-style glob matching, returning true (1) if the match * succeeds, or false (0) if it fails. Equivalent to !fnmatch(@pat, @str, 0). * * Pattern metacharacters are ?, *, [ and \. * (And, inside character classes, !, - and ].) * * This is a small and simple implementation intended for device denylists * where a string is matched against a number of patterns. Thus, it * does not preprocess the patterns. It is non-recursive, and run-time * is at most quadratic: strlen(@str)*strlen(@pat). * * An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa"); * it takes 6 passes over the pattern before matching the string. * * Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT * treat / or leading . specially; it isn't actually used for pathnames. * * Note that according to glob(7) (and unlike bash), character classes * are complemented by a leading !; this does not support the regex-style * [^a-z] syntax. * * An opening bracket without a matching close is matched literally. */bool __pure glob_match(char const *pat, char const *str){ return glob_match_str(pat, str, NULL);}EXPORT_SYMBOL(glob_match);
/** * glob_match_len - glob match against a length-bounded string * @pat: Shell-style pattern to match. * @str: String to match. Need not be NUL-terminated. * @len: Number of bytes of @str that may be read. * * Like glob_match(), but @str is only read up to @len bytes, so it can be * used on buffers that are not NUL-terminated (e.g. trace event fields). * A NUL byte within @len still terminates the string. */bool __pure glob_match_len(char const *pat, char const *str, size_t len){ return glob_match_str(pat, str, str + len);}EXPORT_SYMBOL(glob_match_len);
static bool __pure glob_match_str(char const *pat, char const *str, char const *str_end){ /* * Backtrack to previous * on mismatch and retry starting one * character later in the string. Because * matches all characters * (no exception for /), it can be easily proved that there's * never a need to backtrack multiple levels. */ char const *back_pat = NULL, *back_str = NULL;
/* * Loop over each token (character or class) in pat, matching * it against the remaining unmatched tail of str. Return false * on mismatch, or true after matching the trailing nul bytes. */ for (;;) { unsigned char c = (str_end && str >= str_end) ? '\0' : *str; unsigned char d = *pat++;
str++;
switch (d) { case '?': /* Wildcard: anything but nul */ if (c == '\0') return false; break; case '*': /* Any-length wildcard */ if (*pat == '\0') /* Optimize trailing * case */ return true; back_pat = pat; back_str = --str; /* Allow zero-length match */ break; case '[': { /* Character class */ if (c == '\0') /* No possible match */ return false; bool match = false, inverted = (*pat == '!'); char const *class = inverted ? pat + 1 : pat; unsigned char a = *class++;
/* * Iterate over each span in the character class. * A span is either a single character a, or a * range a-b. The first span may begin with ']'. */ do { unsigned char b = a;
if (a == '\0') /* Malformed */ goto literal;
if (class[0] == '-' && class[1] != ']') { b = class[1];
if (b == '\0') goto literal;
class += 2; /* Any special action if a > b? */ } if (a <= c && c <= b) match = true; } while ((a = *class++) != ']');
if (match == inverted) goto backtrack; pat = class; } break; case '\\': d = *pat++; fallthrough; default: /* Literal character */literal: if (c == d) { if (d == '\0') return true; break; }backtrack: if (c == '\0' || !back_pat) return false; /* No point continuing */ /* Try again from last *, one character later in str. */ pat = back_pat; str = ++back_str; break; } }}