chmod patch

Liam J. Foy liamfoy at sepulcrum.org
Mon Oct 25 13:11:47 PDT 2004


	Hey, can you please give me the go ahead for the following patch:

-Setmode will deal with the parsing, so we dont need to.
-Setmode can also be caused to fail because of malloc().
 We test to see setmode() has failed. If it has we check errno.
 If errno == 0 then we have an invalid file mode. If not malloc
 has failed and we the print the error via err().
-Remove '?' case.
-Fix some data types. This is for the setmode fix above.

Index: chmod.c
===================================================================
RCS file: /home/dcvs/src/bin/chmod/chmod.c,v
retrieving revision 1.6
diff -u -r1.6 chmod.c
--- chmod.c	25 Aug 2004 01:15:38 -0000	1.6
+++ chmod.c	25 Oct 2004 20:59:18 -0000
@@ -55,16 +55,12 @@
 {
 	FTS *ftsp;
 	FTSENT *p;
-	mode_t *set;
-	long val;
-	int oct, omode, newmode;
+	mode_t *set, newmode;
 	int Hflag, Lflag, Pflag, Rflag, ch, fflag;
 	int fts_options, hflag, rval, vflag;
-	char *ep, *mode;
+	char *mode;
 	int (*change_mode) (const char *, mode_t);
 
-	set = NULL;
-	omode = 0;
 	Hflag = Lflag = Pflag = Rflag = fflag = hflag = vflag = 0;
 	while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1)
 		switch (ch) {
@@ -113,7 +109,6 @@
 		case 'v':
 			vflag = 1;
 			break;
-		case '?':
 		default:
 			usage();
 		}
@@ -142,21 +137,13 @@
 		change_mode = chmod;
 
 	mode = *argv;
-	if (*mode >= '0' && *mode <= '7') {
-		errno = 0;
-		val = strtol(mode, &ep, 8);
-		if (val > INT_MAX || val < 0)
-			errno = ERANGE;
-		if (errno)
-			err(1, "invalid file mode: %s", mode);
-		if (*ep)
+	errno = 0;
+	if ((set = setmode(mode)) == NULL) {
+		if (!errno)
 			errx(1, "invalid file mode: %s", mode);
-		omode = val;
-		oct = 1;
-	} else {
-		if ((set = setmode(mode)) == NULL)
-			errx(1, "invalid file mode: %s", mode);
-		oct = 0;
+		else
+			/* malloc for setmode() failed */
+			err(1, "setmode failed");
 	}
 
 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
@@ -190,7 +177,7 @@
 		default:
 			break;
 		}
-		newmode = oct ? omode : getmode(set, p->fts_statp->st_mode);
+		newmode = getmode(set, p->fts_statp->st_mode);
 		if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
 			continue;
 		if ((*change_mode)(p->fts_accpath, newmode) && !fflag) {


Cheers!

-- 
- Liam J. Foy
liamfoy at xxxxxxxxxxxxx





More information about the Kernel mailing list