tech-userlevel archive

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

Re: glob(3) GLOB_PERIOD bug?



In article <201302200214.VAA13476%Sparkle.Rodents-Montreal.ORG@localhost>,
Mouse  <mouse%Rodents-Montreal.ORG@localhost> wrote:
>It looks to me as though there's a bug in glob(3)'s GLOB_PERIOD.  When

Perhaps the man page is not clear; with GLOB_PERIOD a leading period in
a filename can be matched with a globbing character, without it does not.

Try this:

#include <sys/param.h>
#include <glob.h>
#include <stdio.h>
#include <errno.h>
#include <strings.h>
#include <stdlib.h>

static int 
gerr(const char *s, int e)
{
        printf("%s: path=%s err=%d (%s)\n", __func__, s, e, strerror(e));
        return 0;
}

static void 
trywith(int f)
{
        int             rv;
        glob_t          g;
        int             i;
        const char      *h;
        char            gpath[MAXPATHLEN];

        h = getenv("HOME");
        h = h ? h : ".";
        snprintf(gpath, sizeof(gpath), "%s/?*", h);
        rv = glob(gpath, f, gerr, &g);
        switch (rv) {
        case GLOB_ABORTED:
                printf("  GLOB_ABORTED\n");
                break;
        case GLOB_NOMATCH:
                printf("  GLOB_NOMATCH\n");
                break;
        case GLOB_NOSPACE:
                printf("  GLOB_NOSPACE\n");
                break;
        default:
                printf("  rv = %d?\n", rv);
                break;
        case 0:
                printf("  pathc=%d matchc=%d\n",
                    (int) g.gl_pathc, (int) g.gl_matchc);
                for (i = 0; i < g.gl_pathc; i++) {
                        printf("    [%d] %s\n", i, g.gl_pathv[i]);
                }
                break;
        }
}

int 
main(void)
{
        printf("flags = 0:\n");
        trywith(0);
        printf("flags = GLOB_PERIOD:\n");
        trywith(GLOB_PERIOD);
        return 0;
}



Home | Main Index | Thread Index | Old Index