[git commit future 1/1] scandir, readdir[_t]: use one common source for X() and X64()

Peter S. Mazinger ps.m at gmx.net
Wed Mar 16 19:20:32 UTC 2011


commit: http://git.uclibc.org/uClibc/commit/?id=fbd6d724de80513392b7c3302a45369d01e4b7c3
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/future

Since the sources differ only minimally, use only scandir.c,readdir[_t].c
and redefine the used functions accordingly.
Use a strong_alias instead of compiling *64.c if __WORDSIZE = 64

Signed-off-by: Peter S. Mazinger <ps.m at gmx.net>
---
 libc/misc/dirent/readdir.c     |   28 ++++++----
 libc/misc/dirent/readdir64.c   |   52 ++----------------
 libc/misc/dirent/readdir64_r.c |   62 ++-------------------
 libc/misc/dirent/readdir_r.c   |   25 ++++++---
 libc/misc/dirent/scandir.c     |   40 ++++++++------
 libc/misc/dirent/scandir64.c   |  117 ++++------------------------------------
 6 files changed, 81 insertions(+), 243 deletions(-)

diff --git a/libc/misc/dirent/readdir.c b/libc/misc/dirent/readdir.c
index 4fcd1cc..7517106 100644
--- a/libc/misc/dirent/readdir.c
+++ b/libc/misc/dirent/readdir.c
@@ -4,20 +4,22 @@
  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  */
 
-#include <features.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #include <dirent.h>
+#include <errno.h>
+#define __need_NULL
+#include <stddef.h>
 #include "dirstream.h"
 
+#ifndef __READDIR
+# define __READDIR readdir
+# define __DIRENT_TYPE struct dirent
+# define __GETDENTS __getdents
+#endif
 
-struct dirent *readdir(DIR * dir)
+__DIRENT_TYPE *__READDIR(DIR * dir)
 {
 	ssize_t bytes;
-	struct dirent *de;
+	__DIRENT_TYPE *de;
 
 	if (!dir) {
 		__set_errno(EBADF);
@@ -29,7 +31,7 @@ struct dirent *readdir(DIR * dir)
 	do {
 	    if (dir->dd_size <= dir->dd_nextloc) {
 		/* read dir->dd_max bytes of directory entries. */
-		bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
+		bytes = __GETDENTS(dir->dd_fd, dir->dd_buf, dir->dd_max);
 		if (bytes <= 0) {
 		    de = NULL;
 		    goto all_done;
@@ -38,7 +40,7 @@ struct dirent *readdir(DIR * dir)
 		dir->dd_nextloc = 0;
 	    }
 
-	    de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
+	    de = (__DIRENT_TYPE *) (((char *) dir->dd_buf) + dir->dd_nextloc);
 
 	    /* Am I right? H.J. */
 	    dir->dd_nextloc += de->d_reclen;
@@ -53,4 +55,8 @@ all_done:
 	__UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
 	return de;
 }
-libc_hidden_def(readdir)
+libc_hidden_def(__READDIR)
+#if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
+strong_alias_untyped(readdir,readdir64)
+libc_hidden_def(readdir64)
+#endif
diff --git a/libc/misc/dirent/readdir64.c b/libc/misc/dirent/readdir64.c
index 1264c38..17577a7 100644
--- a/libc/misc/dirent/readdir64.c
+++ b/libc/misc/dirent/readdir64.c
@@ -5,52 +5,12 @@
  */
 
 #include <_lfs_64.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #include <dirent.h>
-#include "dirstream.h"
-
-struct dirent64 *readdir64(DIR * dir)
-{
-	ssize_t bytes;
-	struct dirent64 *de;
-
-	if (!dir) {
-		__set_errno(EBADF);
-		return NULL;
-	}
-
-	__UCLIBC_MUTEX_LOCK(dir->dd_lock);
-
-	do {
-	    if (dir->dd_size <= dir->dd_nextloc) {
-		/* read dir->dd_max bytes of directory entries. */
-		bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
-		if (bytes <= 0) {
-		    de = NULL;
-		    goto all_done;
-		}
-		dir->dd_size = bytes;
-		dir->dd_nextloc = 0;
-	    }
-
-	    de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
-
-	    /* Am I right? H.J. */
-	    dir->dd_nextloc += de->d_reclen;
-
-	    /* We have to save the next offset here. */
-	    dir->dd_nextoff = de->d_off;
-
-	    /* Skip deleted files.  */
-	} while (de->d_ino == 0);
 
-all_done:
-	__UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
+#if __WORDSIZE != 64
+# define __READDIR readdir64
+# define __DIRENT_TYPE struct dirent64
+# define __GETDENTS __getdents64
 
-	return de;
-}
-libc_hidden_def(readdir64)
+# include "readdir.c"
+#endif
diff --git a/libc/misc/dirent/readdir64_r.c b/libc/misc/dirent/readdir64_r.c
index ba72600..c045cbd 100644
--- a/libc/misc/dirent/readdir64_r.c
+++ b/libc/misc/dirent/readdir64_r.c
@@ -5,62 +5,12 @@
  */
 
 #include <_lfs_64.h>
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
 #include <dirent.h>
-#include "dirstream.h"
-
-
-int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
-{
-	int ret;
-	ssize_t bytes;
-	struct dirent64 *de;
-
-	if (!dir) {
-	    __set_errno(EBADF);
-	    return(EBADF);
-	}
-	de = NULL;
-
-	__UCLIBC_MUTEX_LOCK(dir->dd_lock);
-
-	do {
-	    if (dir->dd_size <= dir->dd_nextloc) {
-		/* read dir->dd_max bytes of directory entries. */
-		bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
-		if (bytes <= 0) {
-		    *result = NULL;
-		    ret = (bytes==0)? 0 : errno;
-		    goto all_done;
-		}
-		dir->dd_size = bytes;
-		dir->dd_nextloc = 0;
-	    }
-
-	    de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
-
-	    /* Am I right? H.J. */
-	    dir->dd_nextloc += de->d_reclen;
-
-	    /* We have to save the next offset here. */
-	    dir->dd_nextoff = de->d_off;
-	    /* Skip deleted files.  */
-	} while (de->d_ino == 0);
-
-	if (de == NULL) {
-	    *result = NULL;
-	} else {
-	    *result = memcpy (entry, de, de->d_reclen);
-	}
-	ret = 0;
 
-all_done:
+#if __WORDSIZE != 64
+# define __READDIR_R readdir64_r
+# define __DIRENT_TYPE struct dirent64
+# define __GETDENTS __getdents64
 
-	__UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
-        return((de != NULL)? 0 : ret);
-}
-libc_hidden_def(readdir64_r)
+# include "readdir_r.c"
+#endif
diff --git a/libc/misc/dirent/readdir_r.c b/libc/misc/dirent/readdir_r.c
index d08997f..7cdc0ab 100644
--- a/libc/misc/dirent/readdir_r.c
+++ b/libc/misc/dirent/readdir_r.c
@@ -4,19 +4,24 @@
  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  */
 
+#include <dirent.h>
 #include <errno.h>
-#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include <dirent.h>
+#define __need_NULL
+#include <stddef.h>
 #include "dirstream.h"
 
+#ifndef __READDIR_R
+# define __READDIR_R readdir_r
+# define __DIRENT_TYPE struct dirent
+# define __GETDENTS __getdents
+#endif
 
-int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
+int __READDIR_R(DIR *dir, __DIRENT_TYPE *entry, __DIRENT_TYPE **result)
 {
 	int ret;
 	ssize_t bytes;
-	struct dirent *de;
+	__DIRENT_TYPE *de;
 
 	if (!dir) {
 	    __set_errno(EBADF);
@@ -29,7 +34,7 @@ int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
 	do {
 	    if (dir->dd_size <= dir->dd_nextloc) {
 		/* read dir->dd_max bytes of directory entries. */
-		bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
+		bytes = __GETDENTS(dir->dd_fd, dir->dd_buf, dir->dd_max);
 		if (bytes <= 0) {
 		    *result = NULL;
 		    ret = (bytes==0)? 0 : errno;
@@ -39,7 +44,7 @@ int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
 		dir->dd_nextloc = 0;
 	    }
 
-	    de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
+	    de = (__DIRENT_TYPE *) (((char *) dir->dd_buf) + dir->dd_nextloc);
 
 	    /* Am I right? H.J. */
 	    dir->dd_nextloc += de->d_reclen;
@@ -61,4 +66,8 @@ all_done:
 	__UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
 	return((de != NULL)? 0 : ret);
 }
-libc_hidden_def(readdir_r)
+libc_hidden_def(__READDIR_R)
+#if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
+strong_alias_untyped(readdir_r,readdir64_r)
+libc_hidden_def(readdir64_r)
+#endif
diff --git a/libc/misc/dirent/scandir.c b/libc/misc/dirent/scandir.c
index bb42564..c036ce5 100644
--- a/libc/misc/dirent/scandir.c
+++ b/libc/misc/dirent/scandir.c
@@ -1,24 +1,29 @@
+/* vi: set sw=4 ts=4: */
 /*
- * Copyright (C) 2000-2006 Erik Andersen <andersen at uclibc.org>
+ * Copyright (C) 2000-2011 Erik Andersen <andersen at uclibc.org>
  *
  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  */
 
 #include <dirent.h>
-#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <sys/types.h>
 #include "dirstream.h"
 
-int scandir(const char *dir, struct dirent ***namelist,
-	int (*selector) (const struct dirent *),
-	int (*compar) (const struct dirent **, const struct dirent **))
+#ifndef __SCANDIR
+# define __SCANDIR scandir
+# define __DIRENT_TYPE struct dirent
+# define __READDIR readdir
+#endif
+
+int __SCANDIR(const char *dir, __DIRENT_TYPE ***namelist,
+	int (*selector) (const __DIRENT_TYPE *),
+	int (*compar) (const __DIRENT_TYPE **, const __DIRENT_TYPE **))
 {
     DIR *dp = opendir (dir);
-    struct dirent *current;
-    struct dirent **names = NULL;
+    __DIRENT_TYPE *current;
+    __DIRENT_TYPE **names = NULL;
     size_t names_size = 0, pos;
     int save;
 
@@ -29,7 +34,7 @@ int scandir(const char *dir, struct dirent ***namelist,
     __set_errno (0);
 
     pos = 0;
-    while ((current = readdir (dp)) != NULL) {
+    while ((current = __READDIR (dp)) != NULL) {
 	int use_it = selector == NULL;
 
 	if (! use_it)
@@ -43,7 +48,7 @@ int scandir(const char *dir, struct dirent ***namelist,
 	}
 	if (use_it)
 	{
-	    struct dirent *vnew;
+	    __DIRENT_TYPE *vnew;
 	    size_t dsize;
 
 	    /* Ignore errors from selector or readdir */
@@ -51,24 +56,24 @@ int scandir(const char *dir, struct dirent ***namelist,
 
 	    if (unlikely(pos == names_size))
 	    {
-		struct dirent **new;
+		__DIRENT_TYPE **new;
 		if (names_size == 0)
 		    names_size = 10;
 		else
 		    names_size *= 2;
-		new = (struct dirent **) realloc (names,
-					names_size * sizeof (struct dirent *));
+		new = (__DIRENT_TYPE **) realloc (names,
+					names_size * sizeof (__DIRENT_TYPE *));
 		if (new == NULL)
 		    break;
 		names = new;
 	    }
 
 	    dsize = &current->d_name[_D_ALLOC_NAMLEN(current)] - (char*)current;
-	    vnew = (struct dirent *) malloc (dsize);
+	    vnew = (__DIRENT_TYPE *) malloc (dsize);
 	    if (vnew == NULL)
 		break;
 
-	    names[pos++] = (struct dirent *) memcpy (vnew, current, dsize);
+	    names[pos++] = (__DIRENT_TYPE *) memcpy (vnew, current, dsize);
 	}
     }
 
@@ -88,7 +93,10 @@ 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 *), (comparison_fn_t) compar);
+	qsort (names, pos, sizeof (__DIRENT_TYPE *), (comparison_fn_t) compar);
     *namelist = names;
     return pos;
 }
+#if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
+strong_alias_untyped(scandir,scandir64)
+#endif
diff --git a/libc/misc/dirent/scandir64.c b/libc/misc/dirent/scandir64.c
index 3d2a250..634c5d8 100644
--- a/libc/misc/dirent/scandir64.c
+++ b/libc/misc/dirent/scandir64.c
@@ -1,111 +1,16 @@
-/* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.
-   */
-
-/* Modified for uClibc by Erik Andersen
-   */
+/*
+ * Copyright (C) 2000-2011 Erik Andersen <andersen at uclibc.org>
+ *
+ * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+ */
 
 #include <_lfs_64.h>
-
 #include <dirent.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <sys/types.h>
-#include "dirstream.h"
-
-int scandir64(const char *dir, struct dirent64 ***namelist,
-	int (*selector) (const struct dirent64 *),
-	int (*compar) (const struct dirent64 **, const struct dirent64 **))
-{
-    DIR *dp = opendir (dir);
-    struct dirent64 *current;
-    struct dirent64 **names = NULL;
-    size_t names_size = 0, pos;
-    int save;
-
-    if (dp == NULL)
-	return -1;
-
-    save = errno;
-    __set_errno (0);
-
-    pos = 0;
-    while ((current = readdir64 (dp)) != NULL) {
-	int use_it = selector == NULL;
-
-	if (! use_it)
-	{
-	    use_it = (*selector) (current);
-	    /* The selector function might have changed errno.
-	     * It was zero before and it need to be again to make
-	     * the latter tests work.  */
-	    if (! use_it)
-		__set_errno (0);
-	}
-	if (use_it)
-	{
-	    struct dirent64 *vnew;
-	    size_t dsize;
-
-	    /* Ignore errors from selector or readdir64 */
-	    __set_errno (0);
-
-	    if (unlikely(pos == names_size))
-	    {
-		struct dirent64 **new;
-		if (names_size == 0)
-		    names_size = 10;
-		else
-		    names_size *= 2;
-		new = (struct dirent64 **) realloc (names,
-				      names_size * sizeof (struct dirent64 *));
-		if (new == NULL)
-		    break;
-		names = new;
-	    }
-
-	    dsize = &current->d_name[_D_ALLOC_NAMLEN(current)] - (char*)current;
-	    vnew = (struct dirent64 *) malloc (dsize);
-	    if (vnew == NULL)
-		break;
-
-	    names[pos++] = (struct dirent64 *) memcpy (vnew, current, dsize);
-	}
-    }
-    if (unlikely(errno != 0))
-    {
-	save = errno;
-	closedir (dp);
-	while (pos > 0)
-	    free (names[--pos]);
-	free (names);
-	__set_errno (save);
-	return -1;
-    }
 
-    closedir (dp);
-    __set_errno (save);
+#if __WORDSIZE != 64
+# define __SCANDIR scandir64
+# define __DIRENT_TYPE struct dirent64
+# define __READDIR readdir64
 
-    /* Sort the list if we have a comparison function to sort with.  */
-    if (compar != NULL)
-	qsort (names, pos, sizeof (struct dirent64 *), (comparison_fn_t) compar);
-    *namelist = names;
-    return pos;
-}
+# include "scandir.c"
+#endif
-- 
1.7.3.4



More information about the uClibc-cvs mailing list