]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Include "openat.h" before other include files.
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 3 Dec 2004 06:43:59 +0000 (06:43 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 3 Dec 2004 06:43:59 +0000 (06:43 +0000)
Include "exitfail.h".
(openat): Remove #undef; no longer needed now that we include openat.h first.
(rpl_openat): Add comment about mode_t promotion.  Simplify
code a bit by moving initializations around.  Use exit_failure
rather than EXIT_FAILURE.

lib/openat.c

index 69370ce2c8f0564f844add9e3b3a97d738b3e254..26e280d786a760674a444e0c2b714f863677af67 100644 (file)
 
 #include <config.h>
 
-/* Disable the definition of openat to rpl_openat (from config.h) in this
-   file.  Otherwise, we'd get conflicting prototypes for rpl_openat on
-   most systems.  */
-#undef openat
+#include "openat.h"
 
 #include <stdlib.h>
 #include <stdarg.h>
@@ -30,9 +27,8 @@
 #include <errno.h>
 #include <fcntl.h>
 
-#include "openat.h"
-
 #include "error.h"
+#include "exitfail.h"
 #include "save-cwd.h"
 
 #include "gettext.h"
@@ -46,42 +42,44 @@ int
 rpl_openat (int fd, char const *filename, int flags, ...)
 {
   struct saved_cwd saved_cwd;
-  int saved_errno = 0;
+  int saved_errno;
   int new_fd;
-  mode_t mode;
+  mode_t mode = 0;
 
   if (flags & O_CREAT)
     {
       va_list arg;
       va_start (arg, flags);
+
+      /* Assume that mode_t is passed compatibly with mode_t's type
+        after argument promotion.  */
       mode = va_arg (arg, mode_t);
+
       va_end (arg);
     }
-  else
-    {
-      mode = 0;
-    }
 
   if (fd == AT_FDCWD || *filename == '/')
     return open (filename, flags, mode);
 
   if (save_cwd (&saved_cwd) != 0)
-    error (EXIT_FAILURE, errno,
+    error (exit_failure, errno,
           _("openat: unable to record current working directory"));
+
   if (fchdir (fd) != 0)
     {
+      saved_errno = errno;
       free_cwd (&saved_cwd);
+      errno = saved_errno;
       return -1;
     }
 
   new_fd = open (filename, flags, mode);
-  if (new_fd < 0)
-    saved_errno = errno;
+  saved_errno = errno;
 
   if (restore_cwd (&saved_cwd) != 0)
-    error (EXIT_FAILURE, errno,
+    error (exit_failure, errno,
           _("openat: unable to restore working directory"));
-  errno = saved_errno;
 
+  errno = saved_errno;
   return new_fd;
 }