Module Name:    src
Committed By:   christos
Date:           Mon Oct 22 17:47:06 UTC 2012

Modified Files:
        src/bin/chmod: chmod.1 chmod.c

Log Message:
add --reference=rfile


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/bin/chmod/chmod.1
cvs rdiff -u -r1.36 -r1.37 src/bin/chmod/chmod.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/chmod/chmod.1
diff -u src/bin/chmod/chmod.1:1.23 src/bin/chmod/chmod.1:1.24
--- src/bin/chmod/chmod.1:1.23	Fri Jan 22 00:41:36 2010
+++ src/bin/chmod/chmod.1	Mon Oct 22 13:47:06 2012
@@ -1,4 +1,4 @@
-.\"	$NetBSD: chmod.1,v 1.23 2010/01/22 05:41:36 snj Exp $
+.\"	$NetBSD: chmod.1,v 1.24 2012/10/22 17:47:06 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"	@(#)chmod.1	8.4 (Berkeley) 3/31/94
 .\"
-.Dd January 22, 2010
+.Dd October 22, 2012
 .Dt CHMOD 1
 .Os
 .Sh NAME
@@ -47,13 +47,26 @@
 .Op Fl fh
 .Ar mode
 .Ar
+.Nm
+.Oo
+.Fl R
+.Op Fl H | Fl L | Fl P
+.Oc
+.Op Fl fh
+.Fl Fl reference=rfile
+.Ar
 .Sh DESCRIPTION
 The
 .Nm
 utility modifies the file mode bits of the listed files
 as specified by the
 .Ar mode
-operand.
+operand, or
+copied from a reference
+.Ar rfile ,
+as specified with the
+.Fl Fl reference
+argument.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds

Index: src/bin/chmod/chmod.c
diff -u src/bin/chmod/chmod.c:1.36 src/bin/chmod/chmod.c:1.37
--- src/bin/chmod/chmod.c:1.36	Mon Aug 29 10:51:17 2011
+++ src/bin/chmod/chmod.c	Mon Oct 22 13:47:06 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: chmod.c,v 1.36 2011/08/29 14:51:17 joerg Exp $ */
+/* $NetBSD: chmod.c,v 1.37 2012/10/22 17:47:06 christos Exp $ */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -40,7 +40,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)chmod.c	8.8 (Berkeley) 4/1/94";
 #else
-__RCSID("$NetBSD: chmod.c,v 1.36 2011/08/29 14:51:17 joerg Exp $");
+__RCSID("$NetBSD: chmod.c,v 1.37 2012/10/22 17:47:06 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -57,25 +57,39 @@ __RCSID("$NetBSD: chmod.c,v 1.36 2011/08
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <getopt.h>
 
 __dead static void	usage(void);
 
+struct option chmod_longopts[] = {
+	{ "reference",		required_argument,	0,
+						1 },
+	{ NULL,			0,			0,
+						0 },
+};
+
 int
 main(int argc, char *argv[])
 {
 	FTS *ftsp;
 	FTSENT *p;
-	mode_t *set;
+	void *set;
+	mode_t mval;
 	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
-	char *mode;
+	char *mode, *reference;
 	int (*change_mode)(const char *, mode_t);
 
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
 
 	Hflag = Lflag = Rflag = fflag = hflag = 0;
-	while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1)
+	reference = NULL;
+	while ((ch = getopt_long(argc, argv, "HLPRXfghorstuwx",
+	    chmod_longopts, NULL)) != -1)
 		switch (ch) {
+		case 1:
+			reference = optarg;
+			break;
 		case 'H':
 			Hflag = 1;
 			Lflag = 0;
@@ -124,7 +138,7 @@ main(int argc, char *argv[])
 done:	argv += optind;
 	argc -= optind;
 
-	if (argc < 2)
+	if (argc < 2 && (argc < 1 && reference == NULL))
 		usage();
 
 	fts_options = FTS_PHYSICAL;
@@ -147,13 +161,23 @@ done:	argv += optind;
 	else
 		change_mode = chmod;
 
-	mode = *argv;
-	if ((set = setmode(mode)) == NULL) {
-		err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
-		/* NOTREACHED */
+	if (reference == NULL) {
+		mode = *argv++;
+		if ((set = setmode(mode)) == NULL) {
+			err(EXIT_FAILURE, "Cannot set file mode `%s'", mode);
+			/* NOTREACHED */
+		}
+		mval = 0;
+	} else {
+		struct stat st;
+
+		if (stat(reference, &st) == -1)
+			err(EXIT_FAILURE, "Cannot stat `%s'", reference);
+		mval = st.st_mode;
+		set = NULL;
 	}
 
-	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) {
+	if ((ftsp = fts_open(argv, fts_options, 0)) == NULL) {
 		err(EXIT_FAILURE, "fts_open");
 		/* NOTREACHED */
 	}
@@ -189,7 +213,8 @@ done:	argv += optind;
 			break;
 		}
 		if ((*change_mode)(p->fts_accpath,
-		    getmode(set, p->fts_statp->st_mode)) && !fflag) {
+		    set ? getmode(set, p->fts_statp->st_mode) : mval)
+		    && !fflag) {
 			warn("%s", p->fts_path);
 			rval = 1;
 		}
@@ -206,8 +231,9 @@ static void
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n",
-	    getprogname());
+	    "Usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n"
+	    "\t%s [-R [-H | -L | -P]] [-fh] --reference=rfile file ...\n",
+	    getprogname(), getprogname());
 	exit(1);
 	/* NOTREACHED */
 }

Reply via email to