diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2004-12-03 06:25:03 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2004-12-03 06:25:03 +0000 |
commit | f2173447ba3972a35e0241e4e496520e1c4a7c60 (patch) | |
tree | 45237354742803032729b19be4e8f1c933b0b423 /lib | |
parent | 2da62dbe9ab48896948024e4016c772cafa6685b (diff) | |
download | coreutils-f2173447ba3972a35e0241e4e496520e1c4a7c60.tar.xz |
save-cwd.c: Include "save-cwd.h" before other include files.
(O_DIRECTORY): Remove; not needed here, since "." must be
a directory. All uses removed.
(save_cwd): Use __sgi || __sun, not sun || __sun. __sun is
universal on Suns, and we also need to test for IRIX.
Revamp code to use 'if' rather than '#if'.
Avoid unnecessary comparison of cwd->desc to 0.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/save-cwd.c | 55 |
1 files changed, 26 insertions, 29 deletions
diff --git a/lib/save-cwd.c b/lib/save-cwd.c index 20df32174..272f7421e 100644 --- a/lib/save-cwd.c +++ b/lib/save-cwd.c @@ -21,6 +21,8 @@ # include "config.h" #endif +#include "save-cwd.h" + #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -37,12 +39,6 @@ #include <errno.h> -#ifndef O_DIRECTORY -# define O_DIRECTORY 0 -#endif - -#include "save-cwd.h" - #include "chdir-long.h" #include "xgetcwd.h" @@ -66,48 +62,49 @@ int save_cwd (struct saved_cwd *cwd) { +#if !HAVE_FCHDIR +# undef fchdir +# define fchdir(x) (abort (), 0) + bool have_working_fchdir = false; + bool fchdir_needs_testing = false; +#elif (__sgi || __sun) static bool have_working_fchdir = true; + bool fchdir_needs_testing = true; +#else + bool have_working_fchdir = true; + bool fchdir_needs_testing = false; +#endif cwd->desc = -1; cwd->name = NULL; if (have_working_fchdir) { -#if HAVE_FCHDIR - cwd->desc = open (".", O_RDONLY | O_DIRECTORY); - if (cwd->desc < 0) - cwd->desc = open (".", O_WRONLY | O_DIRECTORY); + cwd->desc = open (".", O_RDONLY); if (cwd->desc < 0) { - cwd->name = xgetcwd (); - return cwd->name ? 0 : -1; + cwd->desc = open (".", O_WRONLY); + if (cwd->desc < 0) + { + cwd->name = xgetcwd (); + return cwd->name ? 0 : -1; + } } -# if __sun__ || sun /* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing is enabled, so we have to fall back to chdir. */ - if (fchdir (cwd->desc)) + if (fchdir_needs_testing && fchdir (cwd->desc) != 0) { - if (errno == EINVAL) - { - close (cwd->desc); - cwd->desc = -1; - have_working_fchdir = false; - } - else + int saved_errno = errno; + close (cwd->desc); + cwd->desc = -1; + if (saved_errno != EINVAL) { - int saved_errno = errno; - close (cwd->desc); - cwd->desc = -1; errno = saved_errno; return -1; } + have_working_fchdir = false; } -# endif /* __sun__ || sun */ -#else -# define fchdir(x) (abort (), 0) - have_working_fchdir = false; -#endif } if (!have_working_fchdir) |