1 /* chown() compatibility.
2 Copyright (C) 1998, 2000, 2002, 2003, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <kernel-features.h>
30 In Linux 2.1.x the chown functions have been changed. A new function lchown
31 was introduced. The new chown now follows symlinks - the old chown and the
32 new lchown do not follow symlinks.
33 This file emulates chown() under the old kernels.
37 __chown (const char *file
, uid_t owner
, gid_t group
)
39 #if __ASSUME_LCHOWN_SYSCALL
40 return INLINE_SYSCALL (chown
, 3, file
, owner
, group
);
44 char link
[PATH_MAX
+ 2];
45 char path
[2 * PATH_MAX
+ 4];
48 static int libc_old_chown
= 0 /* -1=old linux, 1=new linux, 0=unknown */;
50 if (libc_old_chown
== 1)
51 return INLINE_SYSCALL (chown
, 3, __ptrvalue (file
), owner
, group
);
56 if (libc_old_chown
== 0)
58 err
= INLINE_SYSCALL (chown
, 3, __ptrvalue (file
), owner
, group
);
59 if (err
!= -1 || errno
!= ENOSYS
)
68 err
= __readlink (file
, link
, PATH_MAX
+ 1);
71 __set_errno (old_errno
);
72 return __lchown (file
, owner
, group
);
75 filelen
= strlen (file
) + 1;
76 if (filelen
> sizeof (path
))
78 __set_errno (ENAMETOOLONG
);
81 memcpy (path
, file
, filelen
);
83 /* 'The system has an arbitrary limit...' In practise, we'll hit
84 ENAMETOOLONG before this, usually. */
85 for (loopct
= 0; loopct
< 128; ++loopct
)
89 if (err
>= PATH_MAX
+ 1)
91 __set_errno (ENAMETOOLONG
);
95 link
[err
] = 0; /* Null-terminate string, just-in-case. */
97 linklen
= strlen (link
) + 1;
100 memcpy (path
, link
, linklen
);
103 filelen
= strlen (path
);
105 while (filelen
> 1 && path
[filelen
- 1] == '/')
107 while (filelen
> 0 && path
[filelen
- 1] != '/')
109 if (filelen
+ linklen
> sizeof (path
))
111 errno
= ENAMETOOLONG
;
114 memcpy (path
+ filelen
, link
, linklen
);
117 err
= __readlink (path
, link
, PATH_MAX
+ 1);
121 __set_errno (old_errno
);
122 return __lchown (path
, owner
, group
);
129 libc_hidden_def (__chown
)
131 #include <shlib-compat.h>
132 versioned_symbol (libc
, __chown
, chown
, GLIBC_2_1
);