* tests/test-glob.c (main): Enhance test. * doc/posix-functions/glob.texi (glob): Document the fix.
Signed-off-by: Eric Blake <ebl...@redhat.com> --- ChangeLog | 6 +++ doc/posix-functions/glob.texi | 4 ++ tests/test-glob.c | 78 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 43386c8..1aad7f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2010-03-22 Eric Blake <ebl...@redhat.com> + + glob: test previous patch + * tests/test-glob.c (main): Enhance test. + * doc/posix-functions/glob.texi (glob): Document the fix. + 2010-03-22 Andreas Schwab <sch...@redhat.com> glob: fix empty pattern and patterns ending in slash diff --git a/doc/posix-functions/glob.texi b/doc/posix-functions/glob.texi index 3f73489..eab26d1 100644 --- a/doc/posix-functions/glob.texi +++ b/doc/posix-functions/glob.texi @@ -14,6 +14,10 @@ glob @item This function may list symbolic links to nonexistent files among the results, on some platforms. +...@item +On some platforms, this function fails to recognize that the empty +pattern cannot match any files: +glibc 2.11. @end itemize Portability problems not fixed by Gnulib: diff --git a/tests/test-glob.c b/tests/test-glob.c index ab2d531..93f6b2b 100644 --- a/tests/test-glob.c +++ b/tests/test-glob.c @@ -25,12 +25,34 @@ SIGNATURE_CHECK (glob, int, (char const *, int, int (*) (char const *, int), glob_t *)); SIGNATURE_CHECK (globfree, void, (glob_t *)); +#include <errno.h> #include <string.h> #include "macros.h" #define GL_NO_SUCH_FILE "/gnulib-magic-does-not-exist" +#define STREQ(a, b) (strcmp (a, b) == 0) + +static int iteration; +static int +errfunc (char const *name, int error) +{ + switch (iteration) + { + case 1: + ASSERT (STREQ (name, GL_NO_SUCH_FILE)); + ASSERT (error == ENOENT); + return 0; + case 2: + ASSERT (STREQ (name, GL_NO_SUCH_FILE)); + ASSERT (error == ENOENT); + return 1; + default: + ASSERT (0); + } +} + int main () { @@ -44,20 +66,46 @@ main () res = glob (".", 0, NULL, &g); ASSERT (res == 0 && g.gl_pathc == 1); + ASSERT (STREQ (g.gl_pathv[0], ".")); + ASSERT (g.gl_pathv[1] == NULL); globfree (&g); res = glob (".", GLOB_NOSORT, NULL, &g); ASSERT (res == 0 && g.gl_pathc == 1); + ASSERT (STREQ (g.gl_pathv[0], ".")); + ASSERT (g.gl_pathv[1] == NULL); globfree (&g); res = glob (".", GLOB_MARK, NULL, &g); ASSERT (res == 0 && g.gl_pathc == 1); + ASSERT (STREQ (g.gl_pathv[0], "./")); + ASSERT (g.gl_pathv[1] == NULL); res = glob (".", GLOB_APPEND, NULL, &g); ASSERT (res == 0 && g.gl_pathc == 2); + ASSERT (STREQ (g.gl_pathv[0], "./")); + ASSERT (STREQ (g.gl_pathv[1], ".")); + ASSERT (g.gl_pathv[2] == NULL); + globfree (&g); + + ASSERT (g.gl_offs == 0); + g.gl_offs = 1; + res = glob (".", GLOB_DOOFFS, NULL, &g); + ASSERT (res == 0 && g.gl_pathc == 1); + ASSERT (g.gl_pathv[0] == NULL); + ASSERT (STREQ (g.gl_pathv[1], ".")); + ASSERT (g.gl_pathv[2] == NULL); + + res = glob (".", GLOB_APPEND | GLOB_DOOFFS | GLOB_MARK, NULL, &g); + ASSERT (res == 0 && g.gl_pathc == 2); + ASSERT (g.gl_pathv[0] == NULL); + ASSERT (STREQ (g.gl_pathv[1], ".")); + ASSERT (STREQ (g.gl_pathv[2], "./")); + ASSERT (g.gl_pathv[3] == NULL); globfree (&g); + g.gl_offs = 0; - res = glob (".", GLOB_NOESCAPE, NULL, &g); + res = glob (".", GLOB_NOESCAPE, errfunc, &g); ASSERT (res == 0 && g.gl_pathc == 1); globfree (&g); @@ -66,11 +114,33 @@ main () globfree (&g); res = glob (GL_NO_SUCH_FILE, 0, NULL, &g); - ASSERT (res == GLOB_NOMATCH); + ASSERT (res == GLOB_NOMATCH && g.gl_pathc == 0); + globfree (&g); + + iteration = 1; + res = glob (GL_NO_SUCH_FILE "/*", GLOB_NOCHECK, errfunc, &g); + ASSERT (res == 0 && g.gl_pathc == 1); + ASSERT (STREQ (g.gl_pathv[0], GL_NO_SUCH_FILE "/*")); + globfree (&g); + + iteration = 2; + res = glob (GL_NO_SUCH_FILE "/*", 0, errfunc, &g); + ASSERT (res == GLOB_ABORTED && g.gl_pathc == 0); + globfree (&g); + + iteration = 3; + res = glob (GL_NO_SUCH_FILE "/*", GLOB_ERR, NULL, &g); + ASSERT (res == GLOB_ABORTED && g.gl_pathc == 0); + globfree (&g); + + /* The empty pattern cannot match. */ + res = glob ("", 0, NULL, &g); + ASSERT (res == GLOB_NOMATCH && g.gl_pathc == 0); + globfree (&g); - res = glob (GL_NO_SUCH_FILE, GLOB_NOCHECK, NULL, &g); + res = glob ("", GLOB_NOCHECK, NULL, &g); ASSERT (res == 0 && g.gl_pathc == 1); - ASSERT (strcmp (g.gl_pathv[0], GL_NO_SUCH_FILE) == 0); + ASSERT (STREQ (g.gl_pathv[0], "")); globfree (&g); return 0; -- 1.6.6.1