]> git.proxmox.com Git - libgit2.git/commitdiff
Always use internal fnmatch, not system
authorRussell Belfer <rb@github.com>
Thu, 11 Oct 2012 18:58:00 +0000 (11:58 -0700)
committerRussell Belfer <rb@github.com>
Mon, 15 Oct 2012 19:54:46 +0000 (12:54 -0700)
src/compat/fnmatch.c [deleted file]
src/compat/fnmatch.h [deleted file]
src/fnmatch.c [new file with mode: 0644]
src/fnmatch.h [new file with mode: 0644]
src/posix.h
src/unix/posix.h
src/win32/posix.h

diff --git a/src/compat/fnmatch.c b/src/compat/fnmatch.c
deleted file mode 100644 (file)
index 835d811..0000000
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * Copyright (C) 2009-2012 the libgit2 contributors
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-
-/*
- * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
- * Compares a filename or pathname to a pattern.
- */
-
-#include <ctype.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "fnmatch.h"
-
-#define EOS            '\0'
-
-#define RANGE_MATCH            1
-#define RANGE_NOMATCH          0
-#define RANGE_ERROR            (-1)
-
-static int rangematch(const char *, char, int, char **);
-
-int
-p_fnmatch(const char *pattern, const char *string, int flags)
-{
-               const char *stringstart;
-               char *newp;
-               char c, test;
-
-               for (stringstart = string;;)
-                               switch (c = *pattern++) {
-                               case EOS:
-                                               if ((flags & FNM_LEADING_DIR) && *string == '/')
-                                                               return (0);
-                                               return (*string == EOS ? 0 : FNM_NOMATCH);
-                               case '?':
-                                               if (*string == EOS)
-                                                               return (FNM_NOMATCH);
-                                               if (*string == '/' && (flags & FNM_PATHNAME))
-                                                               return (FNM_NOMATCH);
-                                               if (*string == '.' && (flags & FNM_PERIOD) &&
-                                                       (string == stringstart ||
-                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
-                                                               return (FNM_NOMATCH);
-                                               ++string;
-                                               break;
-                               case '*':
-                                               c = *pattern;
-                                               /* Collapse multiple stars. */
-                                               while (c == '*')
-                                                               c = *++pattern;
-
-                                               if (*string == '.' && (flags & FNM_PERIOD) &&
-                                                       (string == stringstart ||
-                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
-                                                               return (FNM_NOMATCH);
-
-                                               /* Optimize for pattern with * at end or before /. */
-                                               if (c == EOS) {
-                                                               if (flags & FNM_PATHNAME)
-                                                                               return ((flags & FNM_LEADING_DIR) ||
-                                                                                       strchr(string, '/') == NULL ?
-                                                                                       0 : FNM_NOMATCH);
-                                                               else
-                                                                               return (0);
-                                               } else if (c == '/' && (flags & FNM_PATHNAME)) {
-                                                               if ((string = strchr(string, '/')) == NULL)
-                                                                               return (FNM_NOMATCH);
-                                                               break;
-                                               }
-
-                                               /* General case, use recursion. */
-                                               while ((test = *string) != EOS) {
-                                                               if (!p_fnmatch(pattern, string, flags & ~FNM_PERIOD))
-                                                                               return (0);
-                                                               if (test == '/' && (flags & FNM_PATHNAME))
-                                                                               break;
-                                                               ++string;
-                                               }
-                                               return (FNM_NOMATCH);
-                               case '[':
-                                               if (*string == EOS)
-                                                               return (FNM_NOMATCH);
-                                               if (*string == '/' && (flags & FNM_PATHNAME))
-                                                               return (FNM_NOMATCH);
-                                               if (*string == '.' && (flags & FNM_PERIOD) &&
-                                                       (string == stringstart ||
-                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
-                                                               return (FNM_NOMATCH);
-
-                                               switch (rangematch(pattern, *string, flags, &newp)) {
-                                               case RANGE_ERROR:
-                                                               /* not a good range, treat as normal text */
-                                                               goto normal;
-                                               case RANGE_MATCH:
-                                                               pattern = newp;
-                                                               break;
-                                               case RANGE_NOMATCH:
-                                                               return (FNM_NOMATCH);
-                                               }
-                                               ++string;
-                                               break;
-                               case '\\':
-                                               if (!(flags & FNM_NOESCAPE)) {
-                                                               if ((c = *pattern++) == EOS) {
-                                                                               c = '\\';
-                                                                               --pattern;
-                                                               }
-                                               }
-                                               /* FALLTHROUGH */
-                               default:
-                               normal:
-                                               if (c != *string && !((flags & FNM_CASEFOLD) &&
-                                                                       (tolower((unsigned char)c) ==
-                                                                       tolower((unsigned char)*string))))
-                                                               return (FNM_NOMATCH);
-                                               ++string;
-                                               break;
-                               }
-               /* NOTREACHED */
-}
-
-static int
-rangematch(const char *pattern, char test, int flags, char **newp)
-{
-               int negate, ok;
-               char c, c2;
-
-               /*
-                       * A bracket expression starting with an unquoted circumflex
-                       * character produces unspecified results (IEEE 1003.2-1992,
-                       * 3.13.2). This implementation treats it like '!', for
-                       * consistency with the regular expression syntax.
-                       * J.T. Conklin (conklin@ngai.kaleida.com)
-                       */
-               if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
-                               ++pattern;
-
-               if (flags & FNM_CASEFOLD)
-                               test = (char)tolower((unsigned char)test);
-
-               /*
-                       * A right bracket shall lose its special meaning and represent
-                       * itself in a bracket expression if it occurs first in the list.
-                       * -- POSIX.2 2.8.3.2
-                       */
-               ok = 0;
-               c = *pattern++;
-               do {
-                               if (c == '\\' && !(flags & FNM_NOESCAPE))
-                                               c = *pattern++;
-                               if (c == EOS)
-                                               return (RANGE_ERROR);
-                               if (c == '/' && (flags & FNM_PATHNAME))
-                                               return (RANGE_NOMATCH);
-                               if ((flags & FNM_CASEFOLD))
-                                               c = (char)tolower((unsigned char)c);
-                               if (*pattern == '-'
-                                       && (c2 = *(pattern+1)) != EOS && c2 != ']') {
-                                               pattern += 2;
-                                               if (c2 == '\\' && !(flags & FNM_NOESCAPE))
-                                                               c2 = *pattern++;
-                                               if (c2 == EOS)
-                                                               return (RANGE_ERROR);
-                                               if (flags & FNM_CASEFOLD)
-                                                               c2 = (char)tolower((unsigned char)c2);
-                                               if (c <= test && test <= c2)
-                                                               ok = 1;
-                               } else if (c == test)
-                                               ok = 1;
-               } while ((c = *pattern++) != ']');
-
-               *newp = (char *)pattern;
-               return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
-}
-
diff --git a/src/compat/fnmatch.h b/src/compat/fnmatch.h
deleted file mode 100644 (file)
index 7faef09..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * Copyright (C) 2009-2012 the libgit2 contributors
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_fnmatch__compat_h__
-#define INCLUDE_fnmatch__compat_h__
-
-#include "common.h"
-
-#define FNM_NOMATCH            1               /* Match failed. */
-#define FNM_NOSYS              2               /* Function not supported (unused). */
-
-#define FNM_NOESCAPE           0x01            /* Disable backslash escaping. */
-#define FNM_PATHNAME           0x02            /* Slash must be matched by slash. */
-#define FNM_PERIOD             0x04            /* Period must be matched by period. */
-#define FNM_LEADING_DIR 0x08           /* Ignore /<tail> after Imatch. */
-#define FNM_CASEFOLD           0x10            /* Case insensitive search. */
-
-#define FNM_IGNORECASE FNM_CASEFOLD
-#define FNM_FILE_NAME  FNM_PATHNAME
-
-extern int p_fnmatch(const char *pattern, const char *string, int flags);
-
-#endif /* _FNMATCH_H */
-
diff --git a/src/fnmatch.c b/src/fnmatch.c
new file mode 100644 (file)
index 0000000..835d811
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+/*
+ * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
+ * Compares a filename or pathname to a pattern.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "fnmatch.h"
+
+#define EOS            '\0'
+
+#define RANGE_MATCH            1
+#define RANGE_NOMATCH          0
+#define RANGE_ERROR            (-1)
+
+static int rangematch(const char *, char, int, char **);
+
+int
+p_fnmatch(const char *pattern, const char *string, int flags)
+{
+               const char *stringstart;
+               char *newp;
+               char c, test;
+
+               for (stringstart = string;;)
+                               switch (c = *pattern++) {
+                               case EOS:
+                                               if ((flags & FNM_LEADING_DIR) && *string == '/')
+                                                               return (0);
+                                               return (*string == EOS ? 0 : FNM_NOMATCH);
+                               case '?':
+                                               if (*string == EOS)
+                                                               return (FNM_NOMATCH);
+                                               if (*string == '/' && (flags & FNM_PATHNAME))
+                                                               return (FNM_NOMATCH);
+                                               if (*string == '.' && (flags & FNM_PERIOD) &&
+                                                       (string == stringstart ||
+                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
+                                                               return (FNM_NOMATCH);
+                                               ++string;
+                                               break;
+                               case '*':
+                                               c = *pattern;
+                                               /* Collapse multiple stars. */
+                                               while (c == '*')
+                                                               c = *++pattern;
+
+                                               if (*string == '.' && (flags & FNM_PERIOD) &&
+                                                       (string == stringstart ||
+                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
+                                                               return (FNM_NOMATCH);
+
+                                               /* Optimize for pattern with * at end or before /. */
+                                               if (c == EOS) {
+                                                               if (flags & FNM_PATHNAME)
+                                                                               return ((flags & FNM_LEADING_DIR) ||
+                                                                                       strchr(string, '/') == NULL ?
+                                                                                       0 : FNM_NOMATCH);
+                                                               else
+                                                                               return (0);
+                                               } else if (c == '/' && (flags & FNM_PATHNAME)) {
+                                                               if ((string = strchr(string, '/')) == NULL)
+                                                                               return (FNM_NOMATCH);
+                                                               break;
+                                               }
+
+                                               /* General case, use recursion. */
+                                               while ((test = *string) != EOS) {
+                                                               if (!p_fnmatch(pattern, string, flags & ~FNM_PERIOD))
+                                                                               return (0);
+                                                               if (test == '/' && (flags & FNM_PATHNAME))
+                                                                               break;
+                                                               ++string;
+                                               }
+                                               return (FNM_NOMATCH);
+                               case '[':
+                                               if (*string == EOS)
+                                                               return (FNM_NOMATCH);
+                                               if (*string == '/' && (flags & FNM_PATHNAME))
+                                                               return (FNM_NOMATCH);
+                                               if (*string == '.' && (flags & FNM_PERIOD) &&
+                                                       (string == stringstart ||
+                                                       ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
+                                                               return (FNM_NOMATCH);
+
+                                               switch (rangematch(pattern, *string, flags, &newp)) {
+                                               case RANGE_ERROR:
+                                                               /* not a good range, treat as normal text */
+                                                               goto normal;
+                                               case RANGE_MATCH:
+                                                               pattern = newp;
+                                                               break;
+                                               case RANGE_NOMATCH:
+                                                               return (FNM_NOMATCH);
+                                               }
+                                               ++string;
+                                               break;
+                               case '\\':
+                                               if (!(flags & FNM_NOESCAPE)) {
+                                                               if ((c = *pattern++) == EOS) {
+                                                                               c = '\\';
+                                                                               --pattern;
+                                                               }
+                                               }
+                                               /* FALLTHROUGH */
+                               default:
+                               normal:
+                                               if (c != *string && !((flags & FNM_CASEFOLD) &&
+                                                                       (tolower((unsigned char)c) ==
+                                                                       tolower((unsigned char)*string))))
+                                                               return (FNM_NOMATCH);
+                                               ++string;
+                                               break;
+                               }
+               /* NOTREACHED */
+}
+
+static int
+rangematch(const char *pattern, char test, int flags, char **newp)
+{
+               int negate, ok;
+               char c, c2;
+
+               /*
+                       * A bracket expression starting with an unquoted circumflex
+                       * character produces unspecified results (IEEE 1003.2-1992,
+                       * 3.13.2). This implementation treats it like '!', for
+                       * consistency with the regular expression syntax.
+                       * J.T. Conklin (conklin@ngai.kaleida.com)
+                       */
+               if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
+                               ++pattern;
+
+               if (flags & FNM_CASEFOLD)
+                               test = (char)tolower((unsigned char)test);
+
+               /*
+                       * A right bracket shall lose its special meaning and represent
+                       * itself in a bracket expression if it occurs first in the list.
+                       * -- POSIX.2 2.8.3.2
+                       */
+               ok = 0;
+               c = *pattern++;
+               do {
+                               if (c == '\\' && !(flags & FNM_NOESCAPE))
+                                               c = *pattern++;
+                               if (c == EOS)
+                                               return (RANGE_ERROR);
+                               if (c == '/' && (flags & FNM_PATHNAME))
+                                               return (RANGE_NOMATCH);
+                               if ((flags & FNM_CASEFOLD))
+                                               c = (char)tolower((unsigned char)c);
+                               if (*pattern == '-'
+                                       && (c2 = *(pattern+1)) != EOS && c2 != ']') {
+                                               pattern += 2;
+                                               if (c2 == '\\' && !(flags & FNM_NOESCAPE))
+                                                               c2 = *pattern++;
+                                               if (c2 == EOS)
+                                                               return (RANGE_ERROR);
+                                               if (flags & FNM_CASEFOLD)
+                                                               c2 = (char)tolower((unsigned char)c2);
+                                               if (c <= test && test <= c2)
+                                                               ok = 1;
+                               } else if (c == test)
+                                               ok = 1;
+               } while ((c = *pattern++) != ']');
+
+               *newp = (char *)pattern;
+               return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
+}
+
diff --git a/src/fnmatch.h b/src/fnmatch.h
new file mode 100644 (file)
index 0000000..7faef09
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_fnmatch__compat_h__
+#define INCLUDE_fnmatch__compat_h__
+
+#include "common.h"
+
+#define FNM_NOMATCH            1               /* Match failed. */
+#define FNM_NOSYS              2               /* Function not supported (unused). */
+
+#define FNM_NOESCAPE           0x01            /* Disable backslash escaping. */
+#define FNM_PATHNAME           0x02            /* Slash must be matched by slash. */
+#define FNM_PERIOD             0x04            /* Period must be matched by period. */
+#define FNM_LEADING_DIR 0x08           /* Ignore /<tail> after Imatch. */
+#define FNM_CASEFOLD           0x10            /* Case insensitive search. */
+
+#define FNM_IGNORECASE FNM_CASEFOLD
+#define FNM_FILE_NAME  FNM_PATHNAME
+
+extern int p_fnmatch(const char *pattern, const char *string, int flags);
+
+#endif /* _FNMATCH_H */
+
index 71bb8228308aca20f3f18bdbd4c2d97d54ceebc1..d565dc11f0b88d86ecc9e9d76b93cc28ad2ebd91 100644 (file)
@@ -10,6 +10,7 @@
 #include "common.h"
 #include <fcntl.h>
 #include <time.h>
+#include "fnmatch.h"
 
 #ifndef S_IFGITLINK
 #define S_IFGITLINK 0160000
index 25038c82736b747d43e2f51acc08d735924af8ed..bcd8003019fc9e5e21195d22ac2eb85092fa07f0 100644 (file)
@@ -7,13 +7,6 @@
 #ifndef INCLUDE_posix__w32_h__
 #define INCLUDE_posix__w32_h__
 
-#if !defined(__sun) && !defined(__amigaos4__)
-#      include <fnmatch.h>
-#      define p_fnmatch(p, s, f) fnmatch(p, s, f)
-#else
-#      include "compat/fnmatch.h"
-#endif
-
 #include <stdio.h>
 
 #define p_lstat(p,b) lstat(p,b)
index da46cf514ed727db6d440946233fc9d8ec09e64e..80dcca5c1bde56d02a3c8a9b53e065a014e2eefa 100644 (file)
@@ -8,7 +8,6 @@
 #define INCLUDE_posix__w32_h__
 
 #include "common.h"
-#include "compat/fnmatch.h"
 #include "utf-conv.h"
 
 GIT_INLINE(int) p_link(const char *old, const char *new)