Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Aug 2010 00:35:38 -0700
From:      Garrett Cooper <yanegomi@gmail.com>
To:        standards@freebsd.org
Subject:   Confusion over wording in glob(3)
Message-ID:  <AANLkTinoKOxBxQ0NmXyXvHuTeNQ9Nx=HGgGwb8pAK-xF@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
    I have some question about the ambiguity of the ERRORS section in
our glob(3) manpage.

POSIX states:

ERRORS

    The glob() function shall fail and return the corresponding value if:

    GLOB_ABORTED
        The scan was stopped because GLOB_ERR was set or (*errfunc())
returned non-zero.
    GLOB_NOMATCH
        The pattern does not match any existing pathname, and
GLOB_NOCHECK was not set in flags.
    GLOB_NOSPACE
        An attempt to allocate memory failed.

(Note that there's no mention of `errno').
Our manpage states:

     If glob() terminates due to an error, it sets errno and returns one of
     the following non-zero constants, which are defined in the include file
     <glob.h>:

     GLOB_NOSPACE  An attempt to allocate memory failed, or if errno was 0
                   GLOB_LIMIT was specified in the flags and pglob->gl_matchc
                   or more patterns were matched.

     GLOB_ABORTED  The scan was stopped because an error was encountered and
                   either GLOB_ERR was set or (*errfunc)() returned non-zero.

     GLOB_NOMATCH  The pattern did not match a pathname and GLOB_NOCHECK was
                   not set.

(Note the mention of errno).
So far I've only been able to hit a sensical error case once by doing
the following (but that could have been a side-effect from a malloc(3)
failure in terms of finding malloc.conf -- don't know for sure). The
rest of the time I get errno = 0:

$ cc -o test_glob test_glob.c
$ ln -f test_glob test_glob_nomatch
$ ./test_glob_nomatch
NOMATCH
glob(./test_glob_nomatch.*) didn't match: 0: Unknown error: 0

So I suppose my question is: should the confusing wording be removed
for clarity?

Thanks,
-Garrett

/* test_glob.c */
#include <err.h>
#include <errno.h>
#include <glob.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, char **argv)
{
	char p[PATH_MAX];
	glob_t globp;
	int err, ret;

	sprintf(p, "%s.*", *argv);

	printf("glob(%s) ", p);
	ret = glob(p, GLOB_NOESCAPE, NULL, &globp);
	err = errno;
	if (ret == 0) {
		if (globp.gl_pathc)
			printf("matches !\n");
		else
			errx(1, "what what...?\n");
	} else {
		switch (ret) {
		case GLOB_NOSPACE:
			fprintf(stderr, "NOSPACE\n");
			break;
		case GLOB_ABORTED:
			fprintf(stderr, "ABORTED\n");
			break;
		case GLOB_NOMATCH:
			fprintf(stderr, "NOMATCH\n");
			break;
		default:
			fprintf(stderr, "unknown: %d\n", ret);
		}
		printf("didn't match: %d: %s\n", err, strerror(err));
	}

	globfree(&globp);
	return (ret);
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTinoKOxBxQ0NmXyXvHuTeNQ9Nx=HGgGwb8pAK-xF>