STM32F7 Ethernet interface for nucleo STM32F767

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cfstore_fnmatch.c Source File

cfstore_fnmatch.c

00001 /*
00002  * Copyright (c) 1989, 1993, 1994
00003  *  The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Guido van Rossum.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 4. Neither the name of the University nor the names of its contributors
00017  *    may be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  */
00032 
00033 #ifndef CFSTORE_NO_FNMATCH
00034 #define CFSTORE_NO_FNMATCH
00035 
00036 #if defined(LIBC_SCCS) && !defined(lint)
00037 static char sccsid[] = "@(#)cfstore_fnmatch.c   8.2 (Berkeley) 4/16/94";
00038 #endif /* LIBC_SCCS and not lint */
00039 
00040 /* In order to support ARM toolchain, this have been removed from the original
00041  * cfstore_fnmatch.c from newlib.
00042  * #include <sys/cdefs.h>
00043  */
00044 
00045 /*
00046  * Function cfstore_fnmatch() as specified in POSIX 1003.2-1992, section B.6.
00047  * Compares a filename or pathname to a pattern.
00048  */
00049 
00050 #include "cfstore_fnmatch.h"
00051 #include <ctype.h>
00052 #include <string.h>
00053 #include <stdio.h>
00054 #include <limits.h>
00055 
00056 /* code copied from the original cfstore_fnmatch.h */
00057 
00058 #define FNM_NOESCAPE    0x01    /* Disable backslash escaping. */
00059 #define FNM_PATHNAME    0x02    /* Slash must be matched by slash. */
00060 #define FNM_PERIOD  0x04    /* Period must be matched by period. */
00061 
00062 #define FNM_LEADING_DIR 0x08    /* Ignore /<tail> after Imatch. */
00063 #define FNM_CASEFOLD    0x10    /* Case insensitive search. */
00064 
00065 
00066 #define EOS '\0'
00067 
00068 #define RANGE_MATCH     1
00069 #define RANGE_NOMATCH   0
00070 #define RANGE_ERROR     (-1)
00071 
00072 
00073 /* In order to support ARM toolchain and simplify the number of newlib posix files used,
00074  * this have been copied from collate.c, and the license for this code has been included at the
00075  * here:
00076  *
00077  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
00078  *      at Electronni Visti IA, Kiev, Ukraine.
00079  *          All rights reserved.
00080  *
00081  * Redistribution and use in source and binary forms, with or without
00082  * modification, are permitted provided that the following conditions
00083  * are met:
00084  * 1. Redistributions of source code must retain the above copyright
00085  *    notice, this list of conditions and the following disclaimer.
00086  * 2. Redistributions in binary form must reproduce the above copyright
00087  *    notice, this list of conditions and the following disclaimer in the
00088  *    documentation and/or other materials provided with the distribution.
00089  *
00090  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
00091  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00092  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00093  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
00094  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00095  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00096  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00097  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00098  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00099  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00100  * SUCH DAMAGE.
00101  *
00102  */
00103 int __collate_load_error = 1;
00104 
00105 /* In order to support ARM toolchain and simplify the number of newlib posix files used,
00106  * the following has been copied from collcmp.c, and the license for this code is
00107  * included here:
00108  *
00109  * Copyright (C) 1996 by Andrey A. Chernov, Moscow, Russia.
00110  * All rights reserved.
00111  *
00112  * Redistribution and use in source and binary forms, with or without
00113  * modification, are permitted provided that the following conditions
00114  * are met:
00115  * 1. Redistributions of source code must retain the above copyright
00116  *    notice, this list of conditions and the following disclaimer.
00117  * 2. Redistributions in binary form must reproduce the above copyright
00118  *    notice, this list of conditions and the following disclaimer in the
00119  *    documentation and/or other materials provided with the distribution.
00120  *
00121  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
00122  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00123  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00124  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00125  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00126  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00127  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00128  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00129  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00130  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00131  * SUCH DAMAGE.
00132  */
00133 
00134 /*
00135  * Compare two characters converting collate information
00136  * into ASCII-compatible range, it allows to handle
00137  * "[a-z]"-type ranges with national characters.
00138  */
00139 
00140 static int __collate_range_cmp (int c1, int c2)
00141 {
00142     static char s1[2], s2[2];
00143     int ret;
00144 
00145     c1 &= UCHAR_MAX;
00146     c2 &= UCHAR_MAX;
00147     if (c1 == c2)
00148         return (0);
00149 
00150     s1[0] = c1;
00151     s2[0] = c2;
00152     if ((ret = strcoll(s1, s2)) != 0)
00153         return (ret);
00154     return (c1 - c2);
00155 }
00156 
00157 
00158 static int rangematch(const char *, char, int, char **);
00159 
00160 int cfstore_fnmatch(const char *pattern, const char *string, int flags)
00161 {
00162     const char *stringstart;
00163     char *newp;
00164     char c, test;
00165 
00166     for (stringstart = string;;)
00167         switch (c = *pattern++) {
00168         case EOS:
00169             if ((flags & FNM_LEADING_DIR) && *string == '/')
00170                 return (0);
00171             return (*string == EOS ? 0 : CFSTORE_FNM_NOMATCH);
00172         case '?':
00173             if (*string == EOS)
00174                 return (CFSTORE_FNM_NOMATCH);
00175             if (*string == '/' && (flags & FNM_PATHNAME))
00176                 return (CFSTORE_FNM_NOMATCH);
00177             if (*string == '.' && (flags & FNM_PERIOD) &&
00178                 (string == stringstart ||
00179                 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
00180                 return (CFSTORE_FNM_NOMATCH);
00181             ++string;
00182             break;
00183         case '*':
00184             c = *pattern;
00185             /* Collapse multiple stars. */
00186             while (c == '*')
00187                 c = *++pattern;
00188 
00189             if (*string == '.' && (flags & FNM_PERIOD) &&
00190                 (string == stringstart ||
00191                 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
00192                 return (CFSTORE_FNM_NOMATCH);
00193 
00194             /* Optimize for pattern with * at end or before /. */
00195             if (c == EOS)
00196                 if (flags & FNM_PATHNAME)
00197                     return ((flags & FNM_LEADING_DIR) ||
00198                         strchr(string, '/') == NULL ?
00199                         0 : CFSTORE_FNM_NOMATCH);
00200                 else
00201                     return (0);
00202             else if (c == '/' && flags & FNM_PATHNAME) {
00203                 if ((string = strchr(string, '/')) == NULL)
00204                     return (CFSTORE_FNM_NOMATCH);
00205                 break;
00206             }
00207 
00208             /* General case, use recursion. */
00209             while ((test = *string) != EOS) {
00210                 if (!cfstore_fnmatch(pattern, string, flags & ~FNM_PERIOD))
00211                     return (0);
00212                 if (test == '/' && flags & FNM_PATHNAME)
00213                     break;
00214                 ++string;
00215             }
00216             return (CFSTORE_FNM_NOMATCH);
00217         case '[':
00218             if (*string == EOS)
00219                 return (CFSTORE_FNM_NOMATCH);
00220             if (*string == '/' && (flags & FNM_PATHNAME))
00221                 return (CFSTORE_FNM_NOMATCH);
00222             if (*string == '.' && (flags & FNM_PERIOD) &&
00223                 (string == stringstart ||
00224                 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
00225                 return (CFSTORE_FNM_NOMATCH);
00226 
00227             switch (rangematch(pattern, *string, flags, &newp)) {
00228             case RANGE_ERROR:
00229                 goto norm;
00230             case RANGE_MATCH:
00231                 pattern = newp;
00232                 break;
00233             case RANGE_NOMATCH:
00234                 return (CFSTORE_FNM_NOMATCH);
00235             }
00236             ++string;
00237             break;
00238         case '\\':
00239             if (!(flags & FNM_NOESCAPE)) {
00240                 if ((c = *pattern++) == EOS) {
00241                     c = '\\';
00242                     --pattern;
00243                 }
00244             }
00245             /* FALLTHROUGH */
00246         default:
00247         norm:
00248             if (c == *string)
00249                 ;
00250             else if ((flags & FNM_CASEFOLD) &&
00251                  (tolower((unsigned char)c) ==
00252                   tolower((unsigned char)*string)))
00253                 ;
00254             else
00255                 return (CFSTORE_FNM_NOMATCH);
00256             string++;
00257             break;
00258         }
00259     /* NOTREACHED */
00260 }
00261 
00262 static int
00263 rangematch(const char *pattern, char test, int flags, char **newp)
00264 {
00265     int negate, ok;
00266     char c, c2;
00267 
00268     /*
00269      * A bracket expression starting with an unquoted circumflex
00270      * character produces unspecified results (IEEE 1003.2-1992,
00271      * 3.13.2).  This implementation treats it like '!', for
00272      * consistency with the regular expression syntax.
00273      * J.T. Conklin (conklin@ngai.kaleida.com)
00274      */
00275     negate = (*pattern == '!' || *pattern == '^');
00276     if ( negate )
00277         ++pattern;
00278 
00279     if (flags & FNM_CASEFOLD)
00280         test = tolower((unsigned char)test);
00281 
00282     /*
00283      * A right bracket shall lose its special meaning and represent
00284      * itself in a bracket expression if it occurs first in the list.
00285      * -- POSIX.2 2.8.3.2
00286      */
00287     ok = 0;
00288     c = *pattern++;
00289     do {
00290         if (c == '\\' && !(flags & FNM_NOESCAPE))
00291             c = *pattern++;
00292         if (c == EOS)
00293             return (RANGE_ERROR);
00294 
00295         if (c == '/' && (flags & FNM_PATHNAME))
00296             return (RANGE_NOMATCH);
00297 
00298         if (flags & FNM_CASEFOLD)
00299             c = tolower((unsigned char)c);
00300 
00301         if (*pattern == '-'
00302             && (c2 = *(pattern+1)) != EOS && c2 != ']') {
00303             pattern += 2;
00304             if (c2 == '\\' && !(flags & FNM_NOESCAPE))
00305                 c2 = *pattern++;
00306             if (c2 == EOS)
00307                 return (RANGE_ERROR);
00308 
00309             if (flags & FNM_CASEFOLD)
00310                 c2 = tolower((unsigned char)c2);
00311 
00312             if (__collate_load_error ?
00313                 c <= test && test <= c2 :
00314                    __collate_range_cmp(c, test) <= 0
00315                 && __collate_range_cmp(test, c2) <= 0
00316                )
00317                 ok = 1;
00318         } else if (c == test)
00319             ok = 1;
00320     } while ((c = *pattern++) != ']');
00321 
00322     *newp = (char *)pattern;
00323     return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
00324 }
00325 
00326 #endif /* CFSTORE_NO_FNMATCH  */