]> Savannah Git Hosting - gnulib.git/commitdiff
openat: use C99 decls after stmts
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 18 Nov 2024 15:58:03 +0000 (07:58 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 20 Nov 2024 19:23:32 +0000 (11:23 -0800)
* lib/openat.c (rpl_openat, openat_permissive):
Refactor to put decls closer to where they’re used.

ChangeLog
lib/openat.c

index 13f68f57ce26e53ab9daac29f45e777ed0d85618..11d0a0f67992014bbad6c7ce352b8a838174cf2c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-11-20  Paul Eggert  <eggert@cs.ucla.edu>
 
+       openat: use C99 decls after stmts
+       * lib/openat.c (rpl_openat, openat_permissive):
+       Refactor to put decls closer to where they’re used.
+
        openat: don’t lose track of save_cwd errno
        * lib/openat.c (openat_permissive): When ‘close’ is called
        after save_cwd failed, don’t lose track of save_cwd’s errno.
index c5d5d7d82787849e6382a6dedf696c6220958abd..9c8180c01347272a365be9b49f96c5856b023aec 100644 (file)
@@ -61,17 +61,8 @@ orig_openat (int fd, char const *filename, int flags, mode_t mode)
 int
 rpl_openat (int dfd, char const *filename, int flags, ...)
 {
-  /* 0 = unknown, 1 = yes, -1 = no.  */
-#if GNULIB_defined_O_CLOEXEC
-  int have_cloexec = -1;
-#else
-  static int have_cloexec;
-#endif
-
-  mode_t mode;
-  int fd;
+  mode_t mode = 0;
 
-  mode = 0;
   if (flags & O_CREAT)
     {
       va_list arg;
@@ -116,8 +107,15 @@ rpl_openat (int dfd, char const *filename, int flags, ...)
     }
 # endif
 
-  fd = orig_openat (dfd, filename,
-                    flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
+  /* 0 = unknown, 1 = yes, -1 = no.  */
+#if GNULIB_defined_O_CLOEXEC
+  int have_cloexec = -1;
+#else
+  static int have_cloexec;
+#endif
+
+  int fd = orig_openat (dfd, filename,
+                        flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
 
   if (flags & O_CLOEXEC)
     {
@@ -217,10 +215,6 @@ int
 openat_permissive (int fd, char const *file, int flags, mode_t mode,
                    int *cwd_errno)
 {
-  struct saved_cwd saved_cwd;
-  int saved_errno;
-  int err;
-
   if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
     return open (file, flags, mode);
 
@@ -244,6 +238,7 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode,
       }
   }
 
+  struct saved_cwd saved_cwd;
   int save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0;
 
   /* If save_cwd allocated a descriptor DFD other than FD, do another
@@ -273,8 +268,8 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode,
       *cwd_errno = save_failed;
     }
 
-  err = fchdir (fd);
-  saved_errno = errno;
+  int err = fchdir (fd);
+  int saved_errno = errno;
 
   if (! err)
     {