Use a consistent style for including <config.h>.
[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, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* written by Jim Meyering */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 /* Disable the definition of chown to rpl_chown (from config.h) in this
26    file.  Otherwise, we'd get conflicting prototypes for rpl_chown on
27    most systems.  */
28 #undef chown
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include <fcntl.h>
36 #include <errno.h>
37
38 /* Provide a more-closely POSIX-conforming version of chown on
39    systems with one or both of the following problems:
40    - chown doesn't treat an ID of -1 as meaning
41    `don't change the corresponding ID'.
42    - chown doesn't dereference symlinks.  */
43
44 int
45 rpl_chown (const char *file, uid_t uid, gid_t gid)
46 {
47 #if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
48   if (gid == (gid_t) -1 || uid == (uid_t) -1)
49     {
50       struct stat file_stats;
51
52       /* Stat file to get id(s) that should remain unchanged.  */
53       if (stat (file, &file_stats))
54         return -1;
55
56       if (gid == (gid_t) -1)
57         gid = file_stats.st_gid;
58
59       if (uid == (uid_t) -1)
60         uid = file_stats.st_uid;
61     }
62 #endif
63
64 #if CHOWN_MODIFIES_SYMLINK
65   {
66     /* Handle the case in which the system-supplied chown function
67        does *not* follow symlinks.  Instead, it changes permissions
68        on the symlink itself.  To work around that, we open the
69        file (but this can fail due to lack of read or write permission) and
70        use fchown on the resulting descriptor.  */
71     int fd = open (file, O_RDONLY | O_NONBLOCK | O_NOCTTY);
72     if (fd < 0
73         && (fd = open (file, O_WRONLY | O_NONBLOCK | O_NOCTTY)) < 0)
74       return -1;
75     if (fchown (fd, uid, gid))
76       {
77         int saved_errno = errno;
78         close (fd);
79         errno = saved_errno;
80         return -1;
81       }
82     return close (fd);
83   }
84 #else
85   return chown (file, uid, gid);
86 #endif
87 }