xref: /titanic_50/usr/src/lib/libast/common/comp/fnmatch.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * fnmatch implementation
25  */
26 
27 #include <ast_lib.h>
28 
29 #include <ast.h>
30 #include <regex.h>
31 #include <fnmatch.h>
32 
33 typedef struct
34 {
35 	int	fnm;		/* fnmatch flag			*/
36 	int	reg;		/* regex flag			*/
37 } Map_t;
38 
39 static const Map_t	map[] =
40 {
41 	FNM_AUGMENTED,	REG_AUGMENTED,
42 	FNM_ICASE,	REG_ICASE,
43 	FNM_NOESCAPE,	REG_SHELL_ESCAPED,
44 	FNM_PATHNAME,	REG_SHELL_PATH,
45 	FNM_PERIOD,	REG_SHELL_DOT,
46 };
47 
48 #if defined(__EXPORT__)
49 #define extern	__EXPORT__
50 #endif
51 
52 extern int
53 fnmatch(const char* pattern, const char* subject, register int flags)
54 {
55 	register int		reflags = REG_SHELL|REG_LEFT;
56 	register const Map_t*	mp;
57 	regex_t			re;
58 	regmatch_t		match;
59 
60 	for (mp = map; mp < &map[elementsof(map)]; mp++)
61 		if (flags & mp->fnm)
62 			reflags |= mp->reg;
63 	if (flags & FNM_LEADING_DIR)
64 	{
65 		if (!(reflags = regcomp(&re, pattern, reflags)))
66 		{
67 			reflags = regexec(&re, subject, 1, &match, 0);
68 			regfree(&re);
69 			if (!reflags && (reflags = subject[match.rm_eo]))
70 				reflags = reflags == '/' ? 0 : FNM_NOMATCH;
71 		}
72 	}
73 	else if (!(reflags = regcomp(&re, pattern, reflags|REG_RIGHT)))
74 	{
75 		reflags = regexec(&re, subject, 0, NiL, 0);
76 		regfree(&re);
77 	}
78 	return reflags;
79 }
80