(restricted_chown):
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 27 Dec 2005 07:59:00 +0000 (07:59 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 27 Dec 2005 07:59:00 +0000 (07:59 +0000)
Don't try O_WRONLY unless O_RDONLY failed wth EACCES.

src/chown-core.c

index 562b175..e691023 100644 (file)
@@ -191,15 +191,13 @@ restricted_chown (char const *file,
 {
   enum RCH_status status = RC_ok;
   struct stat st;
-  int o_flags = (O_NONBLOCK | O_NOCTTY);
+  int open_flags = O_NONBLOCK | O_NOCTTY;
 
-  int fd = open (file, O_RDONLY | o_flags);
-  if (fd < 0)
-    {
-      fd = open (file, O_WRONLY | o_flags);
-      if (fd < 0)
-       return RC_error;
-    }
+  int fd = open (file, O_RDONLY | open_flags);
+  if (! (0 <= fd
+        || (errno == EACCES
+            && 0 <= (fd = open (file, O_WRONLY | open_flags)))))
+    return RC_error;
 
   if (fstat (fd, &st) != 0)
     {