cfunctionscandir

How does the 4th argument of scandir() work?


The definition of scandir() looks like this:

int scandir(const char *dirp, struct dirent ***namelist,
   int (*filter)(const struct dirent *),
   int (*compar)(const struct dirent **, const struct dirent **));

What is compar? What difference does it make what callback I pass there? The man page doesn't seem to say anything. Is it safe to pass NULL there? Can someone explain to me the purpose and usage of the 4th argument? I already did my research, and didn't find any useful information so, well, here I am.


Solution

  • It determines the order in which the items are listed, in a similar manner to qsort. Passing NULL is undefined behavior. Thanks for the comments.