Search code examples
c++posixglobglibc

Glob searching directories also returns files


Why does does using the glob function with the path /home/user/*/ return both directories and files. I expect that since the path ends in /, only directories should be returned, but it is also returning files.

I am running the following code:

vector<string> glob(const std::string& path)
{
    glob_t glob_result;
    glob(path.c_str(),GLOB_TILDE,NULL,&glob_result);
    vector<string> ret;
    for(unsigned int i=0;i<glob_result.gl_pathc;++i){
        ret.push_back(string(glob_result.gl_pathv[i]));
    }
    globfree(&glob_result);
    return ret;
}

The input for the function is /home/user/*/ and the output is /home/user/a.txt, /home/user/b.txt and /home/user/nested_folder

Why is returning both files and directories instead of only directories.


Solution

  • This is an old glibc bug which was fixed in glibc 2.19: