diff options
author | Werner Koch <wk@gnupg.org> | 2007-08-02 20:12:43 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2007-08-02 20:12:43 +0200 |
commit | ebd36b634450ce9fdf0104052cca2b035ad3a22d (patch) | |
tree | 1708507fee17d3520460bd94636800d4b828c8ed /jnlib | |
parent | Applied exact length hack. (diff) | |
download | gnupg2-ebd36b634450ce9fdf0104052cca2b035ad3a22d.tar.xz gnupg2-ebd36b634450ce9fdf0104052cca2b035ad3a22d.zip |
Factored common gpgconf constants out
Fixed W32 compare_filenames
Diffstat (limited to 'jnlib')
-rw-r--r-- | jnlib/ChangeLog | 8 | ||||
-rw-r--r-- | jnlib/stringhelp.c | 85 | ||||
-rw-r--r-- | jnlib/t-stringhelp.c | 37 |
3 files changed, 100 insertions, 30 deletions
diff --git a/jnlib/ChangeLog b/jnlib/ChangeLog index 947e7ebd6..4fec67999 100644 --- a/jnlib/ChangeLog +++ b/jnlib/ChangeLog @@ -1,3 +1,11 @@ +2007-08-02 Werner Koch <wk@g10code.com> + + * t-stringhelp.c (test_compare_filenames): New. + + * stringhelp.c (compare_filenames) [HAVE_DRIVE_LETTERS]: Fixed + comparison to take slash and backslash in account. + (make_filename): Avoid mixing / and \. + 2007-07-04 Werner Koch <wk@g10code.com> * utf8conv.c (load_libiconv): Remove URL from translatble string. diff --git a/jnlib/stringhelp.c b/jnlib/stringhelp.c index c1c1e451b..b1f6f73db 100644 --- a/jnlib/stringhelp.c +++ b/jnlib/stringhelp.c @@ -271,10 +271,10 @@ make_dirname(const char *filepath) char *p; if ( !(p=strrchr(filepath, '/')) ) - #ifdef HAVE_DRIVE_LETTERS +#ifdef HAVE_DRIVE_LETTERS if ( !(p=strrchr(filepath, '\\')) ) if ( !(p=strrchr(filepath, ':')) ) - #endif +#endif { return jnlib_xstrdup("."); } @@ -296,42 +296,67 @@ make_dirname(const char *filepath) char * make_filename( const char *first_part, ... ) { - va_list arg_ptr ; - size_t n; - const char *s; - char *name, *home, *p; - - va_start( arg_ptr, first_part ) ; - n = strlen(first_part)+1; - while( (s=va_arg(arg_ptr, const char *)) ) - n += strlen(s) + 1; - va_end(arg_ptr); - - home = NULL; - if( *first_part == '~' && first_part[1] == '/' - && (home = getenv("HOME")) && *home ) - n += strlen(home); + va_list arg_ptr ; + size_t n; + const char *s; + char *name, *home, *p; + + va_start (arg_ptr, first_part); + n = strlen (first_part) + 1; + while ( (s = va_arg (arg_ptr, const char *)) ) + n += strlen(s) + 1; + va_end(arg_ptr); + + home = NULL; + if ( *first_part == '~' && first_part[1] == '/' + && (home = getenv("HOME")) && *home ) + n += strlen (home); + + name = jnlib_xmalloc (n); + p = (home + ? stpcpy (stpcpy (name,home), first_part + 1) + : stpcpy(name, first_part)); - name = jnlib_xmalloc(n); - p = home ? stpcpy(stpcpy(name,home), first_part+1) - : stpcpy(name, first_part); - va_start( arg_ptr, first_part ) ; - while( (s=va_arg(arg_ptr, const char *)) ) - p = stpcpy(stpcpy(p,"/"), s); - va_end(arg_ptr); + va_start (arg_ptr, first_part) ; + while ( (s = va_arg(arg_ptr, const char *)) ) + p = stpcpy (stpcpy (p,"/"), s); + va_end(arg_ptr); - return name; +#ifdef HAVE_DRIVE_LETTERS + /* We better avoid mixing slashes and backslashes and prefer + backslashes. There is usual no problem with mixing them, however + a very few W32 API calls can't grok plain slashes. Printing + filenames with mixed slashes also looks a bit strange. */ + if (strchr (name, '\\')) + { + for (p=name; *p; p++) + if (*p == '/') + *p = '\\'; + } +#endif /*HAVE_DRIVE_LETTERS*/ + return name; } int -compare_filenames( const char *a, const char *b ) +compare_filenames (const char *a, const char *b) { - /* ? check whether this is an absolute filename and - * resolve symlinks? - */ + /* ? check whether this is an absolute filename and resolve + symlinks? */ #ifdef HAVE_DRIVE_LETTERS - return stricmp(a,b); + for ( ; *a && *b; a++, b++ ) + { + if (*a != *b + && (toupper (*(const unsigned char*)a) + != toupper (*(const unsigned char*)b) ) + && !((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/'))) + break; + } + if ((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/')) + return 0; + else + return (toupper (*(const unsigned char*)a) + - toupper (*(const unsigned char*)b)); #else return strcmp(a,b); #endif diff --git a/jnlib/t-stringhelp.c b/jnlib/t-stringhelp.c index 89ba643bf..12331d241 100644 --- a/jnlib/t-stringhelp.c +++ b/jnlib/t-stringhelp.c @@ -80,12 +80,49 @@ test_percent_escape (void) } +static void +test_compare_filenames (void) +{ + struct { + const char *a; + const char *b; + int result; + } tests[] = { + { "", "", 0 }, + { "", "a", -1 }, + { "a", "", 1 }, + { "a", "a", 0 }, + { "a", "aa", -1 }, + { "aa", "a", 1 }, + { "a", "b", -1 }, + +#ifdef HAVE_W32_SYSTEM + { "a", "A", 0 }, + { "A", "a", 0 }, + { "foo/bar", "foo\\bar", 0 }, + { "foo\\bar", "foo/bar", 0 }, + { "foo\\", "foo/", 0 }, + { "foo/", "foo\\", 0 }, +#endif /*HAVE_W32_SYSTEM*/ + { NULL, NULL, 0} + }; + int testno, result; + + for (testno=0; tests[testno].a; testno++) + { + result = compare_filenames (tests[testno].a, tests[testno].b); + result = result < 0? -1 : result > 0? 1 : 0; + if (result != tests[testno].result) + fail (testno); + } +} int main (int argc, char **argv) { test_percent_escape (); + test_compare_filenames (); return 0; } |