xref: /titanic_52/usr/src/cmd/chgrp/chgrp.c (revision bda8495c343003ad268b467cd347f95f25eeffc4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*bda8495cSas145665  * Common Development and Distribution License (the "License").
6*bda8495cSas145665  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*bda8495cSas145665  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * chgrp [-fhR] gid file ...
457c478bd9Sstevel@tonic-gate  * chgrp -R [-f] [-H|-L|-P] gid file ...
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/avl.h>
537c478bd9Sstevel@tonic-gate #include <grp.h>
547c478bd9Sstevel@tonic-gate #include <dirent.h>
557c478bd9Sstevel@tonic-gate #include <unistd.h>
567c478bd9Sstevel@tonic-gate #include <stdlib.h>
577c478bd9Sstevel@tonic-gate #include <locale.h>
587c478bd9Sstevel@tonic-gate #include <libcmdutils.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <strings.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static struct group	*gr;
637c478bd9Sstevel@tonic-gate static struct stat	stbuf;
647c478bd9Sstevel@tonic-gate static struct stat	stbuf2;
657c478bd9Sstevel@tonic-gate static gid_t		gid;
667c478bd9Sstevel@tonic-gate static int		hflag = 0,
677c478bd9Sstevel@tonic-gate 			fflag = 0,
687c478bd9Sstevel@tonic-gate 			rflag = 0,
697c478bd9Sstevel@tonic-gate 			Hflag = 0,
707c478bd9Sstevel@tonic-gate 			Lflag = 0,
717c478bd9Sstevel@tonic-gate 			Pflag = 0;
727c478bd9Sstevel@tonic-gate static int		status = 0;	/* total number of errors received */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static avl_tree_t	*tree;		/* search tree to store inode data */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static void usage(void);
777c478bd9Sstevel@tonic-gate static int isnumber(char *);
787c478bd9Sstevel@tonic-gate static int Perror(char *);
797c478bd9Sstevel@tonic-gate static void chgrpr(char *, gid_t);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #ifdef XPG4
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
847c478bd9Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
857c478bd9Sstevel@tonic-gate  * specified, so we are just looking to see if -R -L, or -R -H was specified,
867c478bd9Sstevel@tonic-gate  * or, since -R has the same behavior as -R -L, if -R was specified by itself.
877c478bd9Sstevel@tonic-gate  * Therefore, all we really need to check for is if -R was specified.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag)
907c478bd9Sstevel@tonic-gate #else
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
937c478bd9Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
947c478bd9Sstevel@tonic-gate  * specified, so we are just looking to see if -R -L, or -R -H was specified.
957c478bd9Sstevel@tonic-gate  * Note: -R by itself will change the group of a directory referenced by a
967c478bd9Sstevel@tonic-gate  * symlink however it will not follow the symlink to any other part of the
977c478bd9Sstevel@tonic-gate  * file hierarchy.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag && (Hflag || Lflag))
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #ifdef XPG4
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Since -R behaves the
1057c478bd9Sstevel@tonic-gate  * same as -R -L, we always want to follow symlinks to other parts
1067c478bd9Sstevel@tonic-gate  * of the file hierarchy unless -H was specified.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(!Hflag)
1097c478bd9Sstevel@tonic-gate #else
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Only follow symlinks
1127c478bd9Sstevel@tonic-gate  * to other parts of the file hierarchy if -L was specified.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(Lflag)
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	CHOWN(f, u, g)	if (chown(f, u, g) < 0) { \
1187c478bd9Sstevel@tonic-gate 				status += Perror(f); \
1197c478bd9Sstevel@tonic-gate 			}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	LCHOWN(f, u, g)	if (lchown(f, u, g) < 0) { \
1227c478bd9Sstevel@tonic-gate 				status += Perror(f); \
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * We're ignoring errors here because preserving the SET[UG]ID bits is just
1267c478bd9Sstevel@tonic-gate  * a courtesy.  This is only used on directories.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate #define	SETUGID_PRESERVE(dir, mode) \
1297c478bd9Sstevel@tonic-gate 	if (((mode) & (S_ISGID|S_ISUID)) != 0) \
1307c478bd9Sstevel@tonic-gate 		(void) chmod((dir), (mode) & ~S_IFMT)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate extern int		optind;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	int		c;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* set the locale for only the messages system (all else is clean) */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1437c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1447c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "RhfHLP")) != EOF)
1497c478bd9Sstevel@tonic-gate 		switch (c) {
1507c478bd9Sstevel@tonic-gate 			case 'R':
1517c478bd9Sstevel@tonic-gate 				rflag++;
1527c478bd9Sstevel@tonic-gate 				break;
1537c478bd9Sstevel@tonic-gate 			case 'h':
1547c478bd9Sstevel@tonic-gate 				hflag++;
1557c478bd9Sstevel@tonic-gate 				break;
1567c478bd9Sstevel@tonic-gate 			case 'f':
1577c478bd9Sstevel@tonic-gate 				fflag++;
1587c478bd9Sstevel@tonic-gate 				break;
1597c478bd9Sstevel@tonic-gate 			case 'H':
1607c478bd9Sstevel@tonic-gate 				/*
1617c478bd9Sstevel@tonic-gate 				 * If more than one of -H, -L, and -P
1627c478bd9Sstevel@tonic-gate 				 * are specified, only the last option
1637c478bd9Sstevel@tonic-gate 				 * specified determines the behavior of
1647c478bd9Sstevel@tonic-gate 				 * chgrp.  In addition, make [-H|-L]
1657c478bd9Sstevel@tonic-gate 				 * mutually exclusive of -h.
1667c478bd9Sstevel@tonic-gate 				 */
1677c478bd9Sstevel@tonic-gate 				Lflag = Pflag = 0;
1687c478bd9Sstevel@tonic-gate 				Hflag++;
1697c478bd9Sstevel@tonic-gate 				break;
1707c478bd9Sstevel@tonic-gate 			case 'L':
1717c478bd9Sstevel@tonic-gate 				Hflag = Pflag = 0;
1727c478bd9Sstevel@tonic-gate 				Lflag++;
1737c478bd9Sstevel@tonic-gate 				break;
1747c478bd9Sstevel@tonic-gate 			case 'P':
1757c478bd9Sstevel@tonic-gate 				Hflag = Lflag = 0;
1767c478bd9Sstevel@tonic-gate 				Pflag++;
1777c478bd9Sstevel@tonic-gate 				break;
1787c478bd9Sstevel@tonic-gate 			default:
1797c478bd9Sstevel@tonic-gate 				usage();
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
1847c478bd9Sstevel@tonic-gate 	 * or a usage error.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	argc -= optind;
1877c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if ((argc < 2) ||
1907c478bd9Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && !rflag) ||
1917c478bd9Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && hflag)) {
1927c478bd9Sstevel@tonic-gate 		usage();
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
195*bda8495cSas145665 	if ((gr = getgrnam(argv[0])) != NULL) {
196*bda8495cSas145665 		gid = gr->gr_gid;
197*bda8495cSas145665 	} else {
1987c478bd9Sstevel@tonic-gate 		if (isnumber(argv[0])) {
1997c478bd9Sstevel@tonic-gate 			errno = 0;
200*bda8495cSas145665 			/* gid is an int */
201*bda8495cSas145665 			gid = (gid_t)strtol(argv[0], NULL, 10);
2027c478bd9Sstevel@tonic-gate 			if (errno != 0) {
2037c478bd9Sstevel@tonic-gate 				if (errno == ERANGE) {
2047c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2057c478bd9Sstevel@tonic-gate 					"chgrp: group id is too large\n"));
2067c478bd9Sstevel@tonic-gate 					exit(2);
2077c478bd9Sstevel@tonic-gate 				} else {
2087c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2097c478bd9Sstevel@tonic-gate 					"chgrp: invalid group id\n"));
2107c478bd9Sstevel@tonic-gate 					exit(2);
2117c478bd9Sstevel@tonic-gate 				}
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 		} else {
2147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "chgrp: ");
2157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("unknown group: %s\n"),
2167c478bd9Sstevel@tonic-gate 				argv[0]);
2177c478bd9Sstevel@tonic-gate 			exit(2);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
2227c478bd9Sstevel@tonic-gate 		tree = NULL;
2237c478bd9Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
2247c478bd9Sstevel@tonic-gate 			status += Perror(argv[c]);
2257c478bd9Sstevel@tonic-gate 			continue;
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
2287c478bd9Sstevel@tonic-gate 			if (hflag || Pflag) {
2297c478bd9Sstevel@tonic-gate 				/*
2307c478bd9Sstevel@tonic-gate 				 * Change the group id of the symbolic link
2317c478bd9Sstevel@tonic-gate 				 * specified on the command line.
2327c478bd9Sstevel@tonic-gate 				 * Don't follow the symbolic link to
2337c478bd9Sstevel@tonic-gate 				 * any other part of the file hierarchy.
2347c478bd9Sstevel@tonic-gate 				 */
2357c478bd9Sstevel@tonic-gate 				LCHOWN(argv[c], -1, gid);
2367c478bd9Sstevel@tonic-gate 			} else {
2377c478bd9Sstevel@tonic-gate 				if (stat(argv[c], &stbuf2) < 0) {
2387c478bd9Sstevel@tonic-gate 					status += Perror(argv[c]);
2397c478bd9Sstevel@tonic-gate 					continue;
2407c478bd9Sstevel@tonic-gate 				}
2417c478bd9Sstevel@tonic-gate 				/*
2427c478bd9Sstevel@tonic-gate 				 * We know that we are to change the
2437c478bd9Sstevel@tonic-gate 				 * group of the file referenced by the
2447c478bd9Sstevel@tonic-gate 				 * symlink specified on the command line.
2457c478bd9Sstevel@tonic-gate 				 * Now check to see if we are to follow
2467c478bd9Sstevel@tonic-gate 				 * the symlink to any other part of the
2477c478bd9Sstevel@tonic-gate 				 * file hierarchy.
2487c478bd9Sstevel@tonic-gate 				 */
2497c478bd9Sstevel@tonic-gate 				if (FOLLOW_CL_LINKS) {
2507c478bd9Sstevel@tonic-gate 					if ((stbuf2.st_mode & S_IFMT)
2517c478bd9Sstevel@tonic-gate 					    == S_IFDIR) {
2527c478bd9Sstevel@tonic-gate 						/*
2537c478bd9Sstevel@tonic-gate 						 * We are following symlinks so
2547c478bd9Sstevel@tonic-gate 						 * traverse into the directory.
2557c478bd9Sstevel@tonic-gate 						 * Add this node to the search
2567c478bd9Sstevel@tonic-gate 						 * tree so we don't get into an
2577c478bd9Sstevel@tonic-gate 						 * endless loop.
2587c478bd9Sstevel@tonic-gate 						 */
2597c478bd9Sstevel@tonic-gate 						if (add_tnode(&tree,
2607c478bd9Sstevel@tonic-gate 						    stbuf2.st_dev,
2617c478bd9Sstevel@tonic-gate 						    stbuf2.st_ino) == 1) {
2627c478bd9Sstevel@tonic-gate 							chgrpr(argv[c], gid);
2637c478bd9Sstevel@tonic-gate 							/*
2647c478bd9Sstevel@tonic-gate 							 * Try to restore the
2657c478bd9Sstevel@tonic-gate 							 * SET[UG]ID bits.
2667c478bd9Sstevel@tonic-gate 							 */
2677c478bd9Sstevel@tonic-gate 							SETUGID_PRESERVE(
2687c478bd9Sstevel@tonic-gate 							    argv[c],
2697c478bd9Sstevel@tonic-gate 							    stbuf2.st_mode &
2707c478bd9Sstevel@tonic-gate 							    ~S_IFMT);
2717c478bd9Sstevel@tonic-gate 						} else {
2727c478bd9Sstevel@tonic-gate 							/*
2737c478bd9Sstevel@tonic-gate 							 * Error occurred.
2747c478bd9Sstevel@tonic-gate 							 * rc can't be 0
2757c478bd9Sstevel@tonic-gate 							 * as this is the first
2767c478bd9Sstevel@tonic-gate 							 * node to be added to
2777c478bd9Sstevel@tonic-gate 							 * the search tree.
2787c478bd9Sstevel@tonic-gate 							 */
2797c478bd9Sstevel@tonic-gate 							status += Perror(
2807c478bd9Sstevel@tonic-gate 							    argv[c]);
2817c478bd9Sstevel@tonic-gate 						}
2827c478bd9Sstevel@tonic-gate 					} else {
2837c478bd9Sstevel@tonic-gate 						/*
2847c478bd9Sstevel@tonic-gate 						 * Change the group id of the
2857c478bd9Sstevel@tonic-gate 						 * file referenced by the
2867c478bd9Sstevel@tonic-gate 						 * symbolic link.
2877c478bd9Sstevel@tonic-gate 						 */
2887c478bd9Sstevel@tonic-gate 						CHOWN(argv[c], -1, gid);
2897c478bd9Sstevel@tonic-gate 					}
2907c478bd9Sstevel@tonic-gate 				} else {
2917c478bd9Sstevel@tonic-gate 					/*
2927c478bd9Sstevel@tonic-gate 					 * Change the group id of the file
2937c478bd9Sstevel@tonic-gate 					 * referenced by the symbolic link.
2947c478bd9Sstevel@tonic-gate 					 */
2957c478bd9Sstevel@tonic-gate 					CHOWN(argv[c], -1, gid);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 					if ((stbuf2.st_mode & S_IFMT)
2987c478bd9Sstevel@tonic-gate 					    == S_IFDIR) {
2997c478bd9Sstevel@tonic-gate 						/* Reset the SET[UG]ID bits. */
3007c478bd9Sstevel@tonic-gate 						SETUGID_PRESERVE(argv[c],
3017c478bd9Sstevel@tonic-gate 						    stbuf2.st_mode & ~S_IFMT);
3027c478bd9Sstevel@tonic-gate 					}
3037c478bd9Sstevel@tonic-gate 				}
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 		} else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
3067c478bd9Sstevel@tonic-gate 			/*
3077c478bd9Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
3087c478bd9Sstevel@tonic-gate 			 * get into a endless loop.
3097c478bd9Sstevel@tonic-gate 			 */
3107c478bd9Sstevel@tonic-gate 			if (add_tnode(&tree, stbuf.st_dev,
3117c478bd9Sstevel@tonic-gate 			    stbuf.st_ino) == 1) {
3127c478bd9Sstevel@tonic-gate 				chgrpr(argv[c], gid);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 				/* Restore the SET[UG]ID bits. */
3157c478bd9Sstevel@tonic-gate 				SETUGID_PRESERVE(argv[c],
3167c478bd9Sstevel@tonic-gate 				    stbuf.st_mode & ~S_IFMT);
3177c478bd9Sstevel@tonic-gate 			} else {
3187c478bd9Sstevel@tonic-gate 				/*
3197c478bd9Sstevel@tonic-gate 				 * An error occurred while trying
3207c478bd9Sstevel@tonic-gate 				 * to add the node to the tree.
3217c478bd9Sstevel@tonic-gate 				 * Continue on with next file
3227c478bd9Sstevel@tonic-gate 				 * specified.  Note: rc shouldn't
3237c478bd9Sstevel@tonic-gate 				 * be 0 as this was the first node
3247c478bd9Sstevel@tonic-gate 				 * being added to the search tree.
3257c478bd9Sstevel@tonic-gate 				 */
3267c478bd9Sstevel@tonic-gate 				status += Perror(argv[c]);
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 		} else {
3297c478bd9Sstevel@tonic-gate 			if (hflag || Pflag) {
3307c478bd9Sstevel@tonic-gate 				LCHOWN(argv[c], -1, gid);
3317c478bd9Sstevel@tonic-gate 			} else {
3327c478bd9Sstevel@tonic-gate 				CHOWN(argv[c], -1, gid);
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 			/* If a directory, reset the SET[UG]ID bits. */
3357c478bd9Sstevel@tonic-gate 			if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3367c478bd9Sstevel@tonic-gate 				SETUGID_PRESERVE(argv[c],
3377c478bd9Sstevel@tonic-gate 				    stbuf.st_mode & ~S_IFMT);
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	return (status);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * chgrpr() - recursive chown()
3467c478bd9Sstevel@tonic-gate  *
3477c478bd9Sstevel@tonic-gate  * Recursively chowns the input directory then its contents.  rflag must
3487c478bd9Sstevel@tonic-gate  * have been set if chgrpr() is called.  The input directory should not
3497c478bd9Sstevel@tonic-gate  * be a sym link (this is handled in the calling routine).  In
3507c478bd9Sstevel@tonic-gate  * addition, the calling routine should have already added the input
3517c478bd9Sstevel@tonic-gate  * directory to the search tree so we do not get into endless loops.
3527c478bd9Sstevel@tonic-gate  * Note: chgrpr() doesn't need a return value as errors are reported
3537c478bd9Sstevel@tonic-gate  * through the global "status" variable.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static void
3567c478bd9Sstevel@tonic-gate chgrpr(char *dir, gid_t gid)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	struct dirent *dp;
3597c478bd9Sstevel@tonic-gate 	DIR *dirp;
3607c478bd9Sstevel@tonic-gate 	struct stat st, st2;
3617c478bd9Sstevel@tonic-gate 	char savedir[1024];
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == 0) {
3647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
3657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), savedir);
3667c478bd9Sstevel@tonic-gate 		exit(255);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Attempt to chown the directory, however don't return if we
3717c478bd9Sstevel@tonic-gate 	 * can't as we still may be able to chown the contents of the
3727c478bd9Sstevel@tonic-gate 	 * directory.  Note: the calling routine resets the SUID bits
3737c478bd9Sstevel@tonic-gate 	 * on this directory so we don't have to perform an extra 'stat'.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	CHOWN(dir, -1, gid);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
3787c478bd9Sstevel@tonic-gate 		status += Perror(dir);
3797c478bd9Sstevel@tonic-gate 		return;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
3827c478bd9Sstevel@tonic-gate 		status += Perror(dir);
3837c478bd9Sstevel@tonic-gate 		return;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
3867c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
3877c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0)) {
3887c478bd9Sstevel@tonic-gate 			continue;	/* skip "." and ".." */
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
3917c478bd9Sstevel@tonic-gate 			status += Perror(dp->d_name);
3927c478bd9Sstevel@tonic-gate 			continue;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		if ((st.st_mode & S_IFMT) == S_IFLNK) {
3957c478bd9Sstevel@tonic-gate 			if (hflag || Pflag) {
3967c478bd9Sstevel@tonic-gate 				/*
3977c478bd9Sstevel@tonic-gate 				 * Change the group id of the symbolic link
3987c478bd9Sstevel@tonic-gate 				 * encountered while traversing the
3997c478bd9Sstevel@tonic-gate 				 * directory.  Don't follow the symbolic
4007c478bd9Sstevel@tonic-gate 				 * link to any other part of the file
4017c478bd9Sstevel@tonic-gate 				 * hierarchy.
4027c478bd9Sstevel@tonic-gate 				 */
4037c478bd9Sstevel@tonic-gate 				LCHOWN(dp->d_name, -1, gid);
4047c478bd9Sstevel@tonic-gate 			} else {
4057c478bd9Sstevel@tonic-gate 				if (stat(dp->d_name, &st2) < 0) {
4067c478bd9Sstevel@tonic-gate 					status += Perror(dp->d_name);
4077c478bd9Sstevel@tonic-gate 					continue;
4087c478bd9Sstevel@tonic-gate 				}
4097c478bd9Sstevel@tonic-gate 				/*
4107c478bd9Sstevel@tonic-gate 				 * We know that we are to change the
4117c478bd9Sstevel@tonic-gate 				 * group of the file referenced by the
4127c478bd9Sstevel@tonic-gate 				 * symlink encountered while traversing
4137c478bd9Sstevel@tonic-gate 				 * the directory.  Now check to see if we
4147c478bd9Sstevel@tonic-gate 				 * are to follow the symlink to any other
4157c478bd9Sstevel@tonic-gate 				 * part of the file hierarchy.
4167c478bd9Sstevel@tonic-gate 				 */
4177c478bd9Sstevel@tonic-gate 				if (FOLLOW_D_LINKS) {
4187c478bd9Sstevel@tonic-gate 					if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4197c478bd9Sstevel@tonic-gate 						/*
4207c478bd9Sstevel@tonic-gate 						 * We are following symlinks so
4217c478bd9Sstevel@tonic-gate 						 * traverse into the directory.
4227c478bd9Sstevel@tonic-gate 						 * Add this node to the search
4237c478bd9Sstevel@tonic-gate 						 * tree so we don't get into an
4247c478bd9Sstevel@tonic-gate 						 * endless loop.
4257c478bd9Sstevel@tonic-gate 						 */
4267c478bd9Sstevel@tonic-gate 						int rc;
4277c478bd9Sstevel@tonic-gate 						if ((rc = add_tnode(&tree,
4287c478bd9Sstevel@tonic-gate 						    st2.st_dev,
4297c478bd9Sstevel@tonic-gate 						    st2.st_ino)) == 1) {
4307c478bd9Sstevel@tonic-gate 							chgrpr(dp->d_name, gid);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 							/*
4337c478bd9Sstevel@tonic-gate 							 * Restore SET[UG]ID
4347c478bd9Sstevel@tonic-gate 							 * bits.
4357c478bd9Sstevel@tonic-gate 							 */
4367c478bd9Sstevel@tonic-gate 							SETUGID_PRESERVE(
4377c478bd9Sstevel@tonic-gate 							    dp->d_name,
4387c478bd9Sstevel@tonic-gate 							    st2.st_mode &
4397c478bd9Sstevel@tonic-gate 							    ~S_IFMT);
4407c478bd9Sstevel@tonic-gate 						} else if (rc == 0) {
4417c478bd9Sstevel@tonic-gate 							/* already visited */
4427c478bd9Sstevel@tonic-gate 							continue;
4437c478bd9Sstevel@tonic-gate 						} else {
4447c478bd9Sstevel@tonic-gate 							/*
4457c478bd9Sstevel@tonic-gate 							 * An error occurred
4467c478bd9Sstevel@tonic-gate 							 * while trying to add
4477c478bd9Sstevel@tonic-gate 							 * the node to the tree.
4487c478bd9Sstevel@tonic-gate 							 */
4497c478bd9Sstevel@tonic-gate 							status += Perror(
4507c478bd9Sstevel@tonic-gate 							    dp->d_name);
4517c478bd9Sstevel@tonic-gate 							continue;
4527c478bd9Sstevel@tonic-gate 						}
4537c478bd9Sstevel@tonic-gate 					} else {
4547c478bd9Sstevel@tonic-gate 						/*
4557c478bd9Sstevel@tonic-gate 						 * Change the group id of the
4567c478bd9Sstevel@tonic-gate 						 * file referenced by the
4577c478bd9Sstevel@tonic-gate 						 * symbolic link.
4587c478bd9Sstevel@tonic-gate 						 */
4597c478bd9Sstevel@tonic-gate 						CHOWN(dp->d_name, -1, gid);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 					}
4627c478bd9Sstevel@tonic-gate 				} else {
4637c478bd9Sstevel@tonic-gate 					/*
4647c478bd9Sstevel@tonic-gate 					 * Change the group id of the file
4657c478bd9Sstevel@tonic-gate 					 * referenced by the symbolic link.
4667c478bd9Sstevel@tonic-gate 					 */
4677c478bd9Sstevel@tonic-gate 					CHOWN(dp->d_name, -1, gid);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 					if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4707c478bd9Sstevel@tonic-gate 						/* Restore SET[UG]ID bits. */
4717c478bd9Sstevel@tonic-gate 						SETUGID_PRESERVE(dp->d_name,
4727c478bd9Sstevel@tonic-gate 						    st2.st_mode & ~S_IFMT);
4737c478bd9Sstevel@tonic-gate 					}
4747c478bd9Sstevel@tonic-gate 				}
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 		} else if ((st.st_mode & S_IFMT) == S_IFDIR) {
4777c478bd9Sstevel@tonic-gate 			/*
4787c478bd9Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
4797c478bd9Sstevel@tonic-gate 			 * get into a endless loop.
4807c478bd9Sstevel@tonic-gate 			 */
4817c478bd9Sstevel@tonic-gate 			int rc;
4827c478bd9Sstevel@tonic-gate 			if ((rc = add_tnode(&tree, st.st_dev,
4837c478bd9Sstevel@tonic-gate 			    st.st_ino)) == 1) {
4847c478bd9Sstevel@tonic-gate 				chgrpr(dp->d_name, gid);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 				/* Restore the SET[UG]ID bits. */
4877c478bd9Sstevel@tonic-gate 				SETUGID_PRESERVE(dp->d_name,
4887c478bd9Sstevel@tonic-gate 				    st.st_mode & ~S_IFMT);
4897c478bd9Sstevel@tonic-gate 			} else if (rc == 0) {
4907c478bd9Sstevel@tonic-gate 				/* already visited */
4917c478bd9Sstevel@tonic-gate 				continue;
4927c478bd9Sstevel@tonic-gate 			} else {
4937c478bd9Sstevel@tonic-gate 				/*
4947c478bd9Sstevel@tonic-gate 				 * An error occurred while trying
4957c478bd9Sstevel@tonic-gate 				 * to add the node to the search tree.
4967c478bd9Sstevel@tonic-gate 				 */
4977c478bd9Sstevel@tonic-gate 				status += Perror(dp->d_name);
4987c478bd9Sstevel@tonic-gate 				continue;
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 		} else {
5017c478bd9Sstevel@tonic-gate 			CHOWN(dp->d_name, -1, gid);
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5057c478bd9Sstevel@tonic-gate 	if (chdir(savedir) < 0) {
5067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
5077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't change back to %s\n"),
5087c478bd9Sstevel@tonic-gate 		    savedir);
5097c478bd9Sstevel@tonic-gate 		exit(255);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static int
5147c478bd9Sstevel@tonic-gate isnumber(char *s)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	int c;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
5197c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
5207c478bd9Sstevel@tonic-gate 			return (0);
5217c478bd9Sstevel@tonic-gate 	return (1);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate static int
5267c478bd9Sstevel@tonic-gate Perror(char *s)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	if (!fflag) {
5297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
5307c478bd9Sstevel@tonic-gate 		perror(s);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	return (!fflag);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate static void
5377c478bd9Sstevel@tonic-gate usage(void)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
5407c478bd9Sstevel@tonic-gate 	    "usage:\n"
5417c478bd9Sstevel@tonic-gate 	    "\tchgrp [-fhR] group file ...\n"
5427c478bd9Sstevel@tonic-gate 	    "\tchgrp -R [-f] [-H|-L|-P] group file ...\n"));
5437c478bd9Sstevel@tonic-gate 	exit(2);
5447c478bd9Sstevel@tonic-gate }
545