tronkko/dirent

Bad `scandir` prototype

Closed this issue · 2 comments

My installation of Linux has the following prototype for scandir:

extern int scandir (const char *__restrict __dir,
		    struct dirent ***__restrict __namelist,
		    int (*__selector) (const struct dirent *),
		    int (*__cmp) (const struct dirent **,
				  const struct dirent **))

The difference between void (your dirent) and dirent (Linux) pointers for compare causes problems in our project. Depending on the enabled warnings, you can't even pass alphasort (which uses dirent pointers in your code) to scandir. A Visual Studio warning recommends to cast it, and that includes the __cdecl convention - a macro which does not exist on other compilers.
So people patch this code for Visual Studio and by adding the cast, they break other platforms.

This cast should not be necessary in the first place - why is the prototype like this?
Can the prototype in your dirent.h be changed to use dirent pointers (on this repo)?

Thanks for your thorough error report! The prototype is truly different from Linux and cast only makes matters worse!

Could you please try the version 1.23.2 at https://github.com/tronkko/dirent/releases/tag/1.23.2 to see it fixes the issue? The same code is also on master branch.

Yes, this seems to work fine for us.
Thanks for the quickfix!

(Also: brave move to tag unverified changes for a release)