[Midnightbsd-cvs] src [10834] add x flag

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jun 10 16:36:32 EDT 2018


Revision: 10834
          http://svnweb.midnightbsd.org/src/?rev=10834
Author:   laffer1
Date:     2018-06-10 16:36:31 -0400 (Sun, 10 Jun 2018)
Log Message:
-----------
add x flag

Modified Paths:
--------------
    trunk/usr.sbin/chown/Makefile
    trunk/usr.sbin/chown/chgrp.1
    trunk/usr.sbin/chown/chown.8
    trunk/usr.sbin/chown/chown.c

Property Changed:
----------------
    trunk/usr.sbin/chown/chgrp.1
    trunk/usr.sbin/chown/chown.8

Modified: trunk/usr.sbin/chown/Makefile
===================================================================
--- trunk/usr.sbin/chown/Makefile	2018-06-10 20:36:01 UTC (rev 10833)
+++ trunk/usr.sbin/chown/Makefile	2018-06-10 20:36:31 UTC (rev 10834)
@@ -1,10 +1,15 @@
+# $MidnightBSD$
 #	@(#)Makefile	8.1 (Berkeley) 6/6/93
-# $FreeBSD: src/usr.sbin/chown/Makefile,v 1.12 2003/04/25 09:02:59 johan Exp $
+# $FreeBSD: stable/10/usr.sbin/chown/Makefile 321130 2017-07-18 16:27:10Z ngie $
 
+.include <bsd.own.mk>
+
 PROG=	chown
 LINKS=	${BINDIR}/chown /usr/bin/chgrp
 MAN=	chgrp.1 chown.8
 
-WARNS?=	5
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
 
 .include <bsd.prog.mk>

Modified: trunk/usr.sbin/chown/chgrp.1
===================================================================
--- trunk/usr.sbin/chown/chgrp.1	2018-06-10 20:36:01 UTC (rev 10833)
+++ trunk/usr.sbin/chown/chgrp.1	2018-06-10 20:36:31 UTC (rev 10834)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\" Copyright (c) 1983, 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
 .\"
@@ -29,9 +30,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)chgrp.1	8.3 (Berkeley) 3/31/94
-.\" $FreeBSD: src/usr.sbin/chown/chgrp.1,v 1.19 2005/02/13 22:25:27 ru Exp $
+.\" $FreeBSD: stable/10/usr.sbin/chown/chgrp.1 283875 2015-06-01 09:04:57Z smh $
 .\"
-.Dd April 25, 2003
+.Dd April 20, 2015
 .Dt CHGRP 1
 .Os
 .Sh NAME
@@ -39,7 +40,7 @@
 .Nd change group
 .Sh SYNOPSIS
 .Nm
-.Op Fl fhv
+.Op Fl fhvx
 .Oo
 .Fl R
 .Op Fl H | Fl L | Fl P
@@ -60,8 +61,9 @@
 .It Fl H
 If the
 .Fl R
-option is specified, symbolic links on the command line are followed.
-(Symbolic links encountered in the tree traversal are not followed).
+option is specified, symbolic links on the command line are followed
+and hence unaffected by the command.
+(Symbolic links encountered during traversal are not followed.)
 .It Fl L
 If the
 .Fl R
@@ -72,8 +74,12 @@
 option is specified, no symbolic links are followed.
 This is the default.
 .It Fl R
-Change the group ID for the file hierarchies rooted
-in the files instead of just the files themselves.
+Change the group ID of the file hierarchies rooted in the files,
+instead of just the files themselves.
+Beware of unintentionally matching the
+.Dq Pa ".."
+hard link to the parent directory when using wildcards like
+.Dq Li ".*" .
 .It Fl f
 The force option ignores errors, except for usage errors and does not
 query about strange modes (unless the user does not have proper permissions).
@@ -89,6 +95,8 @@
 flag is specified more than once,
 .Nm
 will print the filename, followed by the old and new numeric group ID.
+.It Fl x
+File system mount points are not traversed.
 .El
 .Pp
 The
@@ -125,7 +133,9 @@
 .Pp
 The
 .Fl v
-option is non-standard and its use in scripts is not recommended.
+and
+.Fl x
+options are non-standard and their use in scripts is not recommended.
 .Sh SEE ALSO
 .Xr chown 2 ,
 .Xr fts 3 ,


Property changes on: trunk/usr.sbin/chown/chgrp.1
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/usr.sbin/chown/chown.8
===================================================================
--- trunk/usr.sbin/chown/chown.8	2018-06-10 20:36:01 UTC (rev 10833)
+++ trunk/usr.sbin/chown/chown.8	2018-06-10 20:36:31 UTC (rev 10834)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\" Copyright (c) 1990, 1991, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
 .\"
@@ -26,9 +27,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)chown.8	8.3 (Berkeley) 3/31/94
-.\" $FreeBSD: src/usr.sbin/chown/chown.8,v 1.25 2005/02/13 22:25:27 ru Exp $
+.\" $FreeBSD: stable/10/usr.sbin/chown/chown.8 283875 2015-06-01 09:04:57Z smh $
 .\"
-.Dd April 25, 2003
+.Dd April 20, 2015
 .Dt CHOWN 8
 .Os
 .Sh NAME
@@ -36,7 +37,7 @@
 .Nd change file owner and group
 .Sh SYNOPSIS
 .Nm
-.Op Fl fhv
+.Op Fl fhvx
 .Oo
 .Fl R
 .Op Fl H | Fl L | Fl P
@@ -44,7 +45,7 @@
 .Ar owner Ns Op : Ns Ar group
 .Ar
 .Nm
-.Op Fl fhv
+.Op Fl fhvx
 .Oo
 .Fl R
 .Op Fl H | Fl L | Fl P
@@ -64,8 +65,9 @@
 .It Fl H
 If the
 .Fl R
-option is specified, symbolic links on the command line are followed.
-(Symbolic links encountered in the tree traversal are not followed.)
+option is specified, symbolic links on the command line are followed
+and hence unaffected by the command.
+(Symbolic links encountered during traversal are not followed.)
 .It Fl L
 If the
 .Fl R
@@ -76,8 +78,8 @@
 option is specified, no symbolic links are followed.
 This is the default.
 .It Fl R
-Change the user ID and/or the group ID of the specified directory trees
-(recursively, including their contents) and files.
+Change the user ID and/or the group ID of the file hierarchies rooted
+in the files, instead of just the files themselves.
 Beware of unintentionally matching the
 .Dq Pa ".."
 hard link to the parent directory when using wildcards like
@@ -97,6 +99,8 @@
 flag is specified more than once,
 .Nm
 will print the filename, followed by the old and new numeric user/group ID.
+.It Fl x
+File system mount points are not traversed.
 .El
 .Pp
 The
@@ -146,7 +150,9 @@
 .Pp
 The
 .Fl v
-option is non-standard and its use in scripts is not recommended.
+and
+.Fl x
+options are non-standard and their use in scripts is not recommended.
 .Sh SEE ALSO
 .Xr chgrp 1 ,
 .Xr find 1 ,


Property changes on: trunk/usr.sbin/chown/chown.8
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/usr.sbin/chown/chown.c
===================================================================
--- trunk/usr.sbin/chown/chown.c	2018-06-10 20:36:01 UTC (rev 10833)
+++ trunk/usr.sbin/chown/chown.c	2018-06-10 20:36:31 UTC (rev 10834)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*
  * Copyright (c) 1988, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -40,7 +41,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.sbin/chown/chown.c,v 1.29 2004/08/07 04:19:37 imp Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.sbin/chown/chown.c 283875 2015-06-01 09:04:57Z smh $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -47,6 +48,7 @@
 
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <fts.h>
 #include <grp.h>
 #include <libgen.h>
@@ -57,16 +59,16 @@
 #include <string.h>
 #include <unistd.h>
 
-void	a_gid(const char *);
-void	a_uid(const char *);
-void	chownerr(const char *);
-uid_t	id(const char *, const char *);
-void	usage(void);
+static void	a_gid(const char *);
+static void	a_uid(const char *);
+static void	chownerr(const char *);
+static uid_t	id(const char *, const char *);
+static void	usage(void);
 
-uid_t uid;
-gid_t gid;
-int ischown;
-const char *gname;
+static uid_t uid;
+static gid_t gid;
+static int ischown;
+static const char *gname;
 
 int
 main(int argc, char **argv)
@@ -73,14 +75,14 @@
 {
 	FTS *ftsp;
 	FTSENT *p;
-	int Hflag, Lflag, Rflag, fflag, hflag, vflag;
+	int Hflag, Lflag, Rflag, fflag, hflag, vflag, xflag;
 	int ch, fts_options, rval;
 	char *cp;
 
 	ischown = (strcmp(basename(argv[0]), "chown") == 0);
 
-	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
-	while ((ch = getopt(argc, argv, "HLPRfhv")) != -1)
+	Hflag = Lflag = Rflag = fflag = hflag = vflag = xflag = 0;
+	while ((ch = getopt(argc, argv, "HLPRfhvx")) != -1)
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -105,6 +107,9 @@
 		case 'v':
 			vflag++;
 			break;
+		case 'x':
+			xflag = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -116,19 +121,27 @@
 		usage();
 
 	if (Rflag) {
-		fts_options = FTS_PHYSICAL;
 		if (hflag && (Hflag || Lflag))
 			errx(1, "the -R%c and -h options may not be "
 			    "specified together", Hflag ? 'H' : 'L');
-		if (Hflag)
-			fts_options |= FTS_COMFOLLOW;
-		else if (Lflag) {
-			fts_options &= ~FTS_PHYSICAL;
-			fts_options |= FTS_LOGICAL;
+		if (Lflag) {
+			fts_options = FTS_LOGICAL;
+		} else {
+			fts_options = FTS_PHYSICAL;
+
+			if (Hflag) {
+				fts_options |= FTS_COMFOLLOW;
+			}
 		}
-	} else
-		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
+	} else if (hflag) {
+		fts_options = FTS_PHYSICAL;
+	} else {
+		fts_options = FTS_LOGICAL;
+	}
 
+	if (xflag)
+		fts_options |= FTS_XDEV;
+
 	uid = (uid_t)-1;
 	gid = (gid_t)-1;
 	if (ischown) {
@@ -151,6 +164,15 @@
 		err(1, NULL);
 
 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
+		int atflag;
+
+		if ((fts_options & FTS_LOGICAL) ||
+		    ((fts_options & FTS_COMFOLLOW) &&
+		    p->fts_level == FTS_ROOTLEVEL))
+			atflag = 0;
+		else
+			atflag = AT_SYMLINK_NOFOLLOW;
+
 		switch (p->fts_info) {
 		case FTS_D:			/* Change it at FTS_DP. */
 			if (!Rflag)
@@ -165,17 +187,6 @@
 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
 			rval = 1;
 			continue;
-		case FTS_SL:
-		case FTS_SLNONE:
-			/*
-			 * The only symlinks that end up here are ones that
-			 * don't point to anything and ones that we found
-			 * doing a physical walk.
-			 */
-			if (hflag)
-				break;
-			else
-				continue;
 		default:
 			break;
 		}
@@ -182,41 +193,38 @@
 		if ((uid == (uid_t)-1 || uid == p->fts_statp->st_uid) &&
 		    (gid == (gid_t)-1 || gid == p->fts_statp->st_gid))
 			continue;
-		if ((hflag ? lchown : chown)(p->fts_accpath, uid, gid) == -1) {
-			if (!fflag) {
-				chownerr(p->fts_path);
-				rval = 1;
-			}
-		} else {
-			if (vflag) {
-				printf("%s", p->fts_path);
-				if (vflag > 1) {
-					if (ischown) {
-						printf(": %ju:%ju -> %ju:%ju",
-						    (uintmax_t)
-						    p->fts_statp->st_uid, 
-						    (uintmax_t)
-						    p->fts_statp->st_gid,
-						    (uid == (uid_t)-1) ? 
-						    (uintmax_t)
-						    p->fts_statp->st_uid : 
-						    (uintmax_t)uid,
-						    (gid == (gid_t)-1) ? 
-						    (uintmax_t)
-						    p->fts_statp->st_gid :
-						    (uintmax_t)gid);
-					} else {
-						printf(": %ju -> %ju",
-						    (uintmax_t)
-						    p->fts_statp->st_gid,
-						    (gid == (gid_t)-1) ? 
-						    (uintmax_t)
-						    p->fts_statp->st_gid : 
-						    (uintmax_t)gid);
-					}
+		if (fchownat(AT_FDCWD, p->fts_accpath, uid, gid, atflag)
+		    == -1 && !fflag) {
+			chownerr(p->fts_path);
+			rval = 1;
+		} else if (vflag) {
+			printf("%s", p->fts_path);
+			if (vflag > 1) {
+				if (ischown) {
+					printf(": %ju:%ju -> %ju:%ju",
+					    (uintmax_t)
+					    p->fts_statp->st_uid, 
+					    (uintmax_t)
+					    p->fts_statp->st_gid,
+					    (uid == (uid_t)-1) ? 
+					    (uintmax_t)
+					    p->fts_statp->st_uid : 
+					    (uintmax_t)uid,
+					    (gid == (gid_t)-1) ? 
+					    (uintmax_t)
+					    p->fts_statp->st_gid :
+					    (uintmax_t)gid);
+				} else {
+					printf(": %ju -> %ju",
+					    (uintmax_t)
+					    p->fts_statp->st_gid,
+					    (gid == (gid_t)-1) ? 
+					    (uintmax_t)
+					    p->fts_statp->st_gid : 
+					    (uintmax_t)gid);
 				}
-				printf("\n");
 			}
+			printf("\n");
 		}
 	}
 	if (errno)
@@ -224,7 +232,7 @@
 	exit(rval);
 }
 
-void
+static void
 a_gid(const char *s)
 {
 	struct group *gr;
@@ -235,7 +243,7 @@
 	gid = ((gr = getgrnam(s)) != NULL) ? gr->gr_gid : id(s, "group");
 }
 
-void
+static void
 a_uid(const char *s)
 {
 	struct passwd *pw;
@@ -245,7 +253,7 @@
 	uid = ((pw = getpwnam(s)) != NULL) ? pw->pw_uid : id(s, "user");
 }
 
-uid_t
+static uid_t
 id(const char *name, const char *type)
 {
 	uid_t val;
@@ -257,19 +265,18 @@
 	 */
 	errno = 0;
 	val = strtoul(name, &ep, 10);
-	if (errno)
-		err(1, "%s", name);
-	if (*ep != '\0')
+	if (errno || *ep != '\0')
 		errx(1, "%s: illegal %s name", name, type);
 	return (val);
 }
 
-void
+static void
 chownerr(const char *file)
 {
 	static uid_t euid = -1;
 	static int ngroups = -1;
-	gid_t groups[NGROUPS_MAX];
+	static long ngroups_max;
+	gid_t *groups;
 
 	/* Check for chown without being root. */
 	if (errno != EPERM || (uid != (uid_t)-1 &&
@@ -281,8 +288,12 @@
 	/* Check group membership; kernel just returns EPERM. */
 	if (gid != (gid_t)-1 && ngroups == -1 &&
 	    euid == (uid_t)-1 && (euid = geteuid()) != 0) {
-		ngroups = getgroups(NGROUPS_MAX, groups);
+		ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
+		if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
+			err(1, "malloc");
+		ngroups = getgroups(ngroups_max, groups);
 		while (--ngroups >= 0 && gid != groups[ngroups]);
+		free(groups);
 		if (ngroups < 0) {
 			warnx("you are not a member of group %s", gname);
 			return;
@@ -291,17 +302,17 @@
 	warn("%s", file);
 }
 
-void
+static void
 usage(void)
 {
 
 	if (ischown)
 		(void)fprintf(stderr, "%s\n%s\n",
-		    "usage: chown [-fhv] [-R [-H | -L | -P]] owner[:group]"
+		    "usage: chown [-fhvx] [-R [-H | -L | -P]] owner[:group]"
 		    " file ...",
-		    "       chown [-fhv] [-R [-H | -L | -P]] :group file ...");
+		    "       chown [-fhvx] [-R [-H | -L | -P]] :group file ...");
 	else
 		(void)fprintf(stderr, "%s\n",
-		    "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...");
+		    "usage: chgrp [-fhvx] [-R [-H | -L | -P]] group file ...");
 	exit(1);
 }



More information about the Midnightbsd-cvs mailing list