[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: $(wildcard x*/) returns non-directories?



# [1]
forwarded 496615 http://sourceware.org/bugzilla/show_bug.cgi?id=10278
reassign 496615 libc6
severity 496615 normal
tags 496615 + upstream
found 496615 eglibc/2.13-0exp1
affects 496615 + make
quit

Hi,

Trent W. Buck wrote:

> In the transcript below, I test globbing in both the shell and in GNU
> Make's $(wildcard) function.  It seems to me that while * and */
> behave correctly, but ?*/ should NOT list the file y.

Thanks for a clear report.  Thrillingly, this appears to be a bug
in libc's glob(3) function.  As Paul Smith tells us[1]:

	The POSIX spec doesn't specify exactly what should happen with
	a pattern ending in "/"; all it says about slashes in patterns
	is:

		The slash character in a pathname shall be
		explicitly matched by using one or more slashes
		in the pattern; it shall neither be matched by
		the asterisk or question-mark special characters
		nor by a bracket expression.

	All shells I'm aware of interpret this to mean that if your
	glob expression ends with a "/", it should match only
	directories.  GNU glob() handles this correctly for simple
	globbing expressions like "*/", but for other expressions like
	"dir/*/", "/*/", etc. it returns all directories with a
	trailing slash, AND all files without a trailing slash.

Test case:
-- 8< --
#if 0
: 'Usage: ./testcase.c'
: 'Should list directories, but if libc is buggy it can list files, too.'
gcc "$0" && exec ./a.out
#else
#include <stdio.h>
#include <stdlib.h>
#include <glob.h>

static void die_errno(const char *s)
{
	perror(s);
	exit(EXIT_FAILURE);
}

int main(void)
{
	glob_t globbuf;
	int i;

	if (glob("?*/", 0, NULL, &globbuf))
		die_errno("glob");
	for (i = 0; i < globbuf.gl_pathc; i++)
		puts(globbuf.gl_pathv[i]);
	return 0;
}
#endif


Reply to: