From d2117d918c29227722f3345cbc66d8c9aca71dac Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 3 Dec 2004 06:43:59 +0000 Subject: Include "openat.h" before other include files. 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 | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'lib/openat.c') diff --git a/lib/openat.c b/lib/openat.c index 69370ce2c..26e280d78 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -19,10 +19,7 @@ #include -/* 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 #include @@ -30,9 +27,8 @@ #include #include -#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; } -- cgit v1.2.3-54-g00ecf