s3-clitar: Simplify is_subpath().
authorAndreas Schneider <asn@samba.org>
Mon, 17 Feb 2014 10:24:33 +0000 (11:24 +0100)
committerAndreas Schneider <asn@samba.org>
Wed, 19 Feb 2014 17:22:30 +0000 (18:22 +0100)
Signed-off-by: Andreas Schneider <asn@samba.org>
Signed-off-by: David Disseldorp <ddiss@samba.org>
source3/client/clitar.c

index bad7eac4ad85a79efd74df20fc5fb7cc3ab4896f..0f6b6a80522a12f61e5a0f4e07987aad6249936e 100644 (file)
@@ -1433,29 +1433,42 @@ static const char* skip_useless_char_in_path(const char *p)
  */
 static bool is_subpath(const char *sub, const char *full)
 {
-    const char *full_copy = full;
-
-    while (*full && *sub &&
-           (*full == *sub || tolower_m(*full) == tolower_m(*sub) ||
-            (*full == '\\' && *sub=='/') || (*full == '/' && *sub=='\\'))) {
-        full++; sub++;
-    }
-
-    /* if full has a trailing slash, it compared equal, so full is an "initial"
-       string of sub.
-    */
-    if (!*full && full != full_copy && (*(full-1) == '/' || *(full-1) == '\\'))
-        return true;
-
-    /* ignore trailing slash on full */
-    if (!*sub && (*full == '/' || *full == '\\') && !*(full+1))
-        return true;
-
-    /* check for full is an "initial" string of sub */
-    if ((*sub == '/' || *sub == '\\') && !*full)
-        return true;
-
-    return *full == *sub;
+       TALLOC_CTX *tmp_ctx = PANIC_IF_NULL(talloc_new(NULL));
+       int len = 0;
+       char *f, *s;
+
+       f = PANIC_IF_NULL(strlower_talloc(tmp_ctx, full));
+       string_replace(f, '\\', '/');
+       s = PANIC_IF_NULL(strlower_talloc(tmp_ctx, sub));
+       string_replace(s, '\\', '/');
+
+       /* find the point where sub and full diverge */
+       while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
+               f++;
+               s++;
+               len++;
+       }
+
+       if ((*f == '\0') && (*s == '\0')) {
+               return true;    /* sub and full match */
+       }
+
+       if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
+               /* sub diverges from full at path separator */
+               return true;
+       }
+
+       if ((*s == '\0') && (strcmp(f, "/") == 0)) {
+               /* full diverges from sub with trailing slash only */
+               return true;
+       }
+
+       if ((*s == '/') && (*f == '\0')) {
+               /* sub diverges from full with extra path component */
+               return true;
+       }
+
+       return false;
 }
 
 /**