add (currently stubbed due to stubbed strverscmp) versionsort function

based on patch by Emil Renner Berthing, with minor changes to dirent.h
for LFS64 and organization of declarations

this code should work unmodified once a real strverscmp is added, but
I've been hesitant to add it because the GNU strverscmp behavior is
harmful in a lot of cases (for instance if you have numeric filenames
in hex). at some point I plan on trying to design a variant of the
algorithm that behaves better on a mix of filename styles.
This commit is contained in:
Rich Felker 2012-06-13 11:14:38 -04:00
parent e361019c24
commit fbffcee63d
2 changed files with 13 additions and 0 deletions

View File

@ -50,12 +50,17 @@ int scandir(const char *, struct dirent ***, int (*)(const struct dirent *), int
#define DTTOIF(x) ((x)<<12) #define DTTOIF(x) ((x)<<12)
#endif #endif
#ifdef _GNU_SOURCE
int versionsort(const struct dirent **, const struct dirent **);
#endif
#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
#define dirent64 dirent #define dirent64 dirent
#define readdir64 readdir #define readdir64 readdir
#define readdir64_r readdir_r #define readdir64_r readdir_r
#define scandir64 scandir #define scandir64 scandir
#define alphasort64 alphasort #define alphasort64 alphasort
#define versionsort64 versionsort
#define off64_t off_t #define off64_t off_t
#define ino64_t ino_t #define ino64_t ino_t
#endif #endif

8
src/dirent/versionsort.c Normal file
View File

@ -0,0 +1,8 @@
#define _GNU_SOURCE
#include <string.h>
#include <dirent.h>
int versionsort(const struct dirent **a, const struct dirent **b)
{
return strverscmp((*a)->d_name, (*b)->d_name);
}