Provide an alternative to exiting immediately upon save_cwd or
authorJim Meyering <jim@meyering.net>
Tue, 14 Jun 2005 06:54:39 +0000 (06:54 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 14 Jun 2005 06:54:39 +0000 (06:54 +0000)
restore_cwd failure.  Now, an application can arrange e.g.,
to perform a longjump in that case.
* openat.c: Include dirname.h.
Use IS_ABSOLUTE_FILE_NAME rather than testing for leading slash.
(rpl_openat, fdopendir, fstatat): Call openat_save_die
and openat_restore_die rather than calling error directly.
Don't include "error.h" or "exitfail.h"; they're no longer needed.

* openat-die.c (openat_save_die, openat_restore_die): New file.
* openat.h (openat_save_die, openat_restore_die): Declare and define.

lib/openat.c

index ec51b2d..495f4b2 100644 (file)
@@ -27,8 +27,7 @@
 #include <errno.h>
 #include <fcntl.h>
 
-#include "error.h"
-#include "exitfail.h"
+#include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
 #include "save-cwd.h"
 
 #include "gettext.h"
@@ -62,12 +61,11 @@ rpl_openat (int fd, char const *file, int flags, ...)
       va_end (arg);
     }
 
-  if (fd == AT_FDCWD || *file == '/')
+  if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
     return open (file, flags, mode);
 
   if (save_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("openat: unable to record current working directory"));
+    openat_save_die (errno);
 
   if (fchdir (fd) != 0)
     {
@@ -81,8 +79,7 @@ rpl_openat (int fd, char const *file, int flags, ...)
   saved_errno = errno;
 
   if (restore_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("openat: unable to restore working directory"));
+    openat_restore_die (errno);
 
   free_cwd (&saved_cwd);
 
@@ -108,8 +105,7 @@ fdopendir (int fd)
     return opendir (".");
 
   if (save_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("fdopendir: unable to record current working directory"));
+    openat_save_die (errno);
 
   if (fchdir (fd) != 0)
     {
@@ -123,8 +119,7 @@ fdopendir (int fd)
   saved_errno = errno;
 
   if (restore_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("fdopendir: unable to restore working directory"));
+    openat_restore_die (errno);
 
   free_cwd (&saved_cwd);
 
@@ -152,8 +147,7 @@ fstatat (int fd, char const *file, struct stat *st, int flag)
            : stat (file, st));
 
   if (save_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("fstatat: unable to record current working directory"));
+    openat_save_die (errno);
 
   if (fchdir (fd) != 0)
     {
@@ -169,8 +163,7 @@ fstatat (int fd, char const *file, struct stat *st, int flag)
   saved_errno = errno;
 
   if (restore_cwd (&saved_cwd) != 0)
-    error (exit_failure, errno,
-          _("fstatat: unable to restore working directory"));
+    openat_restore_die (errno);
 
   free_cwd (&saved_cwd);