From 3d23ede4ebee271a6ab60cd181ee830979756064 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sun, 9 Mar 2003 09:22:55 +0000 Subject: [PATCH] Include "save-cwd.h". (ftw_startup): Avoid use of getcwd, if possible. Instead, use fopen/fchdir via save_cwd/restore_cwd. --- lib/ftw.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/ftw.c b/lib/ftw.c index 2161b89b59..817c0f17bc 100644 --- a/lib/ftw.c +++ b/lib/ftw.c @@ -88,6 +88,8 @@ char *stpcpy (); /* #define NDEBUG 1 */ #include +#include "save-cwd.h" + #ifndef _LIBC # undef __chdir # define __chdir chdir @@ -95,9 +97,6 @@ char *stpcpy (); # define __closedir closedir # undef __fchdir # define __fchdir fchdir -# undef __getcwd -# define __getcwd(P, N) xgetcwd () -extern char *xgetcwd (void); # undef __mempcpy # define __mempcpy mempcpy # undef __opendir @@ -643,7 +642,7 @@ ftw_startup (const char *dir, int is_nftw, NFTW_FUNC_T func, int descriptors, struct STAT st; int result = 0; int save_err; - char *cwd = NULL; + struct saved_cwd cwd; size_t dir_len; /* First make sure the parameters are reasonable. */ @@ -694,9 +693,7 @@ ftw_startup (const char *dir, int is_nftw, NFTW_FUNC_T func, int descriptors, /* Now go to the directory containing the initial file/directory. */ if (flags & FTW_CHDIR) { - /* GNU extension ahead. */ - cwd = __getcwd (NULL, 0); - if (cwd == NULL) + if (save_cwd (&cwd)) result = -1; else if (data.ftw.base > 0) { @@ -772,11 +769,21 @@ ftw_startup (const char *dir, int is_nftw, NFTW_FUNC_T func, int descriptors, } /* Return to the start directory (if necessary). */ - if (cwd != NULL) + if (flags & FTW_CHDIR) { save_err = errno; - __chdir (cwd); - free (cwd); + /* If restore_cwd fails and there wasn't a prior failure, + then let this new errno override any prior value. + FIXME: ideally, we'd be able to return some indication + of what the failure means. Otherwise, the caller will + have a hard time distinguishing between e.g., `out of memory' + and this sort of failure. */ + if (restore_cwd (&cwd) && result == 0) + { + save_err = errno; + result = -1; + } + __set_errno (save_err); } -- 2.47.3