18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1989, 1993, 1994 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 858f0484fSRodney W. Grimes * Guido van Rossum. 958f0484fSRodney W. Grimes * 103c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 115b5fa75aSEd Maste * 123c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 133c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 143c87aa1dSDavid Chisnall * 1558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1758f0484fSRodney W. Grimes * are met: 1858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 2058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 2158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 2258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 23fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 2458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2558f0484fSRodney W. Grimes * without specific prior written permission. 2658f0484fSRodney W. Grimes * 2758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3758f0484fSRodney W. Grimes * SUCH DAMAGE. 3858f0484fSRodney W. Grimes */ 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes /* 4158f0484fSRodney W. Grimes * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 4258f0484fSRodney W. Grimes * Compares a filename or pathname to a pattern. 4358f0484fSRodney W. Grimes */ 4458f0484fSRodney W. Grimes 459d88e270STim J. Robbins /* 469d88e270STim J. Robbins * Some notes on multibyte character support: 479d88e270STim J. Robbins * 1. Patterns with illegal byte sequences match nothing. 489d88e270STim J. Robbins * 2. Illegal byte sequences in the "string" argument are handled by treating 499d88e270STim J. Robbins * them as single-byte characters with a value of the first byte of the 509d88e270STim J. Robbins * sequence cast to wchar_t. 519d88e270STim J. Robbins * 3. Multibyte conversion state objects (mbstate_t) are passed around and 529d88e270STim J. Robbins * used for most, but not all, conversions. Further work will be required 539d88e270STim J. Robbins * to support state-dependent encodings. 549d88e270STim J. Robbins */ 559d88e270STim J. Robbins 5658f0484fSRodney W. Grimes #include <fnmatch.h> 579d88e270STim J. Robbins #include <limits.h> 5858f0484fSRodney W. Grimes #include <string.h> 599d88e270STim J. Robbins #include <wchar.h> 609d88e270STim J. Robbins #include <wctype.h> 6158f0484fSRodney W. Grimes 621daad8f5SAndrey A. Chernov #include "collate.h" 631daad8f5SAndrey A. Chernov 6458f0484fSRodney W. Grimes #define EOS '\0' 6558f0484fSRodney W. Grimes 66e2dbbd9eSAndrey A. Chernov #define RANGE_MATCH 1 67e2dbbd9eSAndrey A. Chernov #define RANGE_NOMATCH 0 68e2dbbd9eSAndrey A. Chernov #define RANGE_ERROR (-1) 69e2dbbd9eSAndrey A. Chernov 70*af79566bSBojan Novković static int rangematch(const char *, wchar_t, const char *, int, char **, 71*af79566bSBojan Novković char **, mbstate_t *, mbstate_t *); 72139ac6b2SJilles Tjoelker static int fnmatch1(const char *, const char *, const char *, int, mbstate_t, 73139ac6b2SJilles Tjoelker mbstate_t); 7458f0484fSRodney W. Grimes 7558f0484fSRodney W. Grimes int 7655b6b759SCraig Rodrigues fnmatch(const char *pattern, const char *string, int flags) 7758f0484fSRodney W. Grimes { 789d88e270STim J. Robbins static const mbstate_t initial; 799d88e270STim J. Robbins 80139ac6b2SJilles Tjoelker return (fnmatch1(pattern, string, string, flags, initial, initial)); 819d88e270STim J. Robbins } 829d88e270STim J. Robbins 839d88e270STim J. Robbins static int 8455b6b759SCraig Rodrigues fnmatch1(const char *pattern, const char *string, const char *stringstart, 8555b6b759SCraig Rodrigues int flags, mbstate_t patmbs, mbstate_t strmbs) 869d88e270STim J. Robbins { 873caeab9dSJilles Tjoelker const char *bt_pattern, *bt_string; 883caeab9dSJilles Tjoelker mbstate_t bt_patmbs, bt_strmbs; 89*af79566bSBojan Novković char *newp, *news; 909d88e270STim J. Robbins char c; 919d88e270STim J. Robbins wchar_t pc, sc; 929d88e270STim J. Robbins size_t pclen, sclen; 9358f0484fSRodney W. Grimes 943caeab9dSJilles Tjoelker bt_pattern = bt_string = NULL; 95139ac6b2SJilles Tjoelker for (;;) { 969d88e270STim J. Robbins pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs); 979d88e270STim J. Robbins if (pclen == (size_t)-1 || pclen == (size_t)-2) 989d88e270STim J. Robbins return (FNM_NOMATCH); 999d88e270STim J. Robbins pattern += pclen; 1009d88e270STim J. Robbins sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs); 1019d88e270STim J. Robbins if (sclen == (size_t)-1 || sclen == (size_t)-2) { 1029d88e270STim J. Robbins sc = (unsigned char)*string; 1039d88e270STim J. Robbins sclen = 1; 1049d88e270STim J. Robbins memset(&strmbs, 0, sizeof(strmbs)); 1059d88e270STim J. Robbins } 1069d88e270STim J. Robbins switch (pc) { 10758f0484fSRodney W. Grimes case EOS: 1089d88e270STim J. Robbins if ((flags & FNM_LEADING_DIR) && sc == '/') 1093deeb59dSAndrey A. Chernov return (0); 1103caeab9dSJilles Tjoelker if (sc == EOS) 1113caeab9dSJilles Tjoelker return (0); 1123caeab9dSJilles Tjoelker goto backtrack; 11358f0484fSRodney W. Grimes case '?': 1149d88e270STim J. Robbins if (sc == EOS) 11558f0484fSRodney W. Grimes return (FNM_NOMATCH); 1169d88e270STim J. Robbins if (sc == '/' && (flags & FNM_PATHNAME)) 1173caeab9dSJilles Tjoelker goto backtrack; 1189d88e270STim J. Robbins if (sc == '.' && (flags & FNM_PERIOD) && 11958f0484fSRodney W. Grimes (string == stringstart || 12058f0484fSRodney W. Grimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 1213caeab9dSJilles Tjoelker goto backtrack; 1229d88e270STim J. Robbins string += sclen; 12358f0484fSRodney W. Grimes break; 12458f0484fSRodney W. Grimes case '*': 12558f0484fSRodney W. Grimes c = *pattern; 12658f0484fSRodney W. Grimes /* Collapse multiple stars. */ 12758f0484fSRodney W. Grimes while (c == '*') 12858f0484fSRodney W. Grimes c = *++pattern; 12958f0484fSRodney W. Grimes 1309d88e270STim J. Robbins if (sc == '.' && (flags & FNM_PERIOD) && 13158f0484fSRodney W. Grimes (string == stringstart || 13258f0484fSRodney W. Grimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 1333caeab9dSJilles Tjoelker goto backtrack; 13458f0484fSRodney W. Grimes 13558f0484fSRodney W. Grimes /* Optimize for pattern with * at end or before /. */ 13658f0484fSRodney W. Grimes if (c == EOS) 13758f0484fSRodney W. Grimes if (flags & FNM_PATHNAME) 138298c8e3dSJohn Polstra return ((flags & FNM_LEADING_DIR) || 139298c8e3dSJohn Polstra strchr(string, '/') == NULL ? 14058f0484fSRodney W. Grimes 0 : FNM_NOMATCH); 14158f0484fSRodney W. Grimes else 14258f0484fSRodney W. Grimes return (0); 14358f0484fSRodney W. Grimes else if (c == '/' && flags & FNM_PATHNAME) { 14458f0484fSRodney W. Grimes if ((string = strchr(string, '/')) == NULL) 14558f0484fSRodney W. Grimes return (FNM_NOMATCH); 14658f0484fSRodney W. Grimes break; 14758f0484fSRodney W. Grimes } 14858f0484fSRodney W. Grimes 1493caeab9dSJilles Tjoelker /* 1503caeab9dSJilles Tjoelker * First try the shortest match for the '*' that 1513caeab9dSJilles Tjoelker * could work. We can forget any earlier '*' since 1523caeab9dSJilles Tjoelker * there is no way having it match more characters 1533caeab9dSJilles Tjoelker * can help us, given that we are already here. 1543caeab9dSJilles Tjoelker */ 1553caeab9dSJilles Tjoelker bt_pattern = pattern, bt_patmbs = patmbs; 1563caeab9dSJilles Tjoelker bt_string = string, bt_strmbs = strmbs; 15758f0484fSRodney W. Grimes break; 15858f0484fSRodney W. Grimes case '[': 1599d88e270STim J. Robbins if (sc == EOS) 16058f0484fSRodney W. Grimes return (FNM_NOMATCH); 1619d88e270STim J. Robbins if (sc == '/' && (flags & FNM_PATHNAME)) 1623caeab9dSJilles Tjoelker goto backtrack; 1639d88e270STim J. Robbins if (sc == '.' && (flags & FNM_PERIOD) && 16405a068e6SAndrey A. Chernov (string == stringstart || 16505a068e6SAndrey A. Chernov ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 1663caeab9dSJilles Tjoelker goto backtrack; 16705a068e6SAndrey A. Chernov 168*af79566bSBojan Novković switch (rangematch(pattern, sc, string + sclen, flags, 169*af79566bSBojan Novković &newp, &news, &patmbs, &strmbs)) { 170e2dbbd9eSAndrey A. Chernov case RANGE_ERROR: 171e2dbbd9eSAndrey A. Chernov goto norm; 172e2dbbd9eSAndrey A. Chernov case RANGE_MATCH: 173e2dbbd9eSAndrey A. Chernov pattern = newp; 174*af79566bSBojan Novković string = news; 175e2dbbd9eSAndrey A. Chernov break; 176e2dbbd9eSAndrey A. Chernov case RANGE_NOMATCH: 1773caeab9dSJilles Tjoelker goto backtrack; 178e2dbbd9eSAndrey A. Chernov } 17958f0484fSRodney W. Grimes break; 18058f0484fSRodney W. Grimes case '\\': 18158f0484fSRodney W. Grimes if (!(flags & FNM_NOESCAPE)) { 1829d88e270STim J. Robbins pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, 1839d88e270STim J. Robbins &patmbs); 184303cbb93SPedro F. Giffuni if (pclen == 0 || pclen == (size_t)-1 || 185303cbb93SPedro F. Giffuni pclen == (size_t)-2) 1869d88e270STim J. Robbins return (FNM_NOMATCH); 1879d88e270STim J. Robbins pattern += pclen; 18858f0484fSRodney W. Grimes } 18958f0484fSRodney W. Grimes /* FALLTHROUGH */ 19058f0484fSRodney W. Grimes default: 191e2dbbd9eSAndrey A. Chernov norm: 1923caeab9dSJilles Tjoelker string += sclen; 1939d88e270STim J. Robbins if (pc == sc) 19495e4966cSWolfram Schneider ; 1953deeb59dSAndrey A. Chernov else if ((flags & FNM_CASEFOLD) && 1969d88e270STim J. Robbins (towlower(pc) == towlower(sc))) 19795e4966cSWolfram Schneider ; 1983caeab9dSJilles Tjoelker else { 1993caeab9dSJilles Tjoelker backtrack: 2003caeab9dSJilles Tjoelker /* 2013caeab9dSJilles Tjoelker * If we have a mismatch (other than hitting 2023caeab9dSJilles Tjoelker * the end of the string), go back to the last 2033caeab9dSJilles Tjoelker * '*' seen and have it match one additional 2043caeab9dSJilles Tjoelker * character. 2053caeab9dSJilles Tjoelker */ 2063caeab9dSJilles Tjoelker if (bt_pattern == NULL) 20758f0484fSRodney W. Grimes return (FNM_NOMATCH); 2083caeab9dSJilles Tjoelker sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX, 2093caeab9dSJilles Tjoelker &bt_strmbs); 2103caeab9dSJilles Tjoelker if (sclen == (size_t)-1 || 2113caeab9dSJilles Tjoelker sclen == (size_t)-2) { 2123caeab9dSJilles Tjoelker sc = (unsigned char)*bt_string; 2133caeab9dSJilles Tjoelker sclen = 1; 2143caeab9dSJilles Tjoelker memset(&bt_strmbs, 0, 2153caeab9dSJilles Tjoelker sizeof(bt_strmbs)); 2163caeab9dSJilles Tjoelker } 2173caeab9dSJilles Tjoelker if (sc == EOS) 2183caeab9dSJilles Tjoelker return (FNM_NOMATCH); 2193caeab9dSJilles Tjoelker if (sc == '/' && flags & FNM_PATHNAME) 2203caeab9dSJilles Tjoelker return (FNM_NOMATCH); 2213caeab9dSJilles Tjoelker bt_string += sclen; 222*af79566bSBojan Novković pattern = bt_pattern; 223*af79566bSBojan Novković patmbs = bt_patmbs; 224*af79566bSBojan Novković string = bt_string; 225*af79566bSBojan Novković strmbs = bt_strmbs; 2263caeab9dSJilles Tjoelker } 22758f0484fSRodney W. Grimes break; 22858f0484fSRodney W. Grimes } 2299d88e270STim J. Robbins } 23058f0484fSRodney W. Grimes /* NOTREACHED */ 23158f0484fSRodney W. Grimes } 23258f0484fSRodney W. Grimes 233e2dbbd9eSAndrey A. Chernov static int 234*af79566bSBojan Novković rangematch(const char *pattern, wchar_t test, const char *string, int flags, 235*af79566bSBojan Novković char **newp, char **news, mbstate_t *patmbs, mbstate_t *strmbs) 23658f0484fSRodney W. Grimes { 237e728d480SAndrey A. Chernov int negate, ok; 2389d88e270STim J. Robbins wchar_t c, c2; 2399d88e270STim J. Robbins size_t pclen; 2409d88e270STim J. Robbins const char *origpat; 2411daad8f5SAndrey A. Chernov struct xlocale_collate *table = 2421daad8f5SAndrey A. Chernov (struct xlocale_collate *)__get_locale()->components[XLC_COLLATE]; 243*af79566bSBojan Novković wchar_t buf[COLLATE_STR_LEN]; /* STR_LEN defined in collate.h */ 244*af79566bSBojan Novković const char *cp, *savestring; 245*af79566bSBojan Novković int special; 246*af79566bSBojan Novković mbstate_t save; 247*af79566bSBojan Novković size_t sclen, len; 24858f0484fSRodney W. Grimes 24958f0484fSRodney W. Grimes /* 25058f0484fSRodney W. Grimes * A bracket expression starting with an unquoted circumflex 25158f0484fSRodney W. Grimes * character produces unspecified results (IEEE 1003.2-1992, 25258f0484fSRodney W. Grimes * 3.13.2). This implementation treats it like '!', for 25358f0484fSRodney W. Grimes * consistency with the regular expression syntax. 25458f0484fSRodney W. Grimes * J.T. Conklin (conklin@ngai.kaleida.com) 25558f0484fSRodney W. Grimes */ 25605a068e6SAndrey A. Chernov if ((negate = (*pattern == '!' || *pattern == '^'))) 25758f0484fSRodney W. Grimes ++pattern; 25858f0484fSRodney W. Grimes 2593deeb59dSAndrey A. Chernov if (flags & FNM_CASEFOLD) 2609d88e270STim J. Robbins test = towlower(test); 26195e4966cSWolfram Schneider 262e2dbbd9eSAndrey A. Chernov /* 263e2dbbd9eSAndrey A. Chernov * A right bracket shall lose its special meaning and represent 264e2dbbd9eSAndrey A. Chernov * itself in a bracket expression if it occurs first in the list. 265e2dbbd9eSAndrey A. Chernov * -- POSIX.2 2.8.3.2 266e2dbbd9eSAndrey A. Chernov */ 267e728d480SAndrey A. Chernov ok = 0; 2689d88e270STim J. Robbins origpat = pattern; 2699d88e270STim J. Robbins for (;;) { 270*af79566bSBojan Novković c = 0; 2719d88e270STim J. Robbins if (*pattern == ']' && pattern > origpat) { 2729d88e270STim J. Robbins break; 2739d88e270STim J. Robbins } else if (*pattern == '\0') { 274e2dbbd9eSAndrey A. Chernov return (RANGE_ERROR); 2759d88e270STim J. Robbins } else if (*pattern == '/' && (flags & FNM_PATHNAME)) { 27605a068e6SAndrey A. Chernov return (RANGE_NOMATCH); 277*af79566bSBojan Novković } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE)) { 2789d88e270STim J. Robbins pattern++; 279*af79566bSBojan Novković } else if (*pattern == '[' && 280*af79566bSBojan Novković ((special = *(pattern + 1)) == '.' || 281*af79566bSBojan Novković special == '=' || special == ':')) { 282*af79566bSBojan Novković cp = (pattern += 2); 283*af79566bSBojan Novković while ((cp = strchr(cp, special))) { 284*af79566bSBojan Novković if (*(cp + 1) == ']') 285*af79566bSBojan Novković break; 286*af79566bSBojan Novković cp++; 287*af79566bSBojan Novković } 288*af79566bSBojan Novković if (!cp) 289*af79566bSBojan Novković return (RANGE_ERROR); 290*af79566bSBojan Novković if (special == '.') { 291*af79566bSBojan Novković treat_like_collating_symbol: 292*af79566bSBojan Novković len = __collate_collating_symbol(buf, 293*af79566bSBojan Novković COLLATE_STR_LEN, pattern, 294*af79566bSBojan Novković cp - pattern, patmbs); 295*af79566bSBojan Novković if (len == (size_t)-1 || len == 0) 296*af79566bSBojan Novković return (RANGE_ERROR); 297*af79566bSBojan Novković pattern = cp + 2; 298*af79566bSBojan Novković if (len > 1) { 299*af79566bSBojan Novković wchar_t *wp, sc; 300*af79566bSBojan Novković 301*af79566bSBojan Novković /* 302*af79566bSBojan Novković * No multi-character collation 303*af79566bSBojan Novković * symbols as start of range. 304*af79566bSBojan Novković */ 305*af79566bSBojan Novković if (*(cp + 2) == '-' && 306*af79566bSBojan Novković *(cp + 3) != EOS && 307*af79566bSBojan Novković *(cp + 3) != ']') 308*af79566bSBojan Novković return (RANGE_ERROR); 309*af79566bSBojan Novković wp = buf; 310*af79566bSBojan Novković if (test != *wp++) 311*af79566bSBojan Novković continue; 312*af79566bSBojan Novković if (len == 1) { 313*af79566bSBojan Novković ok = 1; 314*af79566bSBojan Novković break; 315*af79566bSBojan Novković } 316*af79566bSBojan Novković memcpy(&save, strmbs, sizeof(save)); 317*af79566bSBojan Novković savestring = string; 318*af79566bSBojan Novković while (--len > 0) { 319*af79566bSBojan Novković sclen = mbrtowc(&sc, string, 320*af79566bSBojan Novković MB_LEN_MAX, strmbs); 321*af79566bSBojan Novković if (sclen == (size_t)-1 || 322*af79566bSBojan Novković sclen == (size_t)-2) { 323*af79566bSBojan Novković sc = (unsigned char)*string; 324*af79566bSBojan Novković sclen = 1; 325*af79566bSBojan Novković memset(&strmbs, 0, 326*af79566bSBojan Novković sizeof(strmbs)); 327*af79566bSBojan Novković } 328*af79566bSBojan Novković if (sc != *wp++) { 329*af79566bSBojan Novković memcpy(strmbs, &save, 330*af79566bSBojan Novković sizeof(save)); 331*af79566bSBojan Novković string = savestring; 332*af79566bSBojan Novković break; 333*af79566bSBojan Novković } 334*af79566bSBojan Novković string += sclen; 335*af79566bSBojan Novković } 336*af79566bSBojan Novković if (len == 0) { 337*af79566bSBojan Novković ok = 1; 338*af79566bSBojan Novković break; 339*af79566bSBojan Novković } 340*af79566bSBojan Novković continue; /* no match */ 341*af79566bSBojan Novković } 342*af79566bSBojan Novković c = *buf; 343*af79566bSBojan Novković } else if (special == '=') { 344*af79566bSBojan Novković int ec; 345*af79566bSBojan Novković memcpy(&save, patmbs, sizeof(save)); 346*af79566bSBojan Novković ec = __collate_equiv_class(pattern, 347*af79566bSBojan Novković cp - pattern, patmbs); 348*af79566bSBojan Novković if (ec < 0) 349*af79566bSBojan Novković return (RANGE_ERROR); 350*af79566bSBojan Novković if (ec == 0) { 351*af79566bSBojan Novković memcpy(patmbs, &save, sizeof(save)); 352*af79566bSBojan Novković goto treat_like_collating_symbol; 353*af79566bSBojan Novković } 354*af79566bSBojan Novković pattern = cp + 2; 355*af79566bSBojan Novković /* no equivalence classes as start of range */ 356*af79566bSBojan Novković if (*(cp + 2) == '-' && *(cp + 3) != EOS && 357*af79566bSBojan Novković *(cp + 3) != ']') 358*af79566bSBojan Novković return (RANGE_ERROR); 359*af79566bSBojan Novković len = __collate_equiv_match(ec, NULL, 0, test, 360*af79566bSBojan Novković string, strlen(string), strmbs, &sclen); 361*af79566bSBojan Novković if (len < 0) 362*af79566bSBojan Novković return (RANGE_ERROR); 363*af79566bSBojan Novković if (len > 0) { 364*af79566bSBojan Novković ok = 1; 365*af79566bSBojan Novković string += sclen; 366*af79566bSBojan Novković break; 367*af79566bSBojan Novković } 368*af79566bSBojan Novković continue; 369*af79566bSBojan Novković } else { /* special == ':' */ 370*af79566bSBojan Novković wctype_t charclass; 371*af79566bSBojan Novković char name[CHARCLASS_NAME_MAX + 1]; 372*af79566bSBojan Novković /* no character classes as start of range */ 373*af79566bSBojan Novković if (*(cp + 2) == '-' && *(cp + 3) != EOS && 374*af79566bSBojan Novković *(cp + 3) != ']') 375*af79566bSBojan Novković return (RANGE_ERROR); 376*af79566bSBojan Novković /* assume character class names are ascii */ 377*af79566bSBojan Novković if (cp - pattern > CHARCLASS_NAME_MAX) 378*af79566bSBojan Novković return (RANGE_ERROR); 379*af79566bSBojan Novković strlcpy(name, pattern, cp - pattern + 1); 380*af79566bSBojan Novković pattern = cp + 2; 381*af79566bSBojan Novković if ((charclass = wctype(name)) == 0) 382*af79566bSBojan Novković return (RANGE_ERROR); 383*af79566bSBojan Novković if (iswctype(test, charclass)) { 384*af79566bSBojan Novković ok = 1; 385*af79566bSBojan Novković break; 386*af79566bSBojan Novković } 387*af79566bSBojan Novković continue; 388*af79566bSBojan Novković } 389*af79566bSBojan Novković } 390*af79566bSBojan Novković if (!c) { 3919d88e270STim J. Robbins pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs); 3929d88e270STim J. Robbins if (pclen == (size_t)-1 || pclen == (size_t)-2) 3939d88e270STim J. Robbins return (RANGE_NOMATCH); 3949d88e270STim J. Robbins pattern += pclen; 395*af79566bSBojan Novković } 3963deeb59dSAndrey A. Chernov if (flags & FNM_CASEFOLD) 3979d88e270STim J. Robbins c = towlower(c); 39895e4966cSWolfram Schneider 3999d88e270STim J. Robbins if (*pattern == '-' && *(pattern + 1) != EOS && 4009d88e270STim J. Robbins *(pattern + 1) != ']') { 4019d88e270STim J. Robbins if (*++pattern == '\\' && !(flags & FNM_NOESCAPE)) 4029d88e270STim J. Robbins if (*pattern != EOS) 4039d88e270STim J. Robbins pattern++; 4049d88e270STim J. Robbins pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs); 4059d88e270STim J. Robbins if (pclen == (size_t)-1 || pclen == (size_t)-2) 4069d88e270STim J. Robbins return (RANGE_NOMATCH); 4079d88e270STim J. Robbins pattern += pclen; 40858f0484fSRodney W. Grimes if (c2 == EOS) 409e2dbbd9eSAndrey A. Chernov return (RANGE_ERROR); 41095e4966cSWolfram Schneider 411*af79566bSBojan Novković if ((c2 == '[' && (special = *pattern) == '.') || 412*af79566bSBojan Novković special == '=' || special == ':') { 413*af79566bSBojan Novković 414*af79566bSBojan Novković /* 415*af79566bSBojan Novković * No equivalence classes or character 416*af79566bSBojan Novković * classes as end of range. 417*af79566bSBojan Novković */ 418*af79566bSBojan Novković if (special == '=' || special == ':') 419*af79566bSBojan Novković return (RANGE_ERROR); 420*af79566bSBojan Novković cp = ++pattern; 421*af79566bSBojan Novković while ((cp = strchr(cp, special))) { 422*af79566bSBojan Novković if (*(cp + 1) == ']') 423*af79566bSBojan Novković break; 424*af79566bSBojan Novković cp++; 425*af79566bSBojan Novković } 426*af79566bSBojan Novković if (!cp) 427*af79566bSBojan Novković return (RANGE_ERROR); 428*af79566bSBojan Novković len = __collate_collating_symbol(buf, 429*af79566bSBojan Novković COLLATE_STR_LEN, pattern, 430*af79566bSBojan Novković cp - pattern, patmbs); 431*af79566bSBojan Novković 432*af79566bSBojan Novković /* 433*af79566bSBojan Novković * No multi-character collation symbols 434*af79566bSBojan Novković * as end of range. 435*af79566bSBojan Novković */ 436*af79566bSBojan Novković if (len != 1) 437*af79566bSBojan Novković return (RANGE_ERROR); 438*af79566bSBojan Novković pattern = cp + 2; 439*af79566bSBojan Novković c2 = *buf; 440*af79566bSBojan Novković } 441*af79566bSBojan Novković 4423deeb59dSAndrey A. Chernov if (flags & FNM_CASEFOLD) 4439d88e270STim J. Robbins c2 = towlower(c2); 44495e4966cSWolfram Schneider 4451daad8f5SAndrey A. Chernov if (table->__collate_load_error ? 4461daad8f5SAndrey A. Chernov c <= test && test <= c2 : 44712eae8c8SAndrey A. Chernov __wcollate_range_cmp(c, test) <= 0 44812eae8c8SAndrey A. Chernov && __wcollate_range_cmp(test, c2) <= 0 449*af79566bSBojan Novković ) { 45058f0484fSRodney W. Grimes ok = 1; 451*af79566bSBojan Novković break; 452*af79566bSBojan Novković } 453*af79566bSBojan Novković } else if (c == test) { 45458f0484fSRodney W. Grimes ok = 1; 455*af79566bSBojan Novković break; 456*af79566bSBojan Novković } 4579d88e270STim J. Robbins } 458e728d480SAndrey A. Chernov 459*af79566bSBojan Novković /* go to end of bracket expression */ 460*af79566bSBojan Novković special = 0; 461*af79566bSBojan Novković while (*pattern != ']') { 462*af79566bSBojan Novković if (*pattern == 0) 463*af79566bSBojan Novković return (RANGE_ERROR); 464*af79566bSBojan Novković if (*pattern == special) { 465*af79566bSBojan Novković if (*++pattern == ']') { 466*af79566bSBojan Novković special = 0; 467*af79566bSBojan Novković pattern++; 468*af79566bSBojan Novković } 469*af79566bSBojan Novković continue; 470*af79566bSBojan Novković } 471*af79566bSBojan Novković if (!special && *pattern == '[') { 472*af79566bSBojan Novković special = *++pattern; 473*af79566bSBojan Novković if (special != '.' && special != '=' && special != ':') 474*af79566bSBojan Novković special = 0; 475*af79566bSBojan Novković else 476*af79566bSBojan Novković pattern++; 477*af79566bSBojan Novković continue; 478*af79566bSBojan Novković } 479*af79566bSBojan Novković pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs); 480*af79566bSBojan Novković if (pclen == (size_t)-1 || pclen == (size_t)-2) 481*af79566bSBojan Novković return (RANGE_NOMATCH); 482*af79566bSBojan Novković pattern += pclen; 483*af79566bSBojan Novković } 484*af79566bSBojan Novković 485*af79566bSBojan Novković *newp = (char *)++pattern; 486*af79566bSBojan Novković *news = (char *)string; 487*af79566bSBojan Novković 488e2dbbd9eSAndrey A. Chernov return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); 48958f0484fSRodney W. Grimes } 490