bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

save-cwd.c minor cleanup (plus Irix port)


From: Paul Eggert
Subject: save-cwd.c minor cleanup (plus Irix port)
Date: Thu, 02 Dec 2004 22:41:17 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this minor cleanup for save-cwd.  The only real fix is that
the Irix port now checks for fchdir failures properly.

2004-12-02  Paul Eggert  <address@hidden>

        * 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.

*** lib/save-cwd.c      30 Nov 2004 14:46:25 -0000      1.22
--- lib/save-cwd.c      3 Dec 2004 06:22:04 -0000
***************
*** 21,26 ****
--- 21,28 ----
  # include "config.h"
  #endif
  
+ #include "save-cwd.h"
+ 
  #include <stdbool.h>
  #include <stdio.h>
  #include <stdlib.h>
***************
*** 37,48 ****
  
  #include <errno.h>
  
- #ifndef O_DIRECTORY
- # define O_DIRECTORY 0
- #endif
- 
- #include "save-cwd.h"
- 
  #include "chdir-long.h"
  #include "xgetcwd.h"
  
--- 39,44 ----
***************
*** 66,113 ****
  int
  save_cwd (struct saved_cwd *cwd)
  {
    static bool have_working_fchdir = true;
  
    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);
        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 (errno == EINVAL)
!           {
!             close (cwd->desc);
!             cwd->desc = -1;
!             have_working_fchdir = false;
!           }
!         else
            {
-             int saved_errno = errno;
-             close (cwd->desc);
-             cwd->desc = -1;
              errno = saved_errno;
              return -1;
            }
        }
- # endif /* __sun__ || sun */
- #else
- # define fchdir(x) (abort (), 0)
-       have_working_fchdir = false;
- #endif
      }
  
    if (!have_working_fchdir)
--- 62,110 ----
  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)
      {
!       cwd->desc = open (".", O_RDONLY);
        if (cwd->desc < 0)
        {
!         cwd->desc = open (".", O_WRONLY);
!         if (cwd->desc < 0)
!           {
!             cwd->name = xgetcwd ();
!             return cwd->name ? 0 : -1;
!           }
        }
  
        /* On SunOS 4 and IRIX 5.3, fchdir returns EINVAL when auditing
         is enabled, so we have to fall back to chdir.  */
!       if (fchdir_needs_testing && fchdir (cwd->desc) != 0)
        {
!         int saved_errno = errno;
!         close (cwd->desc);
!         cwd->desc = -1;
!         if (saved_errno != EINVAL)
            {
              errno = saved_errno;
              return -1;
            }
+         have_working_fchdir = false;
        }
      }
  
    if (!have_working_fchdir)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]