Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
5f9be82
Documentation
arch
block
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
fonts
lz4
lzo
mpi
raid6
reed_solomon
xz
zlib_deflate
zlib_inflate
.gitignore
Kconfig
Kconfig.debug
Kconfig.kgdb
Kconfig.kmemcheck
Makefile
argv_split.c
asn1_decoder.c
assoc_array.c
atomic64.c
atomic64_test.c
audit.c
average.c
bcd.c
bch.c
bitmap.c
bitrev.c
bsearch.c
btree.c
bug.c
build_OID_registry
bust_spinlocks.c
check_signature.c
checksum.c
clz_ctz.c
clz_tab.c
cmdline.c
compat_audit.c
cordic.c
cpu-notifier-error-inject.c
cpu_rmap.c
cpumask.c
crc-ccitt.c
crc-itu-t.c
crc-t10dif.c
crc16.c
crc32.c
crc32defs.h
crc7.c
crc8.c
ctype.c
debug_locks.c
debugobjects.c
dec_and_lock.c
decompress.c
decompress_bunzip2.c
decompress_inflate.c
decompress_unlz4.c
decompress_unlzma.c
decompress_unlzo.c
decompress_unxz.c
devres.c
digsig.c
div64.c
dma-debug.c
dump_stack.c
dynamic_debug.c
dynamic_queue_limits.c
earlycpio.c
extable.c
fault-inject.c
fdt.c
fdt_empty_tree.c
fdt_ro.c
fdt_rw.c
fdt_strerror.c
fdt_sw.c
fdt_wip.c
find_last_bit.c
find_next_bit.c
flex_array.c
flex_proportions.c
gcd.c
gen_crc32table.c
genalloc.c
glob.c
halfmd4.c
hash.c
hexdump.c
hweight.c
idr.c
inflate.c
int_sqrt.c
interval_tree.c
interval_tree_test.c
iomap.c
iomap_copy.c
iommu-helper.c
ioremap.c
iovec.c
irq_regs.c
is_single_threaded.c
jedec_ddr_data.c
kasprintf.c
kfifo.c
klist.c
kobject.c
kobject_uevent.c
kstrtox.c
kstrtox.h
lcm.c
libcrc32c.c
list_debug.c
list_sort.c
llist.c
locking-selftest-hardirq.h
locking-selftest-mutex.h
locking-selftest-rlock-hardirq.h
locking-selftest-rlock-softirq.h
locking-selftest-rlock.h
locking-selftest-rsem.h
locking-selftest-softirq.h
locking-selftest-spin-hardirq.h
locking-selftest-spin-softirq.h
locking-selftest-spin.h
locking-selftest-wlock-hardirq.h
locking-selftest-wlock-softirq.h
locking-selftest-wlock.h
locking-selftest-wsem.h
locking-selftest.c
lockref.c
lru_cache.c
md5.c
memory-notifier-error-inject.c
memweight.c
net_utils.c
nlattr.c
notifier-error-inject.c
notifier-error-inject.h
of-reconfig-notifier-error-inject.c
oid_registry.c
parser.c
pci_iomap.c
percpu-refcount.c
percpu_counter.c
percpu_ida.c
percpu_test.c
plist.c
pm-notifier-error-inject.c
prio_heap.c
proportions.c
radix-tree.c
random32.c
ratelimit.c
rational.c
rbtree.c
rbtree_test.c
reciprocal_div.c
rhashtable.c
scatterlist.c
sha1.c
show_mem.c
smp_processor_id.c
sort.c
stmp_device.c
string.c
string_helpers.c
strncpy_from_user.c
strnlen_user.c
swiotlb.c
syscall.c
test-kstrtox.c
test-string_helpers.c
test_bpf.c
test_firmware.c
test_module.c
test_user_copy.c
textsearch.c
timerqueue.c
ts_bm.c
ts_fsm.c
ts_kmp.c
ucs2_string.c
usercopy.c
uuid.c
vsprintf.c
mm
net
samples
scripts
security
sound
tools
usr
virt
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
lib
/
glob.c
Copy path
Blame
Blame
Latest commit
History
History
287 lines (259 loc) · 7.66 KB
Breadcrumbs
linux
/
lib
/
glob.c
Top
File metadata and controls
Code
Blame
287 lines (259 loc) · 7.66 KB
Raw
#include <linux/module.h> #include <linux/glob.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"); /** * 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 small and simple implementation intended for device blacklists * 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) { /* * 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 = back_str; /* * 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++; unsigned char d = *pat++; 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 */ bool match = false, inverted = (*pat == '!'); char const *class = pat + inverted; 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? */ } match |= (a <= c && c <= b); } 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; } } } EXPORT_SYMBOL(glob_match); #ifdef CONFIG_GLOB_SELFTEST #include <linux/printk.h> #include <linux/moduleparam.h> /* Boot with "glob.verbose=1" to show successful tests, too */ static bool verbose = false; module_param(verbose, bool, 0); struct glob_test { char const *pat, *str; bool expected; }; static bool __pure __init test(char const *pat, char const *str, bool expected) { bool match = glob_match(pat, str); bool success = match == expected; /* Can't get string literals into a particular section, so... */ static char const msg_error[] __initconst = KERN_ERR "glob: \"%s\" vs. \"%s\": %s *** ERROR ***\n"; static char const msg_ok[] __initconst = KERN_DEBUG "glob: \"%s\" vs. \"%s\": %s OK\n"; static char const mismatch[] __initconst = "mismatch"; char const *message; if (!success) message = msg_error; else if (verbose) message = msg_ok; else return success; printk(message, pat, str, mismatch + 3*match); return success; } /* * The tests are all jammed together in one array to make it simpler * to place that array in the .init.rodata section. The obvious * "array of structures containing char *" has no way to force the * pointed-to strings to be in a particular section. * * Anyway, a test consists of: * 1. Expected glob_match result: '1' or '0'. * 2. Pattern to match: null-terminated string * 3. String to match against: null-terminated string * * The list of tests is terminated with a final '\0' instead of * a glob_match result character. */ static char const glob_tests[] __initconst = /* Some basic tests */ "1" "a\0" "a\0" "0" "a\0" "b\0" "0" "a\0" "aa\0" "0" "a\0" "\0" "1" "\0" "\0" "0" "\0" "a\0" /* Simple character class tests */ "1" "[a]\0" "a\0" "0" "[a]\0" "b\0" "0" "[!a]\0" "a\0" "1" "[!a]\0" "b\0" "1" "[ab]\0" "a\0" "1" "[ab]\0" "b\0" "0" "[ab]\0" "c\0" "1" "[!ab]\0" "c\0" "1" "[a-c]\0" "b\0" "0" "[a-c]\0" "d\0" /* Corner cases in character class parsing */ "1" "[a-c-e-g]\0" "-\0" "0" "[a-c-e-g]\0" "d\0" "1" "[a-c-e-g]\0" "f\0" "1" "[]a-ceg-ik[]\0" "a\0" "1" "[]a-ceg-ik[]\0" "]\0" "1" "[]a-ceg-ik[]\0" "[\0" "1" "[]a-ceg-ik[]\0" "h\0" "0" "[]a-ceg-ik[]\0" "f\0" "0" "[!]a-ceg-ik[]\0" "h\0" "0" "[!]a-ceg-ik[]\0" "]\0" "1" "[!]a-ceg-ik[]\0" "f\0" /* Simple wild cards */ "1" "?\0" "a\0" "0" "?\0" "aa\0" "0" "??\0" "a\0" "1" "?x?\0" "axb\0" "0" "?x?\0" "abx\0" "0" "?x?\0" "xab\0" /* Asterisk wild cards (backtracking) */ "0" "*??\0" "a\0" "1" "*??\0" "ab\0" "1" "*??\0" "abc\0" "1" "*??\0" "abcd\0" "0" "??*\0" "a\0" "1" "??*\0" "ab\0" "1" "??*\0" "abc\0" "1" "??*\0" "abcd\0" "0" "?*?\0" "a\0" "1" "?*?\0" "ab\0" "1" "?*?\0" "abc\0" "1" "?*?\0" "abcd\0" "1" "*b\0" "b\0" "1" "*b\0" "ab\0" "0" "*b\0" "ba\0" "1" "*b\0" "bb\0" "1" "*b\0" "abb\0" "1" "*b\0" "bab\0" "1" "*bc\0" "abbc\0" "1" "*bc\0" "bc\0" "1" "*bc\0" "bbc\0" "1" "*bc\0" "bcbc\0" /* Multiple asterisks (complex backtracking) */ "1" "*ac*\0" "abacadaeafag\0" "1" "*ac*ae*ag*\0" "abacadaeafag\0" "1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0" "0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0" "1" "*abcd*\0" "abcabcabcabcdefg\0" "1" "*ab*cd*\0" "abcabcabcabcdefg\0" "1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0" "0" "*abcd*\0" "abcabcabcabcefg\0" "0" "*ab*cd*\0" "abcabcabcabcefg\0"; static int __init glob_init(void) { unsigned successes = 0; unsigned n = 0; char const *p = glob_tests; static char const message[] __initconst = KERN_INFO "glob: %u self-tests passed, %u failed\n"; /* * Tests are jammed together in a string. The first byte is '1' * or '0' to indicate the expected outcome, or '\0' to indicate the * end of the tests. Then come two null-terminated strings: the * pattern and the string to match it against. */ while (*p) { bool expected = *p++ & 1; char const *pat = p; p += strlen(p) + 1; successes += test(pat, p, expected); p += strlen(p) + 1; n++; } n -= successes; printk(message, successes, n); /* What's the errno for "kernel bug detected"? Guess... */ return n ? -ECANCELED : 0; } /* We need a dummy exit function to allow unload */ static void __exit glob_fini(void) { } module_init(glob_init); module_exit(glob_fini); #endif /* CONFIG_GLOB_SELFTEST */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
You can’t perform that action at this time.