[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: portability patch for io/ftw.c
From: |
Jim Meyering |
Subject: |
Re: portability patch for io/ftw.c |
Date: |
Fri, 07 Feb 2003 22:08:13 +0100 |
Ulrich Drepper <address@hidden> wrote:
> Jim Meyering wrote:
>
>> +#if defined _LIBC
...
>> +#else
>> +# if HAVE_DIRENT_H || defined _LIBC
...
> This part is not correct.
True, but it is also harmless.
I'll remove the `|| defined _LIBC'.
Thanks.
> First, why a || defined _libc in the #else branch?
That's probably a vestige of my first attempt to adapt the
usually-considered-sufficiently-general code to the special
case of glib/_LIBC. IMHO, we shouldn't have to make such
exceptions and litter otherwise-clean code with cpp conditionals
involving _LIBC. end of rant :-)
> Second, I doubt you can rely on d_namlen for all platforms without
> <dirent.h>. And vice versa: there are platforms which have <dirent.h>
> and which define an appropriate field, making the strlen() call unnecessary.
If you can point to such a system, please let everyone know.
That code has been used in the GNU tools I maintain for a long
time, without causing any problems, afaik. Similarly, it made
it into the autoconf manual, so I suspect if there were a significant
problem with it, it would have been addressed by now.
As for being optimal, there may be a few proprietary systems on which
the strlen is not strictly necessary, but I don't want to spend my
time finding them and optimizing this code to accommodate them.
...
> You remove the __getcwd from the first #ifndef _LIBC block just to add
> it in a directly following new #ifndef _LIBC block? Please keep the
> changes to a minimum.
I'm sure you noticed that the lines I removed were not the
same as the ones I added.
I thought the case of getcwd was sufficiently different that it
deserved to be separate, but it'd be better to do the same thing
with a couple blank lines.
Here's a new patch:
2003-02-07 Jim Meyering <address@hidden>
* io/ftw.c: Add autoconf-recommended block of alloca-related code.
Include autoconf-recommended block of dirent/NAMELEN-related
definitions and includes. Use NAMELEN throughout, rather than
_D_EXACT_NAMLEN.
[_LIBC]: Define NAMELEN to _D_EXACT_NAMLEN.
[!_LIBC] (__getcwd): Define to xgetcwd and declare xgetcwd.
(stpcpy): Declare, if necessary.
(mempcpy): Define, if necessary.
[!_LIBC] (__stpcpy, __mempcpy): Define.
[!_LIBC] (LXSTAT, XSTAT): Define.
(lstat) [!LIBC && !LSTAT_FOLLOWS_SLASHED_SYMLINK]: Define to rpl_lstat.
(find_object): Don't use c99-style struct initializer.
Tweak wording in a couple comments.
Index: io/ftw.c
===================================================================
RCS file: /cvs/glibc/libc/io/ftw.c,v
retrieving revision 1.40
diff -u -p -u -p -r1.40 ftw.c
--- io/ftw.c 5 Feb 2003 17:46:12 -0000 1.40
+++ io/ftw.c 7 Feb 2003 21:04:10 -0000
@@ -22,7 +22,42 @@
# include <config.h>
#endif
-#include <dirent.h>
+#if __GNUC__
+# define alloca __builtin_alloca
+#else
+# if HAVE_ALLOCA_H
+# include <alloca.h>
+# else
+# ifdef _AIX
+ # pragma alloca
+# else
+char *alloca ();
+# endif
+# endif
+#endif
+
+#if defined _LIBC
+# include <dirent.h>
+# define NAMLEN(dirent) _D_EXACT_NAMLEN(dirent)
+#else
+# if HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen ((dirent)->d_name)
+# else
+# define dirent direct
+# define NAMLEN(dirent) (dirent)->d_namlen
+# if HAVE_SYS_NDIR_H
+# include <sys/ndir.h>
+# endif
+# if HAVE_SYS_DIR_H
+# include <sys/dir.h>
+# endif
+# if HAVE_NDIR_H
+# include <ndir.h>
+# endif
+# endif
+#endif
+
#include <errno.h>
#include <ftw.h>
#include <limits.h>
@@ -39,6 +74,15 @@
# include <sys/stat.h>
#endif
+#if ! _LIBC && !HAVE_DECL_STPCPY && !defined stpcpy
+char *stpcpy ();
+#endif
+
+#if ! _LIBC && ! defined HAVE_MEMPCPY && ! defined mempcpy
+/* Be CAREFUL that there are no side effects in N. */
+# define mempcpy(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
+#endif
+
/* #define NDEBUG 1 */
#include <assert.h>
@@ -49,8 +93,11 @@
# define __closedir closedir
# undef __fchdir
# define __fchdir fchdir
+
# undef __getcwd
-# define __getcwd getcwd
+# define __getcwd(P, N) xgetcwd ()
+extern char *xgetcwd (void);
+
# undef __opendir
# define __opendir opendir
# undef __readdir64
@@ -61,6 +108,10 @@
# define __tfind tfind
# undef __tsearch
# define __tsearch tsearch
+# undef __stpcpy
+# define __stpcpy stpcpy
+# undef __mempcpy
+# define __mempcpy mempcpy
# undef internal_function
# define internal_function /* empty */
# undef dirent64
@@ -69,6 +120,15 @@
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
+/* Arrange to make lstat calls go through the wrapper function
+ on systems with an lstat function that does not dereference symlinks
+ that are specified with a trailing slash. */
+#if ! _LIBC && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
+int rpl_lstat (const char *, struct stat *);
+# undef lstat
+# define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
+#endif
+
#ifndef __set_errno
# define __set_errno(Val) errno = (Val)
#endif
@@ -79,8 +139,13 @@
# define NFTW_NAME nftw
# define INO_T ino_t
# define STAT stat
-# define LXSTAT __lxstat
-# define XSTAT __xstat
+# ifdef _LIBC
+# define LXSTAT __lxstat
+# define XSTAT __xstat
+# else
+# define LXSTAT(V,f,sb) lstat (f,sb)
+# define XSTAT(V,f,sb) stat (f,sb)
+# endif
# define FTW_FUNC_T __ftw_func_t
# define NFTW_FUNC_T __nftw_func_t
#endif
@@ -124,7 +189,7 @@ struct ftw_data
int flags;
/* Conversion array for flag values. It is the identity mapping for
- `nftw' calls, otherwise it maps the values to those know by
+ `nftw' calls, otherwise it maps the values to those known by
`ftw'. */
const int *cvt_arr;
@@ -140,9 +205,8 @@ struct ftw_data
};
-/* Internally we use the FTW_* constants used for `nftw'. When the
- process called `ftw' we must reduce the flag to the known flags
- for `ftw'. */
+/* Internally we use the FTW_* constants used for `nftw'. When invoked
+ as `ftw', map each flag to the subset of values used by `ftw'. */
static const int nftw_arr[] =
{
FTW_F, FTW_D, FTW_DNR, FTW_NS, FTW_SL, FTW_DP, FTW_SLN
@@ -188,7 +252,9 @@ add_object (struct ftw_data *data, struc
static inline int
find_object (struct ftw_data *data, struct STAT *st)
{
- struct known_object obj = { .dev = st->st_dev, .ino = st->st_ino };
+ struct known_object obj;
+ obj.dev = st->st_dev;
+ obj.ino = st->st_ino;
return __tfind (&obj, &data->known_objects, object_compare) != NULL;
}
@@ -216,7 +282,7 @@ open_dir_stream (struct ftw_data *data,
while ((d = __readdir64 (st)) != NULL)
{
- size_t this_len = _D_EXACT_NAMLEN (d);
+ size_t this_len = NAMLEN (d);
if (actsize + this_len + 2 >= bufsize)
{
char *newp;
@@ -354,7 +420,7 @@ process_entry (struct ftw_data *data, st
if (result == 0 && (data->flags & FTW_CHDIR))
{
- /* Change back to current directory. */
+ /* Change back to parent directory. */
int done = 0;
if (dir->stream != NULL)
if (__fchdir (dirfd (dir->stream)) == 0)
@@ -452,7 +518,7 @@ ftw_dir (struct ftw_data *data, struct S
while (dir.stream != NULL && (d = __readdir64 (dir.stream)) != NULL)
{
- result = process_entry (data, &dir, d->d_name, _D_EXACT_NAMLEN (d));
+ result = process_entry (data, &dir, d->d_name, NAMLEN (d));
if (result != 0)
break;
}
pgpL0LeTinh1X.pgp
Description: PGP signature