Merge from coreutils CVS.
[gnulib.git] / lib / chown.c
1 /* provide consistent interface to chown for systems that don't interpret
2    an ID of -1 as meaning `don't change the corresponding ID'.
3    Copyright (C) 1997, 2004 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 /* Disable the definition of chown to rpl_chown (from config.h) in this
24    file.  Otherwise, we'd get conflicting prototypes for rpl_chown on
25    most systems.  */
26 #undef chown
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #if HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #if HAVE_FCNTL_H
34 # include <fcntl.h>
35 #else
36 # include <sys/file.h>
37 #endif
38 #include <errno.h>
39 #ifndef errno
40 extern int errno;
41 #endif
42
43 /* Provide a more-closely POSIX-conforming version of chown on
44    systems with one or both of the following problems:
45    - chown doesn't treat an ID of -1 as meaning
46    `don't change the corresponding ID'.
47    - chown doesn't dereference symlinks.  */
48
49 int
50 rpl_chown (const char *file, uid_t uid, gid_t gid)
51 {
52 #if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
53   if (gid == (gid_t) -1 || uid == (uid_t) -1)
54     {
55       struct stat file_stats;
56
57       /* Stat file to get id(s) that should remain unchanged.  */
58       if (stat (file, &file_stats))
59         return 1;
60
61       if (gid == (gid_t) -1)
62         gid = file_stats.st_gid;
63
64       if (uid == (uid_t) -1)
65         uid = file_stats.st_uid;
66     }
67 #endif
68
69 #if CHOWN_MODIFIES_SYMLINK
70   {
71     /* Handle the case in which the system-supplied chown function
72        does *not* follow symlinks.  Instead, it changes permissions
73        on the symlink itself.  To work around that, we open the
74        file (but this can fail due to lack of read permission) and
75        use fchown on the resulting descriptor.  */
76     int fd = open (file, O_RDONLY | O_NONBLOCK | O_NOCTTY);
77     if (fd == -1)
78       return -1;
79     if (fchown (fd, uid, gid))
80       {
81         int saved_errno = errno;
82         close (fd);
83         errno = saved_errno;
84         return -1;
85       }
86     return close (fd);
87   }
88 #else
89   return chown (file, uid, gid);
90 #endif
91 }