diff options
author | Akira TAGOH <akira@tagoh.org> | 2015-02-10 19:32:13 +0900 |
---|---|---|
committer | Akira TAGOH <akira@tagoh.org> | 2015-02-23 15:21:08 +0900 |
commit | ed0d705e2221adbfb21de357c1a82b7a2a5c3641 (patch) | |
tree | bf82d741f6ba8411a2186ef0f1ce06613c083e7d /src | |
parent | d6d5adeb7940c0d0beb86489c2a1c2ce59e5c044 (diff) |
Fix a build fail on some non-POSIX platforms
Use own scandir function. according to this change,
we don't need -Werror things in configure anymore.
Diffstat (limited to 'src')
-rw-r--r-- | src/fcstat.c | 80 |
1 files changed, 72 insertions, 8 deletions
diff --git a/src/fcstat.c b/src/fcstat.c index bbae1021..354362f5 100644 --- a/src/fcstat.c +++ b/src/fcstat.c @@ -42,6 +42,7 @@ #ifdef HAVE_SYS_MOUNT_H #include <sys/mount.h> #endif +#include <errno.h> #ifdef _WIN32 #ifdef __GNUC__ @@ -164,21 +165,84 @@ FcDirChecksumScandirFilter(const struct dirent *entry) } #endif -#ifdef HAVE_SCANDIR static int FcDirChecksumScandirSorter(const struct dirent **lhs, const struct dirent **rhs) { return strcmp((*lhs)->d_name, (*rhs)->d_name); } -#elif HAVE_SCANDIR_VOID_P -static int -FcDirChecksumScandirSorter(const void *a, const void *b) + +static void +free_dirent (struct dirent **p) { - const struct dirent *lhs = a, *rhs = b; + struct dirent **x; + + for (x = p; *x != NULL; x++) + free (*x); - return strcmp(lhs->d_name, rhs->d_name); + free (p); +} + +int +FcScandir (const char *dirp, + struct dirent ***namelist, + int (*filter) (const struct dirent *), + int (*compar) (const struct dirent **, const struct dirent **)); + +int +FcScandir (const char *dirp, + struct dirent ***namelist, + int (*filter) (const struct dirent *), + int (*compar) (const struct dirent **, const struct dirent **)) +{ + DIR *d; + struct dirent *dent, *p, **dlist, **dlp; + size_t lsize = 128, n = 0; + + d = opendir (dirp); + if (!d) + return -1; + + dlist = (struct dirent **) malloc (sizeof (struct dirent *) * lsize); + if (!dlist) + { + closedir (d); + errno = ENOMEM; + + return -1; + } + *dlist = NULL; + while ((dent = readdir (d))) + { + if ((filter) (dent)) + { + p = (struct dirent *) malloc (sizeof (struct dirent)); + memcpy (p, dent, sizeof (struct dirent)); + if (n >= lsize) + { + lsize += 128; + dlp = (struct dirent **) realloc (dlist, sizeof (struct dirent *) * lsize); + if (!dlp) + { + free_dirent (dlist); + closedir (d); + errno = ENOMEM; + + return -1; + } + dlist = dlp; + } + dlist[n++] = p; + dlist[n] = NULL; + } + } + closedir (d); + + qsort (dlist, n, sizeof (struct dirent *), (int (*) (const void *, const void *))compar); + + *namelist = dlist; + + return n; } -#endif static int FcDirChecksum (const FcChar8 *dir, time_t *checksum) @@ -191,7 +255,7 @@ FcDirChecksum (const FcChar8 *dir, time_t *checksum) Adler32Init (&ctx); - n = scandir ((const char *)dir, &files, + n = FcScandir ((const char *)dir, &files, #ifdef HAVE_STRUCT_DIRENT_D_TYPE &FcDirChecksumScandirFilter, #else |