xref: /onnv-gate/usr/src/cmd/chgrp/chgrp.c (revision 7057:d3fa1d6dbef7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52403Sas145665  * Common Development and Distribution License (the "License").
62403Sas145665  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7057Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
280Sstevel@tonic-gate  *	  All Rights Reserved
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
330Sstevel@tonic-gate  * The Regents of the University of California
340Sstevel@tonic-gate  * All Rights Reserved
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
370Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
380Sstevel@tonic-gate  * contributors.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * chgrp [-fhR] gid file ...
450Sstevel@tonic-gate  * chgrp -R [-f] [-H|-L|-P] gid file ...
46*7057Smarks  * chgrp -s [-fhR] groupsid file ...
47*7057Smarks  * chgrp -s -R [-f] [-H|-L|-P] groupsid file ...
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <ctype.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <sys/avl.h>
550Sstevel@tonic-gate #include <grp.h>
560Sstevel@tonic-gate #include <dirent.h>
570Sstevel@tonic-gate #include <unistd.h>
580Sstevel@tonic-gate #include <stdlib.h>
590Sstevel@tonic-gate #include <locale.h>
600Sstevel@tonic-gate #include <libcmdutils.h>
610Sstevel@tonic-gate #include <errno.h>
620Sstevel@tonic-gate #include <strings.h>
63*7057Smarks #include <aclutils.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static struct group	*gr;
660Sstevel@tonic-gate static struct stat	stbuf;
670Sstevel@tonic-gate static struct stat	stbuf2;
680Sstevel@tonic-gate static gid_t		gid;
690Sstevel@tonic-gate static int		hflag = 0,
700Sstevel@tonic-gate 			fflag = 0,
710Sstevel@tonic-gate 			rflag = 0,
720Sstevel@tonic-gate 			Hflag = 0,
730Sstevel@tonic-gate 			Lflag = 0,
74*7057Smarks 			Pflag = 0,
75*7057Smarks 			sflag = 0;
760Sstevel@tonic-gate static int		status = 0;	/* total number of errors received */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static avl_tree_t	*tree;		/* search tree to store inode data */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static void usage(void);
810Sstevel@tonic-gate static int isnumber(char *);
820Sstevel@tonic-gate static int Perror(char *);
830Sstevel@tonic-gate static void chgrpr(char *, gid_t);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifdef XPG4
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
880Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
890Sstevel@tonic-gate  * specified, so we are just looking to see if -R -L, or -R -H was specified,
900Sstevel@tonic-gate  * or, since -R has the same behavior as -R -L, if -R was specified by itself.
910Sstevel@tonic-gate  * Therefore, all we really need to check for is if -R was specified.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag)
940Sstevel@tonic-gate #else
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Check to see if we are to follow symlinks specified on the command line.
970Sstevel@tonic-gate  * This assumes we've already checked to make sure neither -h or -P was
980Sstevel@tonic-gate  * specified, so we are just looking to see if -R -L, or -R -H was specified.
990Sstevel@tonic-gate  * Note: -R by itself will change the group of a directory referenced by a
1000Sstevel@tonic-gate  * symlink however it will not follow the symlink to any other part of the
1010Sstevel@tonic-gate  * file hierarchy.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate #define	FOLLOW_CL_LINKS	(rflag && (Hflag || Lflag))
1040Sstevel@tonic-gate #endif
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #ifdef XPG4
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Since -R behaves the
1090Sstevel@tonic-gate  * same as -R -L, we always want to follow symlinks to other parts
1100Sstevel@tonic-gate  * of the file hierarchy unless -H was specified.
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(!Hflag)
1130Sstevel@tonic-gate #else
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * Follow symlinks when traversing directories.  Only follow symlinks
1160Sstevel@tonic-gate  * to other parts of the file hierarchy if -L was specified.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate #define	FOLLOW_D_LINKS	(Lflag)
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #define	CHOWN(f, u, g)	if (chown(f, u, g) < 0) { \
1220Sstevel@tonic-gate 				status += Perror(f); \
1230Sstevel@tonic-gate 			}
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	LCHOWN(f, u, g)	if (lchown(f, u, g) < 0) { \
1260Sstevel@tonic-gate 				status += Perror(f); \
1270Sstevel@tonic-gate 			}
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * We're ignoring errors here because preserving the SET[UG]ID bits is just
1300Sstevel@tonic-gate  * a courtesy.  This is only used on directories.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate #define	SETUGID_PRESERVE(dir, mode) \
1330Sstevel@tonic-gate 	if (((mode) & (S_ISGID|S_ISUID)) != 0) \
1340Sstevel@tonic-gate 		(void) chmod((dir), (mode) & ~S_IFMT)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate extern int		optind;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
main(int argc,char * argv[])1400Sstevel@tonic-gate main(int argc, char *argv[])
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int		c;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/* set the locale for only the messages system (all else is clean) */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1470Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
1480Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1510Sstevel@tonic-gate 
152*7057Smarks 	while ((c = getopt(argc, argv, "RhfHLPs")) != EOF)
1530Sstevel@tonic-gate 		switch (c) {
1540Sstevel@tonic-gate 			case 'R':
1550Sstevel@tonic-gate 				rflag++;
1560Sstevel@tonic-gate 				break;
1570Sstevel@tonic-gate 			case 'h':
1580Sstevel@tonic-gate 				hflag++;
1590Sstevel@tonic-gate 				break;
1600Sstevel@tonic-gate 			case 'f':
1610Sstevel@tonic-gate 				fflag++;
1620Sstevel@tonic-gate 				break;
1630Sstevel@tonic-gate 			case 'H':
1640Sstevel@tonic-gate 				/*
1650Sstevel@tonic-gate 				 * If more than one of -H, -L, and -P
1660Sstevel@tonic-gate 				 * are specified, only the last option
1670Sstevel@tonic-gate 				 * specified determines the behavior of
1680Sstevel@tonic-gate 				 * chgrp.  In addition, make [-H|-L]
1690Sstevel@tonic-gate 				 * mutually exclusive of -h.
1700Sstevel@tonic-gate 				 */
1710Sstevel@tonic-gate 				Lflag = Pflag = 0;
1720Sstevel@tonic-gate 				Hflag++;
1730Sstevel@tonic-gate 				break;
1740Sstevel@tonic-gate 			case 'L':
1750Sstevel@tonic-gate 				Hflag = Pflag = 0;
1760Sstevel@tonic-gate 				Lflag++;
1770Sstevel@tonic-gate 				break;
1780Sstevel@tonic-gate 			case 'P':
1790Sstevel@tonic-gate 				Hflag = Lflag = 0;
1800Sstevel@tonic-gate 				Pflag++;
1810Sstevel@tonic-gate 				break;
182*7057Smarks 			case 's':
183*7057Smarks 				sflag++;
184*7057Smarks 				break;
1850Sstevel@tonic-gate 			default:
1860Sstevel@tonic-gate 				usage();
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Check for sufficient arguments
1910Sstevel@tonic-gate 	 * or a usage error.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	argc -= optind;
1940Sstevel@tonic-gate 	argv = &argv[optind];
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if ((argc < 2) ||
1970Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && !rflag) ||
1980Sstevel@tonic-gate 	    ((Hflag || Lflag || Pflag) && hflag)) {
1990Sstevel@tonic-gate 		usage();
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
202*7057Smarks 	if (sflag) {
203*7057Smarks 		if (sid_to_id(argv[0], B_FALSE, &gid)) {
204*7057Smarks 			(void) fprintf(stderr, gettext(
205*7057Smarks 			    "chgrp: invalid group sid %s\n"), argv[0]);
206*7057Smarks 			exit(2);
207*7057Smarks 		}
208*7057Smarks 	} else if ((gr = getgrnam(argv[0])) != NULL) {
2092403Sas145665 		gid = gr->gr_gid;
2102403Sas145665 	} else {
2112403Sas145665 		if (isnumber(argv[0])) {
2122403Sas145665 			errno = 0;
2132403Sas145665 			/* gid is an int */
214*7057Smarks 			gid = (gid_t)strtoul(argv[0], NULL, 10);
2152403Sas145665 			if (errno != 0) {
2162403Sas145665 				if (errno == ERANGE) {
2172403Sas145665 					(void) fprintf(stderr, gettext(
2180Sstevel@tonic-gate 					"chgrp: group id is too large\n"));
2192403Sas145665 					exit(2);
2202403Sas145665 				} else {
2212403Sas145665 					(void) fprintf(stderr, gettext(
2220Sstevel@tonic-gate 					"chgrp: invalid group id\n"));
2232403Sas145665 					exit(2);
2242403Sas145665 				}
2250Sstevel@tonic-gate 			}
2262403Sas145665 		} else {
2270Sstevel@tonic-gate 			(void) fprintf(stderr, "chgrp: ");
2280Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("unknown group: %s\n"),
229*7057Smarks 			    argv[0]);
2300Sstevel@tonic-gate 			exit(2);
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	for (c = 1; c < argc; c++) {
2350Sstevel@tonic-gate 		tree = NULL;
2360Sstevel@tonic-gate 		if (lstat(argv[c], &stbuf) < 0) {
2370Sstevel@tonic-gate 			status += Perror(argv[c]);
2380Sstevel@tonic-gate 			continue;
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 		if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFLNK)) {
2410Sstevel@tonic-gate 			if (hflag || Pflag) {
2420Sstevel@tonic-gate 				/*
2430Sstevel@tonic-gate 				 * Change the group id of the symbolic link
2440Sstevel@tonic-gate 				 * specified on the command line.
2450Sstevel@tonic-gate 				 * Don't follow the symbolic link to
2460Sstevel@tonic-gate 				 * any other part of the file hierarchy.
2470Sstevel@tonic-gate 				 */
2480Sstevel@tonic-gate 				LCHOWN(argv[c], -1, gid);
2490Sstevel@tonic-gate 			} else {
2500Sstevel@tonic-gate 				if (stat(argv[c], &stbuf2) < 0) {
2510Sstevel@tonic-gate 					status += Perror(argv[c]);
2520Sstevel@tonic-gate 					continue;
2530Sstevel@tonic-gate 				}
2540Sstevel@tonic-gate 				/*
2550Sstevel@tonic-gate 				 * We know that we are to change the
2560Sstevel@tonic-gate 				 * group of the file referenced by the
2570Sstevel@tonic-gate 				 * symlink specified on the command line.
2580Sstevel@tonic-gate 				 * Now check to see if we are to follow
2590Sstevel@tonic-gate 				 * the symlink to any other part of the
2600Sstevel@tonic-gate 				 * file hierarchy.
2610Sstevel@tonic-gate 				 */
2620Sstevel@tonic-gate 				if (FOLLOW_CL_LINKS) {
2630Sstevel@tonic-gate 					if ((stbuf2.st_mode & S_IFMT)
2640Sstevel@tonic-gate 					    == S_IFDIR) {
2650Sstevel@tonic-gate 						/*
2660Sstevel@tonic-gate 						 * We are following symlinks so
2670Sstevel@tonic-gate 						 * traverse into the directory.
2680Sstevel@tonic-gate 						 * Add this node to the search
2690Sstevel@tonic-gate 						 * tree so we don't get into an
2700Sstevel@tonic-gate 						 * endless loop.
2710Sstevel@tonic-gate 						 */
2720Sstevel@tonic-gate 						if (add_tnode(&tree,
2730Sstevel@tonic-gate 						    stbuf2.st_dev,
2740Sstevel@tonic-gate 						    stbuf2.st_ino) == 1) {
2750Sstevel@tonic-gate 							chgrpr(argv[c], gid);
2760Sstevel@tonic-gate 							/*
2770Sstevel@tonic-gate 							 * Try to restore the
2780Sstevel@tonic-gate 							 * SET[UG]ID bits.
2790Sstevel@tonic-gate 							 */
2800Sstevel@tonic-gate 							SETUGID_PRESERVE(
2810Sstevel@tonic-gate 							    argv[c],
2820Sstevel@tonic-gate 							    stbuf2.st_mode &
2830Sstevel@tonic-gate 							    ~S_IFMT);
2840Sstevel@tonic-gate 						} else {
2850Sstevel@tonic-gate 							/*
2860Sstevel@tonic-gate 							 * Error occurred.
2870Sstevel@tonic-gate 							 * rc can't be 0
2880Sstevel@tonic-gate 							 * as this is the first
2890Sstevel@tonic-gate 							 * node to be added to
2900Sstevel@tonic-gate 							 * the search tree.
2910Sstevel@tonic-gate 							 */
2920Sstevel@tonic-gate 							status += Perror(
2930Sstevel@tonic-gate 							    argv[c]);
2940Sstevel@tonic-gate 						}
2950Sstevel@tonic-gate 					} else {
2960Sstevel@tonic-gate 						/*
2970Sstevel@tonic-gate 						 * Change the group id of the
2980Sstevel@tonic-gate 						 * file referenced by the
2990Sstevel@tonic-gate 						 * symbolic link.
3000Sstevel@tonic-gate 						 */
3010Sstevel@tonic-gate 						CHOWN(argv[c], -1, gid);
3020Sstevel@tonic-gate 					}
3030Sstevel@tonic-gate 				} else {
3040Sstevel@tonic-gate 					/*
3050Sstevel@tonic-gate 					 * Change the group id of the file
3060Sstevel@tonic-gate 					 * referenced by the symbolic link.
3070Sstevel@tonic-gate 					 */
3080Sstevel@tonic-gate 					CHOWN(argv[c], -1, gid);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 					if ((stbuf2.st_mode & S_IFMT)
3110Sstevel@tonic-gate 					    == S_IFDIR) {
3120Sstevel@tonic-gate 						/* Reset the SET[UG]ID bits. */
3130Sstevel@tonic-gate 						SETUGID_PRESERVE(argv[c],
3140Sstevel@tonic-gate 						    stbuf2.st_mode & ~S_IFMT);
3150Sstevel@tonic-gate 					}
3160Sstevel@tonic-gate 				}
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 		} else if (rflag && ((stbuf.st_mode & S_IFMT) == S_IFDIR)) {
3190Sstevel@tonic-gate 			/*
3200Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
3210Sstevel@tonic-gate 			 * get into a endless loop.
3220Sstevel@tonic-gate 			 */
3230Sstevel@tonic-gate 			if (add_tnode(&tree, stbuf.st_dev,
3240Sstevel@tonic-gate 			    stbuf.st_ino) == 1) {
3250Sstevel@tonic-gate 				chgrpr(argv[c], gid);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 				/* Restore the SET[UG]ID bits. */
3280Sstevel@tonic-gate 				SETUGID_PRESERVE(argv[c],
3290Sstevel@tonic-gate 				    stbuf.st_mode & ~S_IFMT);
3300Sstevel@tonic-gate 			} else {
3310Sstevel@tonic-gate 				/*
3320Sstevel@tonic-gate 				 * An error occurred while trying
3330Sstevel@tonic-gate 				 * to add the node to the tree.
3340Sstevel@tonic-gate 				 * Continue on with next file
3350Sstevel@tonic-gate 				 * specified.  Note: rc shouldn't
3360Sstevel@tonic-gate 				 * be 0 as this was the first node
3370Sstevel@tonic-gate 				 * being added to the search tree.
3380Sstevel@tonic-gate 				 */
3390Sstevel@tonic-gate 				status += Perror(argv[c]);
3400Sstevel@tonic-gate 			}
3410Sstevel@tonic-gate 		} else {
3420Sstevel@tonic-gate 			if (hflag || Pflag) {
3430Sstevel@tonic-gate 				LCHOWN(argv[c], -1, gid);
3440Sstevel@tonic-gate 			} else {
3450Sstevel@tonic-gate 				CHOWN(argv[c], -1, gid);
3460Sstevel@tonic-gate 			}
3470Sstevel@tonic-gate 			/* If a directory, reset the SET[UG]ID bits. */
3480Sstevel@tonic-gate 			if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3490Sstevel@tonic-gate 				SETUGID_PRESERVE(argv[c],
3500Sstevel@tonic-gate 				    stbuf.st_mode & ~S_IFMT);
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	return (status);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * chgrpr() - recursive chown()
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  * Recursively chowns the input directory then its contents.  rflag must
3610Sstevel@tonic-gate  * have been set if chgrpr() is called.  The input directory should not
3620Sstevel@tonic-gate  * be a sym link (this is handled in the calling routine).  In
3630Sstevel@tonic-gate  * addition, the calling routine should have already added the input
3640Sstevel@tonic-gate  * directory to the search tree so we do not get into endless loops.
3650Sstevel@tonic-gate  * Note: chgrpr() doesn't need a return value as errors are reported
3660Sstevel@tonic-gate  * through the global "status" variable.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate static void
chgrpr(char * dir,gid_t gid)3690Sstevel@tonic-gate chgrpr(char *dir, gid_t gid)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	struct dirent *dp;
3720Sstevel@tonic-gate 	DIR *dirp;
3730Sstevel@tonic-gate 	struct stat st, st2;
3740Sstevel@tonic-gate 	char savedir[1024];
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if (getcwd(savedir, 1024) == 0) {
3770Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
3780Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), savedir);
3790Sstevel@tonic-gate 		exit(255);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * Attempt to chown the directory, however don't return if we
3840Sstevel@tonic-gate 	 * can't as we still may be able to chown the contents of the
3850Sstevel@tonic-gate 	 * directory.  Note: the calling routine resets the SUID bits
3860Sstevel@tonic-gate 	 * on this directory so we don't have to perform an extra 'stat'.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	CHOWN(dir, -1, gid);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (chdir(dir) < 0) {
3910Sstevel@tonic-gate 		status += Perror(dir);
3920Sstevel@tonic-gate 		return;
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
3950Sstevel@tonic-gate 		status += Perror(dir);
3960Sstevel@tonic-gate 		return;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
3990Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
4000Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0)) {
4010Sstevel@tonic-gate 			continue;	/* skip "." and ".." */
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		if (lstat(dp->d_name, &st) < 0) {
4040Sstevel@tonic-gate 			status += Perror(dp->d_name);
4050Sstevel@tonic-gate 			continue;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 		if ((st.st_mode & S_IFMT) == S_IFLNK) {
4080Sstevel@tonic-gate 			if (hflag || Pflag) {
4090Sstevel@tonic-gate 				/*
4100Sstevel@tonic-gate 				 * Change the group id of the symbolic link
4110Sstevel@tonic-gate 				 * encountered while traversing the
4120Sstevel@tonic-gate 				 * directory.  Don't follow the symbolic
4130Sstevel@tonic-gate 				 * link to any other part of the file
4140Sstevel@tonic-gate 				 * hierarchy.
4150Sstevel@tonic-gate 				 */
4160Sstevel@tonic-gate 				LCHOWN(dp->d_name, -1, gid);
4170Sstevel@tonic-gate 			} else {
4180Sstevel@tonic-gate 				if (stat(dp->d_name, &st2) < 0) {
4190Sstevel@tonic-gate 					status += Perror(dp->d_name);
4200Sstevel@tonic-gate 					continue;
4210Sstevel@tonic-gate 				}
4220Sstevel@tonic-gate 				/*
4230Sstevel@tonic-gate 				 * We know that we are to change the
4240Sstevel@tonic-gate 				 * group of the file referenced by the
4250Sstevel@tonic-gate 				 * symlink encountered while traversing
4260Sstevel@tonic-gate 				 * the directory.  Now check to see if we
4270Sstevel@tonic-gate 				 * are to follow the symlink to any other
4280Sstevel@tonic-gate 				 * part of the file hierarchy.
4290Sstevel@tonic-gate 				 */
4300Sstevel@tonic-gate 				if (FOLLOW_D_LINKS) {
4310Sstevel@tonic-gate 					if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4320Sstevel@tonic-gate 						/*
4330Sstevel@tonic-gate 						 * We are following symlinks so
4340Sstevel@tonic-gate 						 * traverse into the directory.
4350Sstevel@tonic-gate 						 * Add this node to the search
4360Sstevel@tonic-gate 						 * tree so we don't get into an
4370Sstevel@tonic-gate 						 * endless loop.
4380Sstevel@tonic-gate 						 */
4390Sstevel@tonic-gate 						int rc;
4400Sstevel@tonic-gate 						if ((rc = add_tnode(&tree,
4410Sstevel@tonic-gate 						    st2.st_dev,
4420Sstevel@tonic-gate 						    st2.st_ino)) == 1) {
4430Sstevel@tonic-gate 							chgrpr(dp->d_name, gid);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 							/*
4460Sstevel@tonic-gate 							 * Restore SET[UG]ID
4470Sstevel@tonic-gate 							 * bits.
4480Sstevel@tonic-gate 							 */
4490Sstevel@tonic-gate 							SETUGID_PRESERVE(
4500Sstevel@tonic-gate 							    dp->d_name,
4510Sstevel@tonic-gate 							    st2.st_mode &
4520Sstevel@tonic-gate 							    ~S_IFMT);
4530Sstevel@tonic-gate 						} else if (rc == 0) {
4540Sstevel@tonic-gate 							/* already visited */
4550Sstevel@tonic-gate 							continue;
4560Sstevel@tonic-gate 						} else {
4570Sstevel@tonic-gate 							/*
4580Sstevel@tonic-gate 							 * An error occurred
4590Sstevel@tonic-gate 							 * while trying to add
4600Sstevel@tonic-gate 							 * the node to the tree.
4610Sstevel@tonic-gate 							 */
4620Sstevel@tonic-gate 							status += Perror(
4630Sstevel@tonic-gate 							    dp->d_name);
4640Sstevel@tonic-gate 							continue;
4650Sstevel@tonic-gate 						}
4660Sstevel@tonic-gate 					} else {
4670Sstevel@tonic-gate 						/*
4680Sstevel@tonic-gate 						 * Change the group id of the
4690Sstevel@tonic-gate 						 * file referenced by the
4700Sstevel@tonic-gate 						 * symbolic link.
4710Sstevel@tonic-gate 						 */
4720Sstevel@tonic-gate 						CHOWN(dp->d_name, -1, gid);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 					}
4750Sstevel@tonic-gate 				} else {
4760Sstevel@tonic-gate 					/*
4770Sstevel@tonic-gate 					 * Change the group id of the file
4780Sstevel@tonic-gate 					 * referenced by the symbolic link.
4790Sstevel@tonic-gate 					 */
4800Sstevel@tonic-gate 					CHOWN(dp->d_name, -1, gid);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 					if ((st2.st_mode & S_IFMT) == S_IFDIR) {
4830Sstevel@tonic-gate 						/* Restore SET[UG]ID bits. */
4840Sstevel@tonic-gate 						SETUGID_PRESERVE(dp->d_name,
4850Sstevel@tonic-gate 						    st2.st_mode & ~S_IFMT);
4860Sstevel@tonic-gate 					}
4870Sstevel@tonic-gate 				}
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 		} else if ((st.st_mode & S_IFMT) == S_IFDIR) {
4900Sstevel@tonic-gate 			/*
4910Sstevel@tonic-gate 			 * Add this node to the search tree so we don't
4920Sstevel@tonic-gate 			 * get into a endless loop.
4930Sstevel@tonic-gate 			 */
4940Sstevel@tonic-gate 			int rc;
4950Sstevel@tonic-gate 			if ((rc = add_tnode(&tree, st.st_dev,
4960Sstevel@tonic-gate 			    st.st_ino)) == 1) {
4970Sstevel@tonic-gate 				chgrpr(dp->d_name, gid);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 				/* Restore the SET[UG]ID bits. */
5000Sstevel@tonic-gate 				SETUGID_PRESERVE(dp->d_name,
5010Sstevel@tonic-gate 				    st.st_mode & ~S_IFMT);
5020Sstevel@tonic-gate 			} else if (rc == 0) {
5030Sstevel@tonic-gate 				/* already visited */
5040Sstevel@tonic-gate 				continue;
5050Sstevel@tonic-gate 			} else {
5060Sstevel@tonic-gate 				/*
5070Sstevel@tonic-gate 				 * An error occurred while trying
5080Sstevel@tonic-gate 				 * to add the node to the search tree.
5090Sstevel@tonic-gate 				 */
5100Sstevel@tonic-gate 				status += Perror(dp->d_name);
5110Sstevel@tonic-gate 				continue;
5120Sstevel@tonic-gate 			}
5130Sstevel@tonic-gate 		} else {
5140Sstevel@tonic-gate 			CHOWN(dp->d_name, -1, gid);
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 	(void) closedir(dirp);
5180Sstevel@tonic-gate 	if (chdir(savedir) < 0) {
5190Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
5200Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("can't change back to %s\n"),
5210Sstevel@tonic-gate 		    savedir);
5220Sstevel@tonic-gate 		exit(255);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static int
isnumber(char * s)5270Sstevel@tonic-gate isnumber(char *s)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	int c;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	while ((c = *s++) != '\0')
5320Sstevel@tonic-gate 		if (!isdigit(c))
5330Sstevel@tonic-gate 			return (0);
5340Sstevel@tonic-gate 	return (1);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate static int
Perror(char * s)5390Sstevel@tonic-gate Perror(char *s)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	if (!fflag) {
5420Sstevel@tonic-gate 		(void) fprintf(stderr, "chgrp: ");
5430Sstevel@tonic-gate 		perror(s);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 	return (!fflag);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate static void
usage(void)5500Sstevel@tonic-gate usage(void)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
5530Sstevel@tonic-gate 	    "usage:\n"
5540Sstevel@tonic-gate 	    "\tchgrp [-fhR] group file ...\n"
555*7057Smarks 	    "\tchgrp -R [-f] [-H|-L|-P] group file ...\n"
556*7057Smarks 	    "\tchgrp -s [-fhR] groupsid file ...\n"
557*7057Smarks 	    "\tchgrp -s -R [-f] [-H|-L|-P] groupsid file ...\n"));
5580Sstevel@tonic-gate 	exit(2);
5590Sstevel@tonic-gate }
560