(INT_STRLEN_BOUND, INT_BUFSIZE_BOUND):
[gnulib.git] / lib / chown.c
index 460e4f6..2b6b29d 100644 (file)
@@ -36,9 +36,6 @@
 # include <sys/file.h>
 #endif
 #include <errno.h>
-#ifndef errno
-extern int errno;
-#endif
 
 /* Provide a more-closely POSIX-conforming version of chown on
    systems with one or both of the following problems:
@@ -71,10 +68,11 @@ rpl_chown (const char *file, uid_t uid, gid_t gid)
     /* Handle the case in which the system-supplied chown function
        does *not* follow symlinks.  Instead, it changes permissions
        on the symlink itself.  To work around that, we open the
-       file (but this can fail due to lack of read permission) and
+       file (but this can fail due to lack of read or write permission) and
        use fchown on the resulting descriptor.  */
     int fd = open (file, O_RDONLY | O_NONBLOCK | O_NOCTTY);
-    if (fd == -1)
+    if (fd < 0
+       && (fd = open (file, O_WRONLY | O_NONBLOCK | O_NOCTTY)) < 0)
       return -1;
     if (fchown (fd, uid, gid))
       {