xref: /onnv-gate/usr/src/lib/libast/common/comp/fnmatch.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * fnmatch implementation
254887Schin  */
264887Schin 
274887Schin #include <ast_lib.h>
284887Schin 
294887Schin #include <ast.h>
304887Schin #include <regex.h>
314887Schin #include <fnmatch.h>
324887Schin 
334887Schin typedef struct
344887Schin {
354887Schin 	int	fnm;		/* fnmatch flag			*/
364887Schin 	int	reg;		/* regex flag			*/
374887Schin } Map_t;
384887Schin 
394887Schin static const Map_t	map[] =
404887Schin {
414887Schin 	FNM_AUGMENTED,	REG_AUGMENTED,
424887Schin 	FNM_ICASE,	REG_ICASE,
434887Schin 	FNM_NOESCAPE,	REG_SHELL_ESCAPED,
444887Schin 	FNM_PATHNAME,	REG_SHELL_PATH,
454887Schin 	FNM_PERIOD,	REG_SHELL_DOT,
464887Schin };
474887Schin 
484887Schin #if defined(__EXPORT__)
494887Schin #define extern	__EXPORT__
504887Schin #endif
514887Schin 
524887Schin extern int
fnmatch(const char * pattern,const char * subject,register int flags)534887Schin fnmatch(const char* pattern, const char* subject, register int flags)
544887Schin {
554887Schin 	register int		reflags = REG_SHELL|REG_LEFT;
564887Schin 	register const Map_t*	mp;
574887Schin 	regex_t			re;
584887Schin 	regmatch_t		match;
594887Schin 
604887Schin 	for (mp = map; mp < &map[elementsof(map)]; mp++)
614887Schin 		if (flags & mp->fnm)
624887Schin 			reflags |= mp->reg;
634887Schin 	if (flags & FNM_LEADING_DIR)
644887Schin 	{
654887Schin 		if (!(reflags = regcomp(&re, pattern, reflags)))
664887Schin 		{
674887Schin 			reflags = regexec(&re, subject, 1, &match, 0);
684887Schin 			regfree(&re);
694887Schin 			if (!reflags && (reflags = subject[match.rm_eo]))
704887Schin 				reflags = reflags == '/' ? 0 : FNM_NOMATCH;
714887Schin 		}
724887Schin 	}
734887Schin 	else if (!(reflags = regcomp(&re, pattern, reflags|REG_RIGHT)))
744887Schin 	{
754887Schin 		reflags = regexec(&re, subject, 0, NiL, 0);
764887Schin 		regfree(&re);
774887Schin 	}
784887Schin 	return reflags;
794887Schin }
80