4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * chown [-fR] uid[.gid] file ...
48 #include <sys/types.h>
56 struct passwd
*getpwnam();
63 void fatal(int, char *, char *);
66 main(int argc
, char *argv
[])
73 extern char *strchr();
76 while (argc
> 0 && argv
[0][0] == '-') {
77 for (cp
= &argv
[0][1]; *cp
; cp
++)
92 fatal(255, "unknown option: %s", optchar
);
97 fprintf(stderr
, "usage: chown [-fR] owner[.group] file ...\n");
101 group
= strchr(argv
[0], '.');
104 if (!isnumber(group
)) {
105 if ((grp
= getgrnam(group
)) == NULL
)
106 fatal(255, "unknown group: %s", group
);
109 } else if (*group
!= '\0') {
111 gid
= (gid_t
)strtol(group
, NULL
, 10);
113 if (errno
== ERANGE
) {
115 "group id too large: %s", group
);
117 fatal(2, "group id invalid: %s", group
);
122 if (!isnumber(argv
[0])) {
123 if ((pwd
= getpwnam(argv
[0])) == NULL
)
124 fatal(255, "unknown user id: %s", argv
[0]);
128 uid
= (uid_t
)strtol(argv
[0], NULL
, 10);
130 if (errno
== ERANGE
) {
131 fatal(2, "user id too large: %s", argv
[0]);
133 fatal(2, "user id invalid: %s", argv
[0]);
137 for (c
= 1; c
< argc
; c
++) {
138 /* do stat for directory arguments */
139 if (lstat(argv
[c
], &stbuf
) < 0) {
140 status
+= Perror(argv
[c
]);
143 if (rflag
&& ((stbuf
.st_mode
&S_IFMT
) == S_IFDIR
)) {
144 status
+= chownr(argv
[c
], uid
, gid
);
147 if (lchown(argv
[c
], uid
, gid
)) {
148 status
+= Perror(argv
[c
]);
167 chownr(char *dir
, uid_t uid
, gid_t gid
)
175 if (getcwd(savedir
, 1024) == NULL
)
176 fatal(255, "%s", savedir
);
178 * Change what we are given before doing it's contents.
180 if (chown(dir
, uid
, gid
) < 0 && Perror(dir
))
182 if (chdir(dir
) < 0) {
186 if ((dirp
= opendir(".")) == NULL
) {
191 dp
= readdir(dirp
); /* read "." and ".." */
193 for (dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)) {
194 if (lstat(dp
->d_name
, &st
) < 0) {
195 ecode
= Perror(dp
->d_name
);
200 if ((st
.st_mode
&S_IFMT
) == S_IFDIR
) {
201 ecode
= chownr(dp
->d_name
, uid
, gid
);
206 if (lchown(dp
->d_name
, uid
, gid
) < 0 &&
207 (ecode
= Perror(dp
->d_name
)))
211 if (chdir(savedir
) < 0)
212 fatal(255, "can't change back to %s", savedir
);
217 error(char *fmt
, char *a
)
221 fprintf(stderr
, "chown: ");
222 fprintf(stderr
, fmt
, a
);
229 fatal(int status
, char *fmt
, char *a
)
233 (void) error(fmt
, a
);
242 fprintf(stderr
, "chown: ");