Search code examples
cfunction-prototypesscandir

scandir match function arguments


I am using scandir to match certain files from a dir. The match function takes const struct dirent *dp argument.

But I also need to pass another argument with it. When I try to do that, compiles gives me a warning (not error) that my match function is of incompatible pointer type.

Is it not allowed to pass another argument to match function? If it's not, I might have to make that particular variable global which I do not want to do.

code snippet:

/* below I am adding new argument - char *str */
match_function (const struct dirent *dp, char *str) {
}

function() {
count = scandir(PATH, &namelist, match_function, alphasort);
}

warning:

warning: passing argument 3 of 'scandir' from incompatible pointer type

Solution

  • Another approach, which may be preferable to using global variables or thread-specific data, is just to write your own replacement for scandir and have it take an extra void * argument which it would pass to the match function. Considering that scandir is easily implemented in less than 50 lines of code, this is perfectly reasonable.

    Here is a possible implementation of scandir:

    http://git.etalabs.net/cgi-bin/gitweb.cgi?p=musl;a=blob;f=src/dirent/scandir.c