lib/util: Move unix_wild_match() from source3/lib/util to lib/util/
authorJeremy Allison <jra@samba.org>
Fri, 11 Nov 2016 18:35:01 +0000 (10:35 -0800)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 16 Nov 2016 11:41:09 +0000 (12:41 +0100)
Use top-level functions instead of source3 specific ones.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=12419

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/util/unix_match.c [new file with mode: 0644]
lib/util/unix_match.h [new file with mode: 0644]
lib/util/wscript_build
source3/include/proto.h
source3/lib/util.c

diff --git a/lib/util/unix_match.c b/lib/util/unix_match.c
new file mode 100644 (file)
index 0000000..38edc18
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+   Unix SMB/CIFS implementation.
+   Samba utility functions
+   Copyright (C) Jeremy Allison       2001
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include <talloc.h>
+#include "lib/util/talloc_stack.h"
+#include "lib/util/charset/charset.h"
+#include "lib/util/unix_match.h"
+
+/*********************************************************
+ Recursive routine that is called by unix_wild_match.
+*********************************************************/
+
+static bool unix_do_match(const char *regexp, const char *str)
+{
+       const char *p;
+
+       for( p = regexp; *p && *str; ) {
+
+               switch(*p) {
+                       case '?':
+                               str++;
+                               p++;
+                               break;
+
+                       case '*':
+
+                               /*
+                                * Look for a character matching
+                                * the one after the '*'.
+                                */
+                               p++;
+                               if(!*p) {
+                                       return true; /* Automatic match */
+                               }
+                               while(*str) {
+
+                                       while(*str && (*p != *str)) {
+                                               str++;
+                                       }
+
+                                       /*
+                                        * Patch from weidel@multichart.de.
+                                        * In the case of the regexp
+                                        * '*XX*' we want to ensure there are
+                                        * at least 2 'X' characters in the
+                                        * string after the '*' for a match to
+                                        * be made.
+                                        */
+
+                                       {
+                                               int matchcount=0;
+
+                                               /*
+                                                * Eat all the characters that
+                                                * match, but count how many
+                                                * there were.
+                                                */
+
+                                               while(*str && (*p == *str)) {
+                                                       str++;
+                                                       matchcount++;
+                                               }
+
+                                               /*
+                                                * Now check that if the regexp
+                                                * had n identical characters
+                                                * that matchcount had at least
+                                                * that many matches.
+                                                */
+
+                                               while (*(p+1) && (*(p+1)==*p)) {
+                                                       p++;
+                                                       matchcount--;
+                                               }
+
+                                               if ( matchcount <= 0 ) {
+                                                       return false;
+                                               }
+                                       }
+
+                                       /*
+                                        * We've eaten the match char
+                                        * after the '*'
+                                        */
+                                       str--;
+
+                                       if(unix_do_match(p, str)) {
+                                               return true;
+                                       }
+
+                                       if(!*str) {
+                                               return false;
+                                       } else {
+                                               str++;
+                                       }
+                               }
+                               return false;
+
+                       default:
+                               if(*str != *p) {
+                                       return false;
+                               }
+                               str++;
+                               p++;
+                               break;
+               }
+       }
+
+       if(!*p && !*str) {
+               return true;
+       }
+
+       if (!*p && str[0] == '.' && str[1] == 0) {
+               return true;
+       }
+
+       if (!*str && *p == '?') {
+               while (*p == '?') {
+                       p++;
+               }
+               return(!*p);
+       }
+
+       if(!*str && (*p == '*' && p[1] == '\0')) {
+               return true;
+       }
+
+       return false;
+}
+
+/*******************************************************************
+ Simple case insensitive interface to a UNIX wildcard matcher.
+ Returns True if match, False if not.
+*******************************************************************/
+
+bool unix_wild_match(const char *pattern, const char *string)
+{
+       TALLOC_CTX *ctx = talloc_stackframe();
+       char *p2;
+       char *s2;
+       char *p;
+       bool ret = false;
+
+       p2 = strlower_talloc(ctx, pattern);
+       s2 = strlower_talloc(ctx, string);
+       if (!p2 || !s2) {
+               TALLOC_FREE(ctx);
+               return false;
+       }
+
+       /* Remove any *? and ** from the pattern as they are meaningless */
+       for(p = p2; *p; p++) {
+               while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
+                       memmove(&p[1], &p[2], strlen(&p[2])+1);
+               }
+       }
+
+       if (p2[0] == '*' && p2[1] == '\0') {
+               TALLOC_FREE(ctx);
+               return true;
+       }
+
+       ret = unix_do_match(p2, s2);
+       TALLOC_FREE(ctx);
+       return ret;
+}
diff --git a/lib/util/unix_match.h b/lib/util/unix_match.h
new file mode 100644 (file)
index 0000000..a7b6935
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+   Unix SMB/CIFS implementation.
+   Utility functions for Samba
+   Copyright (C) Jeremy Allison 2001
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _UNIX_MASK_H_
+#define _UNIX_MASK_H_
+
+bool unix_wild_match(const char *pattern, const char *string);
+
+#endif
index 6d2ab4ac27f201d6e7cbdd497d70246469afda93..e2ae4110647d3546d03dc5f8ed76d2c5c64d105f 100755 (executable)
@@ -120,7 +120,7 @@ else:
                     idtree_random.c base64.c
                     util_str.c util_str_common.c ms_fnmatch.c
                     server_id.c dprintf.c bitmap.c pidfile.c
-                    tevent_debug.c memcache.c''',
+                    tevent_debug.c memcache.c unix_match.c''',
                   deps='samba-util-core DYNCONFIG close-low-fd tini tiniparser genrand',
                   public_deps='talloc tevent execinfo pthread LIBCRYPTO charset util_setid systemd systemd-daemon',
                   public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h string_wrappers.h idtree.h idtree_random.h blocking.h signal.h substitute.h fault.h genrand.h',
index 0b0a2b593258448721c9146a0d740272a85bfdef..2758dc5a47b60a3515cf500919af23ee222e57ad 100644 (file)
@@ -411,7 +411,7 @@ bool ms_has_wild_w(const smb_ucs2_t *s);
 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive);
 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive);
 bool mask_match_list(const char *string, char **list, int listLen, bool is_case_sensitive);
-bool unix_wild_match(const char *pattern, const char *string);
+#include "lib/util/unix_match.h"
 bool name_to_fqdn(fstring fqdn, const char *name);
 uint32_t map_share_mode_to_deny_mode(uint32_t share_access, uint32_t private_options);
 
index 7cb613011aa466ce13cf5a24c2ad807bead1e619..85cb9b396876f310756d9b5fec22079c0ac2cffc 100644 (file)
@@ -1785,165 +1785,6 @@ bool mask_match_list(const char *string, char **list, int listLen, bool is_case_
        return False;
 }
 
-/*********************************************************
- Recursive routine that is called by unix_wild_match.
-*********************************************************/
-
-static bool unix_do_match(const char *regexp, const char *str)
-{
-       const char *p;
-
-       for( p = regexp; *p && *str; ) {
-
-               switch(*p) {
-                       case '?':
-                               str++;
-                               p++;
-                               break;
-
-                       case '*':
-
-                               /*
-                                * Look for a character matching
-                                * the one after the '*'.
-                                */
-                               p++;
-                               if(!*p) {
-                                       return true; /* Automatic match */
-                               }
-                               while(*str) {
-
-                                       while(*str && (*p != *str)) {
-                                               str++;
-                                       }
-
-                                       /*
-                                        * Patch from weidel@multichart.de.
-                                        * In the case of the regexp
-                                        * '*XX*' we want to ensure there are
-                                        * at least 2 'X' characters in the
-                                        * string after the '*' for a match to
-                                        * be made.
-                                        */
-
-                                       {
-                                               int matchcount=0;
-
-                                               /*
-                                                * Eat all the characters that
-                                                * match, but count how many
-                                                * there were.
-                                                */
-
-                                               while(*str && (*p == *str)) {
-                                                       str++;
-                                                       matchcount++;
-                                               }
-
-                                               /*
-                                                * Now check that if the regexp
-                                                * had n identical characters
-                                                * that matchcount had at least
-                                                * that many matches.
-                                                */
-
-                                               while (*(p+1) && (*(p+1)==*p)) {
-                                                       p++;
-                                                       matchcount--;
-                                               }
-
-                                               if ( matchcount <= 0 ) {
-                                                       return false;
-                                               }
-                                       }
-
-                                       /*
-                                        * We've eaten the match char
-                                        * after the '*'
-                                        */
-                                       str--;
-
-                                       if(unix_do_match(p, str)) {
-                                               return true;
-                                       }
-
-                                       if(!*str) {
-                                               return false;
-                                       } else {
-                                               str++;
-                                       }
-                               }
-                               return false;
-
-                       default:
-                               if(*str != *p) {
-                                       return false;
-                               }
-                               str++;
-                               p++;
-                               break;
-               }
-       }
-
-       if(!*p && !*str) {
-               return true;
-       }
-
-       if (!*p && str[0] == '.' && str[1] == 0) {
-               return true;
-       }
-
-       if (!*str && *p == '?') {
-               while (*p == '?') {
-                       p++;
-               }
-               return(!*p);
-       }
-
-       if(!*str && (*p == '*' && p[1] == '\0')) {
-               return true;
-       }
-
-       return false;
-}
-
-/*******************************************************************
- Simple case insensitive interface to a UNIX wildcard matcher.
- Returns True if match, False if not.
-*******************************************************************/
-
-bool unix_wild_match(const char *pattern, const char *string)
-{
-       TALLOC_CTX *ctx = talloc_stackframe();
-       char *p2;
-       char *s2;
-       char *p;
-       bool ret = false;
-
-       p2 = strlower_talloc(ctx, pattern);
-       s2 = strlower_talloc(ctx, string);
-       if (!p2 || !s2) {
-               TALLOC_FREE(ctx);
-               return false;
-       }
-
-       /* Remove any *? and ** from the pattern as they are meaningless */
-       for(p = p2; *p; p++) {
-               while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
-                       memmove(&p[1], &p[2], strlen(&p[2])+1);
-               }
-       }
-
-       if (p2[0] == '*' && p2[1] == '\0') {
-               TALLOC_FREE(ctx);
-               return true;
-       }
-
-       ret = unix_do_match(p2, s2);
-       TALLOC_FREE(ctx);
-       return ret;
-}
-
 /**********************************************************************
   Converts a name to a fully qualified domain name.
   Returns true if lookup succeeded, false if not (then fqdn is set to name)