14297a3b0SGarrett D'Amore /*
24297a3b0SGarrett D'Amore * Copyright (c) 1989, 1993, 1994
34297a3b0SGarrett D'Amore * The Regents of the University of California. All rights reserved.
44297a3b0SGarrett D'Amore *
54297a3b0SGarrett D'Amore * This code is derived from software contributed to Berkeley by
64297a3b0SGarrett D'Amore * Guido van Rossum.
74297a3b0SGarrett D'Amore *
84297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without
94297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions
104297a3b0SGarrett D'Amore * are met:
114297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
124297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer.
134297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
144297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
154297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution.
16*f52b16c6SYuri Pankov * 3. Neither the name of the University nor the names of its contributors
174297a3b0SGarrett D'Amore * may be used to endorse or promote products derived from this software
184297a3b0SGarrett D'Amore * without specific prior written permission.
194297a3b0SGarrett D'Amore *
204297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304297a3b0SGarrett D'Amore * SUCH DAMAGE.
314297a3b0SGarrett D'Amore */
324297a3b0SGarrett D'Amore
334297a3b0SGarrett D'Amore /*
342d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3579d022daSYuri Pankov * Copyright 2017 Nexenta Systems, Inc.
364297a3b0SGarrett D'Amore */
374297a3b0SGarrett D'Amore
384297a3b0SGarrett D'Amore /*
394297a3b0SGarrett D'Amore * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
404297a3b0SGarrett D'Amore * Compares a filename or pathname to a pattern.
414297a3b0SGarrett D'Amore */
424297a3b0SGarrett D'Amore
434297a3b0SGarrett D'Amore /*
444297a3b0SGarrett D'Amore * Some notes on multibyte character support:
454297a3b0SGarrett D'Amore * 1. Patterns with illegal byte sequences match nothing.
464297a3b0SGarrett D'Amore * 2. Illegal byte sequences in the "string" argument are handled by treating
474297a3b0SGarrett D'Amore * them as single-byte characters with a value of the first byte of the
484297a3b0SGarrett D'Amore * sequence cast to wchar_t.
494297a3b0SGarrett D'Amore * 3. Multibyte conversion state objects (mbstate_t) are passed around and
504297a3b0SGarrett D'Amore * used for most, but not all, conversions. Further work will be required
514297a3b0SGarrett D'Amore * to support state-dependent encodings.
524297a3b0SGarrett D'Amore */
534297a3b0SGarrett D'Amore
544297a3b0SGarrett D'Amore #include "lint.h"
554297a3b0SGarrett D'Amore #include <fnmatch.h>
564297a3b0SGarrett D'Amore #include <limits.h>
574297a3b0SGarrett D'Amore #include <string.h>
584297a3b0SGarrett D'Amore #include <wchar.h>
592d08521bSGarrett D'Amore #include <xlocale.h>
604297a3b0SGarrett D'Amore #include <wctype.h>
612d08521bSGarrett D'Amore #include "localeimpl.h"
624297a3b0SGarrett D'Amore #include "collate.h"
634297a3b0SGarrett D'Amore
644297a3b0SGarrett D'Amore #define EOS '\0'
654297a3b0SGarrett D'Amore
664297a3b0SGarrett D'Amore #define RANGE_MATCH 1
674297a3b0SGarrett D'Amore #define RANGE_NOMATCH 0
684297a3b0SGarrett D'Amore #define RANGE_ERROR (-1)
694297a3b0SGarrett D'Amore
702d08521bSGarrett D'Amore static int rangematch(const char *, wchar_t, int, char **, mbstate_t *,
712d08521bSGarrett D'Amore locale_t);
724297a3b0SGarrett D'Amore static int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
732d08521bSGarrett D'Amore mbstate_t, locale_t);
744297a3b0SGarrett D'Amore
754297a3b0SGarrett D'Amore int
fnmatch(const char * pattern,const char * string,int flags)7679d022daSYuri Pankov fnmatch(const char *pattern, const char *string, int flags)
774297a3b0SGarrett D'Amore {
782d08521bSGarrett D'Amore locale_t loc = uselocale(NULL);
794297a3b0SGarrett D'Amore static const mbstate_t initial = { 0 };
804297a3b0SGarrett D'Amore
812d08521bSGarrett D'Amore return (fnmatch1(pattern, string, string, flags, initial, initial,
822d08521bSGarrett D'Amore loc));
834297a3b0SGarrett D'Amore }
844297a3b0SGarrett D'Amore
854297a3b0SGarrett D'Amore static int
fnmatch1(const char * pattern,const char * string,const char * stringstart,int flags,mbstate_t patmbs,mbstate_t strmbs,locale_t loc)864297a3b0SGarrett D'Amore fnmatch1(const char *pattern, const char *string, const char *stringstart,
872d08521bSGarrett D'Amore int flags, mbstate_t patmbs, mbstate_t strmbs, locale_t loc)
884297a3b0SGarrett D'Amore {
8979d022daSYuri Pankov const char *bt_pattern, *bt_string;
9079d022daSYuri Pankov mbstate_t bt_patmbs, bt_strmbs;
914297a3b0SGarrett D'Amore char *newp;
924297a3b0SGarrett D'Amore char c;
934297a3b0SGarrett D'Amore wchar_t pc, sc;
944297a3b0SGarrett D'Amore size_t pclen, sclen;
954297a3b0SGarrett D'Amore
9679d022daSYuri Pankov bt_pattern = bt_string = NULL;
974297a3b0SGarrett D'Amore for (;;) {
982d08521bSGarrett D'Amore pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, &patmbs, loc);
994297a3b0SGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
1004297a3b0SGarrett D'Amore return (FNM_NOMATCH);
1014297a3b0SGarrett D'Amore pattern += pclen;
1022d08521bSGarrett D'Amore sclen = mbrtowc_l(&sc, string, MB_LEN_MAX, &strmbs, loc);
1034297a3b0SGarrett D'Amore if (sclen == (size_t)-1 || sclen == (size_t)-2) {
1044297a3b0SGarrett D'Amore sc = (unsigned char)*string;
1054297a3b0SGarrett D'Amore sclen = 1;
1064297a3b0SGarrett D'Amore (void) memset(&strmbs, 0, sizeof (strmbs));
1074297a3b0SGarrett D'Amore }
1084297a3b0SGarrett D'Amore switch (pc) {
1094297a3b0SGarrett D'Amore case EOS:
110*f52b16c6SYuri Pankov if ((flags & FNM_LEADING_DIR) && sc == '/')
111*f52b16c6SYuri Pankov return (0);
11279d022daSYuri Pankov if (sc == EOS)
11379d022daSYuri Pankov return (0);
11479d022daSYuri Pankov goto backtrack;
1154297a3b0SGarrett D'Amore case '?':
1164297a3b0SGarrett D'Amore if (sc == EOS)
1174297a3b0SGarrett D'Amore return (FNM_NOMATCH);
1184297a3b0SGarrett D'Amore if (sc == '/' && (flags & FNM_PATHNAME))
11979d022daSYuri Pankov goto backtrack;
1204297a3b0SGarrett D'Amore if (sc == '.' && (flags & FNM_PERIOD) &&
1214297a3b0SGarrett D'Amore (string == stringstart ||
1224297a3b0SGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
12379d022daSYuri Pankov goto backtrack;
1244297a3b0SGarrett D'Amore string += sclen;
1254297a3b0SGarrett D'Amore break;
1264297a3b0SGarrett D'Amore case '*':
1274297a3b0SGarrett D'Amore c = *pattern;
1284297a3b0SGarrett D'Amore /* Collapse multiple stars. */
1294297a3b0SGarrett D'Amore while (c == '*')
1304297a3b0SGarrett D'Amore c = *++pattern;
1314297a3b0SGarrett D'Amore
1324297a3b0SGarrett D'Amore if (sc == '.' && (flags & FNM_PERIOD) &&
1334297a3b0SGarrett D'Amore (string == stringstart ||
1344297a3b0SGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
13579d022daSYuri Pankov goto backtrack;
1364297a3b0SGarrett D'Amore
1374297a3b0SGarrett D'Amore /* Optimize for pattern with * at end or before /. */
1384297a3b0SGarrett D'Amore if (c == EOS)
1394297a3b0SGarrett D'Amore if (flags & FNM_PATHNAME)
140*f52b16c6SYuri Pankov return ((flags & FNM_LEADING_DIR) ||
141*f52b16c6SYuri Pankov strchr(string, '/') == NULL ?
1424297a3b0SGarrett D'Amore 0 : FNM_NOMATCH);
1434297a3b0SGarrett D'Amore else
1444297a3b0SGarrett D'Amore return (0);
1454297a3b0SGarrett D'Amore else if (c == '/' && flags & FNM_PATHNAME) {
1464297a3b0SGarrett D'Amore if ((string = strchr(string, '/')) == NULL)
1474297a3b0SGarrett D'Amore return (FNM_NOMATCH);
1484297a3b0SGarrett D'Amore break;
1494297a3b0SGarrett D'Amore }
1504297a3b0SGarrett D'Amore
15179d022daSYuri Pankov /*
15279d022daSYuri Pankov * First try the shortest match for the '*' that
15379d022daSYuri Pankov * could work. We can forget any earlier '*' since
15479d022daSYuri Pankov * there is no way having it match more characters
15579d022daSYuri Pankov * can help us, given that we are already here.
15679d022daSYuri Pankov */
15779d022daSYuri Pankov bt_pattern = pattern, bt_patmbs = patmbs;
15879d022daSYuri Pankov bt_string = string, bt_strmbs = strmbs;
1594297a3b0SGarrett D'Amore break;
1604297a3b0SGarrett D'Amore case '[':
1614297a3b0SGarrett D'Amore if (sc == EOS)
1624297a3b0SGarrett D'Amore return (FNM_NOMATCH);
1634297a3b0SGarrett D'Amore if (sc == '/' && (flags & FNM_PATHNAME))
16479d022daSYuri Pankov goto backtrack;
1654297a3b0SGarrett D'Amore if (sc == '.' && (flags & FNM_PERIOD) &&
1664297a3b0SGarrett D'Amore (string == stringstart ||
1674297a3b0SGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
16879d022daSYuri Pankov goto backtrack;
1694297a3b0SGarrett D'Amore
1704297a3b0SGarrett D'Amore switch (rangematch(pattern, sc, flags, &newp,
1712d08521bSGarrett D'Amore &patmbs, loc)) {
1724297a3b0SGarrett D'Amore case RANGE_ERROR:
1734297a3b0SGarrett D'Amore goto norm;
1744297a3b0SGarrett D'Amore case RANGE_MATCH:
1754297a3b0SGarrett D'Amore pattern = newp;
1764297a3b0SGarrett D'Amore break;
1774297a3b0SGarrett D'Amore case RANGE_NOMATCH:
17879d022daSYuri Pankov goto backtrack;
1794297a3b0SGarrett D'Amore }
1804297a3b0SGarrett D'Amore string += sclen;
1814297a3b0SGarrett D'Amore break;
1824297a3b0SGarrett D'Amore case '\\':
1834297a3b0SGarrett D'Amore if (!(flags & FNM_NOESCAPE)) {
1842d08521bSGarrett D'Amore pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX,
1852d08521bSGarrett D'Amore &patmbs, loc);
186*f52b16c6SYuri Pankov if (pclen == 0 || pclen == (size_t)-1 ||
187*f52b16c6SYuri Pankov pclen == (size_t)-2)
1884297a3b0SGarrett D'Amore return (FNM_NOMATCH);
1894297a3b0SGarrett D'Amore pattern += pclen;
1904297a3b0SGarrett D'Amore }
1914297a3b0SGarrett D'Amore /* FALLTHROUGH */
1924297a3b0SGarrett D'Amore default:
1934297a3b0SGarrett D'Amore norm:
1944297a3b0SGarrett D'Amore string += sclen;
19579d022daSYuri Pankov if (pc == sc)
19679d022daSYuri Pankov break;
197*f52b16c6SYuri Pankov else if ((flags & FNM_CASEFOLD) &&
1982d08521bSGarrett D'Amore (towlower_l(pc, loc) == towlower_l(sc, loc)))
19979d022daSYuri Pankov break;
20079d022daSYuri Pankov else {
20179d022daSYuri Pankov backtrack:
20279d022daSYuri Pankov /*
20379d022daSYuri Pankov * If we have a mismatch (other than hitting
20479d022daSYuri Pankov * the end of the string), go back to the last
20579d022daSYuri Pankov * '*' seen and have it match one additional
20679d022daSYuri Pankov * character.
20779d022daSYuri Pankov */
20879d022daSYuri Pankov if (bt_pattern == NULL)
2094297a3b0SGarrett D'Amore return (FNM_NOMATCH);
21079d022daSYuri Pankov sclen = mbrtowc_l(&sc, bt_string, MB_LEN_MAX,
21179d022daSYuri Pankov &bt_strmbs, loc);
21279d022daSYuri Pankov if (sclen == (size_t)-1 ||
21379d022daSYuri Pankov sclen == (size_t)-2) {
21479d022daSYuri Pankov sc = (unsigned char)*bt_string;
21579d022daSYuri Pankov sclen = 1;
21679d022daSYuri Pankov (void) memset(&bt_strmbs, 0,
21779d022daSYuri Pankov sizeof (bt_strmbs));
21879d022daSYuri Pankov }
21979d022daSYuri Pankov if (sc == EOS)
22079d022daSYuri Pankov return (FNM_NOMATCH);
22179d022daSYuri Pankov if (sc == '/' && flags & FNM_PATHNAME)
22279d022daSYuri Pankov return (FNM_NOMATCH);
22379d022daSYuri Pankov bt_string += sclen;
22479d022daSYuri Pankov pattern = bt_pattern, patmbs = bt_patmbs;
22579d022daSYuri Pankov string = bt_string, strmbs = bt_strmbs;
22679d022daSYuri Pankov }
2274297a3b0SGarrett D'Amore break;
2284297a3b0SGarrett D'Amore }
2294297a3b0SGarrett D'Amore }
2304297a3b0SGarrett D'Amore /* NOTREACHED */
2314297a3b0SGarrett D'Amore }
2324297a3b0SGarrett D'Amore
2334297a3b0SGarrett D'Amore static int
rangematch(const char * pattern,wchar_t test,int flags,char ** newp,mbstate_t * patmbs,locale_t loc)2342d08521bSGarrett D'Amore rangematch(const char *pattern, wchar_t test, int flags, char **newp,
2352d08521bSGarrett D'Amore mbstate_t *patmbs, locale_t loc)
2364297a3b0SGarrett D'Amore {
2374297a3b0SGarrett D'Amore int negate, ok;
2384297a3b0SGarrett D'Amore wchar_t c, c2;
2394297a3b0SGarrett D'Amore size_t pclen;
2404297a3b0SGarrett D'Amore const char *origpat;
2414297a3b0SGarrett D'Amore
2424297a3b0SGarrett D'Amore /*
2434297a3b0SGarrett D'Amore * A bracket expression starting with an unquoted circumflex
2444297a3b0SGarrett D'Amore * character produces unspecified results (IEEE 1003.2-1992,
2454297a3b0SGarrett D'Amore * 3.13.2). This implementation treats it like '!', for
2464297a3b0SGarrett D'Amore * consistency with the regular expression syntax.
2474297a3b0SGarrett D'Amore * J.T. Conklin (conklin@ngai.kaleida.com)
2484297a3b0SGarrett D'Amore */
2494297a3b0SGarrett D'Amore if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
2504297a3b0SGarrett D'Amore ++pattern;
2514297a3b0SGarrett D'Amore
252*f52b16c6SYuri Pankov if (flags & FNM_CASEFOLD)
2532d08521bSGarrett D'Amore test = towlower_l(test, loc);
2544297a3b0SGarrett D'Amore
2554297a3b0SGarrett D'Amore /*
2564297a3b0SGarrett D'Amore * A right bracket shall lose its special meaning and represent
2574297a3b0SGarrett D'Amore * itself in a bracket expression if it occurs first in the list.
2584297a3b0SGarrett D'Amore * -- POSIX.2 2.8.3.2
2594297a3b0SGarrett D'Amore */
2604297a3b0SGarrett D'Amore ok = 0;
2614297a3b0SGarrett D'Amore origpat = pattern;
2624297a3b0SGarrett D'Amore for (;;) {
2634297a3b0SGarrett D'Amore if (*pattern == ']' && pattern > origpat) {
2644297a3b0SGarrett D'Amore pattern++;
2654297a3b0SGarrett D'Amore break;
2664297a3b0SGarrett D'Amore } else if (*pattern == '\0') {
2674297a3b0SGarrett D'Amore return (RANGE_ERROR);
2684297a3b0SGarrett D'Amore } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
2694297a3b0SGarrett D'Amore return (RANGE_NOMATCH);
2704297a3b0SGarrett D'Amore } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
2714297a3b0SGarrett D'Amore pattern++;
2722d08521bSGarrett D'Amore pclen = mbrtowc_l(&c, pattern, MB_LEN_MAX, patmbs, loc);
2734297a3b0SGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
2744297a3b0SGarrett D'Amore return (RANGE_NOMATCH);
2754297a3b0SGarrett D'Amore pattern += pclen;
2764297a3b0SGarrett D'Amore
277*f52b16c6SYuri Pankov if (flags & FNM_CASEFOLD)
2782d08521bSGarrett D'Amore c = towlower_l(c, loc);
2794297a3b0SGarrett D'Amore
2804297a3b0SGarrett D'Amore if (*pattern == '-' && *(pattern + 1) != EOS &&
2814297a3b0SGarrett D'Amore *(pattern + 1) != ']') {
2824297a3b0SGarrett D'Amore if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
2834297a3b0SGarrett D'Amore if (*pattern != EOS)
2844297a3b0SGarrett D'Amore pattern++;
2852d08521bSGarrett D'Amore pclen = mbrtowc_l(&c2, pattern, MB_LEN_MAX, patmbs,
2862d08521bSGarrett D'Amore loc);
2874297a3b0SGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
2884297a3b0SGarrett D'Amore return (RANGE_NOMATCH);
2894297a3b0SGarrett D'Amore pattern += pclen;
2904297a3b0SGarrett D'Amore if (c2 == EOS)
2914297a3b0SGarrett D'Amore return (RANGE_ERROR);
2924297a3b0SGarrett D'Amore
293*f52b16c6SYuri Pankov if (flags & FNM_CASEFOLD)
2942d08521bSGarrett D'Amore c2 = towlower_l(c2, loc);
2954297a3b0SGarrett D'Amore
2962d08521bSGarrett D'Amore if (loc->collate->lc_is_posix ?
2974297a3b0SGarrett D'Amore c <= test && test <= c2 :
2982d08521bSGarrett D'Amore _collate_range_cmp(c, test, loc) <= 0 &&
2992d08521bSGarrett D'Amore _collate_range_cmp(test, c2, loc) <= 0)
3004297a3b0SGarrett D'Amore ok = 1;
3014297a3b0SGarrett D'Amore } else if (c == test)
3024297a3b0SGarrett D'Amore ok = 1;
3034297a3b0SGarrett D'Amore }
3044297a3b0SGarrett D'Amore
3054297a3b0SGarrett D'Amore *newp = (char *)pattern;
3064297a3b0SGarrett D'Amore return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
3074297a3b0SGarrett D'Amore }
308