chown: detect Solaris and FreeBSD bug
[gnulib.git] / lib / lchown.c
1 /* Provide a stub lchown function for systems that lack it.
2
3    Copyright (C) 1998, 1999, 2002, 2004, 2006, 2007, 2009 Free
4    Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <sys/stat.h>
27
28 /* If the system chown does not follow symlinks, we don't want it
29    replaced by gnulib's chown, which does follow symlinks.  */
30 #if CHOWN_MODIFIES_SYMLINK
31 # undef chown
32 #endif
33
34 /* Work just like chown, except when FILE is a symbolic link.
35    In that case, set errno to EOPNOTSUPP and return -1.
36    But if autoconf tests determined that chown modifies
37    symlinks, then just call chown.  */
38
39 int
40 lchown (const char *file, uid_t uid, gid_t gid)
41 {
42 #if HAVE_CHOWN
43 # if ! CHOWN_MODIFIES_SYMLINK
44   struct stat stats;
45
46   if (lstat (file, &stats) == 0 && S_ISLNK (stats.st_mode))
47     {
48       errno = EOPNOTSUPP;
49       return -1;
50     }
51 # endif
52
53   return chown (file, uid, gid);
54
55 #else /* !HAVE_CHOWN */
56   errno = ENOSYS;
57   return -1;
58 #endif
59 }