openat: work around AIX 7.1 fstatat issue
[gnulib.git] / lib / fstatat.c
index 1c6c2d3..d592d60 100644 (file)
@@ -1,6 +1,6 @@
 /* Work around an fstatat bug on Solaris 9.
 
-   Copyright (C) 2006, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2009-2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 /* Written by Paul Eggert and Jim Meyering.  */
 
+/* If the user's config.h happens to include <sys/stat.h>, let it include only
+   the system's <sys/stat.h> here, so that orig_fstatat doesn't recurse to
+   rpl_fstatat.  */
+#define __need_system_sys_stat_h
 #include <config.h>
 
+/* Get the original definition of fstatat.  It might be defined as a macro.  */
+#include <sys/stat.h>
+#undef __need_system_sys_stat_h
+
+#if HAVE_FSTATAT
+static inline int
+orig_fstatat (int fd, char const *filename, struct stat *buf, int flags)
+{
+  return fstatat (fd, filename, buf, flags);
+}
+#endif
+
 #include <sys/stat.h>
 
 #include <errno.h>
@@ -38,7 +54,7 @@
 int
 rpl_fstatat (int fd, char const *file, struct stat *st, int flag)
 {
-  int result = fstatat (fd, file, st, flag);
+  int result = orig_fstatat (fd, file, st, flag);
   size_t len;
 
   if (result != 0)
@@ -48,12 +64,12 @@ rpl_fstatat (int fd, char const *file, struct stat *st, int flag)
     {
       /* Fix lstat behavior.  */
       if (file[len - 1] != '/' || S_ISDIR (st->st_mode))
-       return 0;
+        return 0;
       if (!S_ISLNK (st->st_mode))
-       {
-         errno = ENOTDIR;
-         return -1;
-       }
+        {
+          errno = ENOTDIR;
+          return -1;
+        }
       result = fstatat (fd, file, st, flag & ~AT_SYMLINK_NOFOLLOW);
     }
   /* Fix stat behavior.  */