4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 1989, 1993, 1994
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The Regents of the University of California. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * This code is derived from software contributed to Berkeley by
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Guido van Rossum.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * may be used to endorse or promote products derived from this software
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * without specific prior written permission.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Use is subject to license terms.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Compares a filename or pathname to a pattern.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Some notes on multibyte character support:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Patterns with illegal byte sequences match nothing.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Illegal byte sequences in the "string" argument are handled by treating
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * them as single-byte characters with a value of the first byte of the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * sequence cast to wchar_t.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 3. Multibyte conversion state objects (mbstate_t) are passed around and
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * used for most, but not all, conversions. Further work will be required
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * to support state-dependent encodings.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic int rangematch(const char *, wchar_t, int, char **, mbstate_t *,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (fnmatch1(pattern, string, string, flags, initial, initial,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorefnmatch1(const char *pattern, const char *string, const char *stringstart,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int flags, mbstate_t patmbs, mbstate_t strmbs, locale_t loc)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, &patmbs, loc);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore sclen = mbrtowc_l(&sc, string, MB_LEN_MAX, &strmbs, loc);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (sclen == (size_t)-1 || sclen == (size_t)-2) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Removed FNM_LEADING_DIR, as it is not present
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * on Solaris.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* Collapse multiple stars. */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore while (c == '*')
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* Optimize for pattern with * at end or before /. */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else if (c == '/' && flags & FNM_PATHNAME) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* General case, use recursion. */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore switch (rangematch(pattern, sc, flags, &newp,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* FALLTHROUGH */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (towlower_l(pc, loc) == towlower_l(sc, loc)))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* NOTREACHED */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorerangematch(const char *pattern, wchar_t test, int flags, char **newp,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * A bracket expression starting with an unquoted circumflex
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * character produces unspecified results (IEEE 1003.2-1992,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 3.13.2). This implementation treats it like '!', for
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * consistency with the regular expression syntax.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * J.T. Conklin (conklin@ngai.kaleida.com)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * A right bracket shall lose its special meaning and represent
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * itself in a bracket expression if it occurs first in the list.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * -- POSIX.2 2.8.3.2
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pclen = mbrtowc_l(&c, pattern, MB_LEN_MAX, patmbs, loc);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (*pattern == '-' && *(pattern + 1) != EOS &&
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pclen = mbrtowc_l(&c2, pattern, MB_LEN_MAX, patmbs,
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (pclen == (size_t)-1 || pclen == (size_t)-2)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore } else if (c == test)