Skip to content

Commit a89227d

Browse files
committed
modpost: use fnmatch() to simplify match()
Replace the own implementation for wildcard (glob) matching with a function call to fnmatch(). Also, change the return type to 'bool'. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 8c9ce89 commit a89227d

File tree

1 file changed

+13
-61
lines changed

1 file changed

+13
-61
lines changed

scripts/mod/modpost.c

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#define _GNU_SOURCE
1515
#include <elf.h>
16+
#include <fnmatch.h>
1617
#include <stdio.h>
1718
#include <ctype.h>
1819
#include <string.h>
@@ -710,29 +711,6 @@ static char *get_modinfo(struct elf_info *info, const char *tag)
710711
return get_next_modinfo(info, tag, NULL);
711712
}
712713

713-
/**
714-
* Test if string s ends in string sub
715-
* return 0 if match
716-
**/
717-
static int strrcmp(const char *s, const char *sub)
718-
{
719-
int slen, sublen;
720-
721-
if (!s || !sub)
722-
return 1;
723-
724-
slen = strlen(s);
725-
sublen = strlen(sub);
726-
727-
if ((slen == 0) || (sublen == 0))
728-
return 1;
729-
730-
if (sublen > slen)
731-
return 1;
732-
733-
return memcmp(s + slen - sublen, sub, sublen);
734-
}
735-
736714
static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
737715
{
738716
if (sym)
@@ -741,48 +719,22 @@ static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
741719
return "(unknown)";
742720
}
743721

744-
/* The pattern is an array of simple patterns.
745-
* "foo" will match an exact string equal to "foo"
746-
* "*foo" will match a string that ends with "foo"
747-
* "foo*" will match a string that begins with "foo"
748-
* "*foo*" will match a string that contains "foo"
722+
/*
723+
* Check whether the 'string' argument matches one of the 'patterns',
724+
* an array of shell wildcard patterns (glob).
725+
*
726+
* Return true is there is a match.
749727
*/
750-
static int match(const char *sym, const char * const pat[])
728+
static bool match(const char *string, const char *const patterns[])
751729
{
752-
const char *p;
753-
while (*pat) {
754-
const char *endp;
755-
756-
p = *pat++;
757-
endp = p + strlen(p) - 1;
730+
const char *pattern;
758731

759-
/* "*foo*" */
760-
if (*p == '*' && *endp == '*') {
761-
char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
762-
char *here = strstr(sym, bare);
763-
764-
free(bare);
765-
if (here != NULL)
766-
return 1;
767-
}
768-
/* "*foo" */
769-
else if (*p == '*') {
770-
if (strrcmp(sym, p + 1) == 0)
771-
return 1;
772-
}
773-
/* "foo*" */
774-
else if (*endp == '*') {
775-
if (strncmp(sym, p, strlen(p) - 1) == 0)
776-
return 1;
777-
}
778-
/* no wildcards */
779-
else {
780-
if (strcmp(p, sym) == 0)
781-
return 1;
782-
}
732+
while ((pattern = *patterns++)) {
733+
if (!fnmatch(pattern, string, 0))
734+
return true;
783735
}
784-
/* no match */
785-
return 0;
736+
737+
return false;
786738
}
787739

788740
/* sections that we do not want to do full section mismatch check on */

0 commit comments

Comments
 (0)