From 75552ab845005d5b386d4c1383d9c10193168847 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 20 Jul 2009 13:19:36 -0400 Subject: dirent: push dirent type to prototypes This syncs the dirent related functions with the glibc behavior -- rather than take void pointers everywhere, make the struct dirent pointers explicit in the API. After all, the functions themselves will cast the pointers to a dirent structure, so if it isn't as expected, people will crash. Signed-off-by: Mike Frysinger --- include/dirent.h | 74 +++++++++++++++++++++++++++++++--------- libc/misc/dirent/alphasort.c | 7 ++-- libc/misc/dirent/alphasort64.c | 7 ++-- libc/misc/dirent/scandir.c | 10 ++---- libc/misc/dirent/scandir64.c | 10 ++---- libc/misc/dirent/versionsort.c | 5 ++- libc/misc/dirent/versionsort64.c | 5 ++- 7 files changed, 70 insertions(+), 48 deletions(-) diff --git a/include/dirent.h b/include/dirent.h index d17f10998..25fd77c4e 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -249,7 +249,8 @@ libc_hidden_proto(dirfd) extern int scandir (__const char *__restrict __dir, struct dirent ***__restrict __namelist, int (*__selector) (__const struct dirent *), - int (*__cmp) (__const void *, __const void *)) + int (*__cmp) (__const struct dirent **, + __const struct dirent **)) __nonnull ((1, 2)); # else # ifdef __REDIRECT @@ -257,7 +258,8 @@ extern int __REDIRECT (scandir, (__const char *__restrict __dir, struct dirent ***__restrict __namelist, int (*__selector) (__const struct dirent *), - int (*__cmp) (__const void *, __const void *)), + int (*__cmp) (__const struct dirent **, + __const struct dirent **)), scandir64) __nonnull ((1, 2)); # else # define scandir scandir64 @@ -270,18 +272,21 @@ extern int __REDIRECT (scandir, extern int scandir64 (__const char *__restrict __dir, struct dirent64 ***__restrict __namelist, int (*__selector) (__const struct dirent64 *), - int (*__cmp) (__const void *, __const void *)) + int (*__cmp) (__const struct dirent64 **, + __const struct dirent64 **)) __nonnull ((1, 2)); # endif /* Function to compare two `struct dirent's alphabetically. */ # ifndef __USE_FILE_OFFSET64 -extern int alphasort (__const void *__e1, __const void *__e2) +extern int alphasort (__const struct dirent **__e1, + __const struct dirent **__e2) __THROW __attribute_pure__ __nonnull ((1, 2)); # else # ifdef __REDIRECT -extern int __REDIRECT (alphasort, - (__const void *__e1, __const void *__e2), +extern int __REDIRECT_NTH (alphasort, + (__const struct dirent **__e1, + __const struct dirent **__e2), alphasort64) __attribute_pure__ __nonnull ((1, 2)); # else # define alphasort alphasort64 @@ -289,30 +294,67 @@ extern int __REDIRECT (alphasort, # endif # if defined __USE_GNU && defined __USE_LARGEFILE64 -extern int alphasort64 (__const void *__e1, __const void *__e2) +extern int alphasort64 (__const struct dirent64 **__e1, + __const struct dirent64 **__e2) __THROW __attribute_pure__ __nonnull ((1, 2)); # endif +#endif /* Use BSD or misc or XPG7. */ -/* Function to compare two `struct dirent's alphabetically. */ + +#if defined __USE_BSD || defined __USE_MISC +/* Read directory entries from FD into BUF, reading at most NBYTES. + Reading starts at offset *BASEP, and *BASEP is updated with the new + position after reading. Returns the number of bytes read; zero when at + end of directory; or -1 for errors. */ +# ifndef __USE_FILE_OFFSET64 +extern __ssize_t getdirentries (int __fd, char *__restrict __buf, + size_t __nbytes, + __off_t *__restrict __basep) + __THROW __nonnull ((2, 4)); +# else +# ifdef __REDIRECT +extern __ssize_t __REDIRECT_NTH (getdirentries, + (int __fd, char *__restrict __buf, + size_t __nbytes, + __off64_t *__restrict __basep), + getdirentries64) __nonnull ((2, 4)); +# else +# define getdirentries getdirentries64 +# endif +# endif + +# ifdef __USE_LARGEFILE64 +extern __ssize_t getdirentries64 (int __fd, char *__restrict __buf, + size_t __nbytes, + __off64_t *__restrict __basep) + __THROW __nonnull ((2, 4)); +# endif +#endif /* Use BSD or misc. */ + +#ifdef __USE_GNU +/* Function to compare two `struct dirent's by name & version. */ # ifndef __USE_FILE_OFFSET64 -extern int versionsort (__const void *__e1, __const void *__e2) +extern int versionsort (__const struct dirent **__e1, + __const struct dirent **__e2) __THROW __attribute_pure__ __nonnull ((1, 2)); # else # ifdef __REDIRECT -extern int __REDIRECT (versionsort, - (__const void *__e1, __const void *__e2), - versionsort64) __attribute_pure__ __nonnull ((1, 2)); +extern int __REDIRECT_NTH (versionsort, + (__const struct dirent **__e1, + __const struct dirent **__e2), + versionsort64) + __attribute_pure__ __nonnull ((1, 2)); # else # define versionsort versionsort64 # endif # endif -# if defined __USE_GNU && defined __USE_LARGEFILE64 -extern int versionsort64 (__const void *__e1, __const void *__e2) +# ifdef __USE_LARGEFILE64 +extern int versionsort64 (__const struct dirent64 **__e1, + __const struct dirent64 **__e2) __THROW __attribute_pure__ __nonnull ((1, 2)); # endif - -#endif /* Use BSD or misc. */ +#endif /* Use GNU. */ __END_DECLS diff --git a/libc/misc/dirent/alphasort.c b/libc/misc/dirent/alphasort.c index 70aa2a516..eb0dbf238 100644 --- a/libc/misc/dirent/alphasort.c +++ b/libc/misc/dirent/alphasort.c @@ -8,11 +8,8 @@ #include #include "dirstream.h" -/* Experimentally off - libc_hidden_proto(strcmp) */ - -int alphasort(const void * a, const void * b) +int alphasort(const struct dirent **a, const struct dirent **b) { - return strcmp ((*(const struct dirent **) a)->d_name, - (*(const struct dirent **) b)->d_name); + return strcmp((*a)->d_name, (*b)->d_name); } diff --git a/libc/misc/dirent/alphasort64.c b/libc/misc/dirent/alphasort64.c index de7a87a9a..d65b59647 100644 --- a/libc/misc/dirent/alphasort64.c +++ b/libc/misc/dirent/alphasort64.c @@ -10,10 +10,7 @@ #include #include "dirstream.h" -/* Experimentally off - libc_hidden_proto(strcmp) */ - -int alphasort64(const void * a, const void * b) +int alphasort64(const struct dirent64 **a, const struct dirent64 **b) { - return strcmp ((*(const struct dirent64 **) a)->d_name, - (*(const struct dirent64 **) b)->d_name); + return strcmp((*a)->d_name, (*b)->d_name); } diff --git a/libc/misc/dirent/scandir.c b/libc/misc/dirent/scandir.c index 9f1055439..bb425648b 100644 --- a/libc/misc/dirent/scandir.c +++ b/libc/misc/dirent/scandir.c @@ -12,15 +12,9 @@ #include #include "dirstream.h" -/* Experimentally off - libc_hidden_proto(memcpy) */ -/* libc_hidden_proto(opendir) */ -/* libc_hidden_proto(closedir) */ -/* libc_hidden_proto(qsort) */ -/* libc_hidden_proto(readdir) */ - int scandir(const char *dir, struct dirent ***namelist, int (*selector) (const struct dirent *), - int (*compar) (const void *, const void *)) + int (*compar) (const struct dirent **, const struct dirent **)) { DIR *dp = opendir (dir); struct dirent *current; @@ -94,7 +88,7 @@ int scandir(const char *dir, struct dirent ***namelist, /* Sort the list if we have a comparison function to sort with. */ if (compar != NULL) - qsort (names, pos, sizeof (struct dirent *), compar); + qsort (names, pos, sizeof (struct dirent *), (comparison_fn_t) compar); *namelist = names; return pos; } diff --git a/libc/misc/dirent/scandir64.c b/libc/misc/dirent/scandir64.c index bbd452d50..3d2a250a4 100644 --- a/libc/misc/dirent/scandir64.c +++ b/libc/misc/dirent/scandir64.c @@ -30,15 +30,9 @@ #include #include "dirstream.h" -/* Experimentally off - libc_hidden_proto(memcpy) */ -/* libc_hidden_proto(opendir) */ -/* libc_hidden_proto(closedir) */ -/* libc_hidden_proto(qsort) */ -/* libc_hidden_proto(readdir64) */ - int scandir64(const char *dir, struct dirent64 ***namelist, int (*selector) (const struct dirent64 *), - int (*compar) (const void *, const void *)) + int (*compar) (const struct dirent64 **, const struct dirent64 **)) { DIR *dp = opendir (dir); struct dirent64 *current; @@ -111,7 +105,7 @@ int scandir64(const char *dir, struct dirent64 ***namelist, /* Sort the list if we have a comparison function to sort with. */ if (compar != NULL) - qsort (names, pos, sizeof (struct dirent64 *), compar); + qsort (names, pos, sizeof (struct dirent64 *), (comparison_fn_t) compar); *namelist = names; return pos; } diff --git a/libc/misc/dirent/versionsort.c b/libc/misc/dirent/versionsort.c index 53476e67f..d84da1f6c 100644 --- a/libc/misc/dirent/versionsort.c +++ b/libc/misc/dirent/versionsort.c @@ -8,8 +8,7 @@ #include #include "dirstream.h" -int versionsort(const void *a, const void *b) +int versionsort(const struct dirent **a, const struct dirent **b) { - return strverscmp((*(const struct dirent **) a)->d_name, - (*(const struct dirent **) b)->d_name); + return strverscmp((*a)->d_name, (*b)->d_name); } diff --git a/libc/misc/dirent/versionsort64.c b/libc/misc/dirent/versionsort64.c index 684a46283..af9689eab 100644 --- a/libc/misc/dirent/versionsort64.c +++ b/libc/misc/dirent/versionsort64.c @@ -10,8 +10,7 @@ #include #include "dirstream.h" -int versionsort64(const void *a, const void *b) +int versionsort64(const struct dirent64 **a, const struct dirent64 **b) { - return strverscmp((*(const struct dirent64 **) a)->d_name, - (*(const struct dirent64 **) b)->d_name); + return strverscmp((*a)->d_name, (*b)->d_name); } -- cgit v1.2.3