]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
enumerator: Enumerate glob(3) matches using gl_pathc
authorMartin Willi <martin@revosec.ch>
Mon, 7 Jul 2014 13:27:19 +0000 (15:27 +0200)
committerMartin Willi <martin@revosec.ch>
Mon, 7 Jul 2014 14:14:17 +0000 (16:14 +0200)
While glob should return a NULL terminated gl_pathv when having no matches,
at least on OS X this is not true when using GLOB_DOOFFS. Rely on the
number of matches returned in gl_pathc, which seems to be more reliable in
error cases.

src/libstrongswan/collections/enumerator.c

index 0c97f796fe75c044aae8ef0c26301c645c3b8df3..fa277e7c87ce786ebf61b1ca3760a0bbf1abdec5 100644 (file)
@@ -171,8 +171,8 @@ typedef struct {
        enumerator_t public;
        /** glob data */
        glob_t glob;
-       /** current match */
-       char **match;
+       /** iteration count */
+       u_int pos;
        /** absolute path of current file */
        char full[PATH_MAX];
 } glob_enum_t;
@@ -191,12 +191,13 @@ static void destroy_glob_enum(glob_enum_t *this)
  */
 static bool enumerate_glob_enum(glob_enum_t *this, char **file, struct stat *st)
 {
-       char *match = *(++this->match);
+       char *match;
 
-       if (!match)
+       if (this->pos >= this->glob.gl_pathc)
        {
                return FALSE;
        }
+       match = this->glob.gl_pathv[this->pos++];
        if (file)
        {
                *file = match;
@@ -231,12 +232,9 @@ enumerator_t* enumerator_create_glob(const char *pattern)
                        .enumerate = (void*)enumerate_glob_enum,
                        .destroy = (void*)destroy_glob_enum,
                },
-               .glob = {
-                       .gl_offs = 1, /* reserve one slot so we can enumerate easily */
-               }
        );
 
-       status = glob(pattern, GLOB_DOOFFS | GLOB_ERR, NULL, &this->glob);
+       status = glob(pattern, GLOB_ERR, NULL, &this->glob);
        if (status == GLOB_NOMATCH)
        {
                DBG1(DBG_LIB, "no files found matching '%s'", pattern);
@@ -246,7 +244,6 @@ enumerator_t* enumerator_create_glob(const char *pattern)
                DBG1(DBG_LIB, "expanding file pattern '%s' failed: %s", pattern,
                         strerror(errno));
        }
-       this->match = this->glob.gl_pathv;
        return &this->public;
 }