From b52d021b6ca137d0a9f5d6c4e9888674f37acba8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 18 Nov 2024 07:58:03 -0800 Subject: [PATCH] openat: use C99 decls after stmts MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/openat.c (rpl_openat, openat_permissive): Refactor to put decls closer to where they’re used. --- ChangeLog | 4 ++++ lib/openat.c | 31 +++++++++++++------------------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 13f68f57ce..11d0a0f679 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-11-20 Paul Eggert + 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. diff --git a/lib/openat.c b/lib/openat.c index c5d5d7d827..9c8180c013 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -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) { -- 2.39.5