1 /*
2 * wordfnmatch.c: fnmatch on word boundaries
3 *
4 * Copyright (C) 2001, 2003, 2008 Colin Watson.
5 *
6 * This file is part of man-db.
7 *
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include "fnmatch.h"
32 #include "xalloc.h"
33
34 #include "manconfig.h"
35
36 #include "wordfnmatch.h"
37
38 /* TODO: How on earth do we allow multiple-word matches without
39 * reimplementing fnmatch()?
40 */
41 bool word_fnmatch (const char *pattern, const char *string)
42 {
43 char *dupstring = xstrdup (string);
44 const char *begin = dupstring;
45 char *p;
46
47 for (p = dupstring; *p; p++) {
48 if (CTYPE (isalpha, *p) || *p == '_')
49 continue;
50
51 /* Check for multiple non-word characters in a row. */
52 if (p <= begin + 1)
53 begin++;
54 else {
55 *p = '\0';
56 if (fnmatch (pattern, begin, FNM_CASEFOLD) == 0) {
57 free (dupstring);
58 return true;
59 }
60 begin = p + 1;
61 }
62 }
63
64 free (dupstring);
65 return false;
66 }