Search code examples
clinuxfilesystemssystem-callstraversal

fts_children() not working


I am having difficulty with the function fts_children() referenced in this man page http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html. It seems that fts_children() does not get all of the files in the sub directories as it claims it would in the man page. For instance I have a file temp in the folder v2 with many files in it. I get all of the file in the sub directory except for the one that is alphabetically first. Is there something I can do about this? My code is below and the files in the subdirectory with permissions. code:

int compare (const FTSENT**, const FTSENT**);

int main(int argc, char* const argv[])
{
    FTS* file_system = NULL;
    FTSENT* child = NULL;
    FTSENT* parent = NULL;

    file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);

    if (NULL != file_system)
    {
        while( (parent = fts_read(file_system)) != NULL)
        {

            child = fts_children(file_system,0);
            if (errno != 0)


        while ((NULL != child) && (NULL != child->fts_link))

            {
                child = child->fts_link;
                printf("%s\n", child->fts_name);
            }

        }
        fts_close(file_system);
    }
    return 0;
}

int compare(const FTSENT** one, const FTSENT** two)
{
    return (strcmp((*one)->fts_name, (*two)->fts_name));
}

files of temp (outputted with ls -l):

total 8
-rwxrwxrwx  1 tparisi student 14 Sep 27 13:05 a
-rw-r--r--+ 1 tparisi student 25 Sep 26 14:42 f
-rw-r--r--+ 1 tparisi student 13 Sep 27 11:28 file2
-rw-r--r--+ 1 tparisi student 14 Sep 27 11:42 files
drwxr-xr-x+ 2 tparisi student  3 Sep 27 13:33 temp2
-rw-r--r--+ 1 tparisi student 14 Sep 27 13:04 test
-rw-r--r--+ 1 tparisi student  5 Sep 27 13:23 z

The entire directory is printed accept for a when I run my program.

Any Help would be great! Thanks!


Solution

  • You skip the first entry when traversing the returned list:

    while ((NULL != child) && (NULL != child->fts_link))
    {
        child = child->fts_link;
        printf("%s\n", child->fts_name);
    }
    

    Try

    while(child)
    {
        printf("%s\n", child->fts_name);
        child = child->fts_link;
    }
    

    to get all files from the returned list printed out.