changeset 553:75da5d793fc8

Unwind gratuitous macros.
author Rob Landley <rob@landley.net>
date Mon, 19 Mar 2012 20:56:18 -0500
parents 7b85b0a0e9c0
children 997e016fbdf0
files lib/lib.c
diffstat 1 files changed, 29 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Mon Mar 19 20:15:08 2012 -0500
+++ b/lib/lib.c	Mon Mar 19 20:56:18 2012 -0500
@@ -899,73 +899,43 @@
 
 /* mode parsing */
 
-#define	USR_FLAGS	(S_IRWXU)
-#define	GRP_FLAGS	(S_IRWXG)
-#define	OTH_FLAGS	(S_IRWXO)
-#define SGT_FLAGS	(S_ISUID | S_ISGID | S_ISVTX)
+mode_t apply_mode(int who, int how, int what, mode_t base)
+{
+	mode_t new_mode = 0, tmp_mode = 0, tmp_mask = 0;
 
-#define MODE_WHAT(v, w, x, y, z) { \
-	if (x & v) \
-		y |= S_I##w##z;\
+	if (!who) tmp_mask = 07777;
+	if (who & 0x01) { // u
+		if (what & 0x01) tmp_mode |= S_IXUSR;
+		if (what & 0x02) tmp_mode |= S_IWUSR;
+		if (what & 0x04) tmp_mode |= S_IRUSR;
+		if (what & 0x08) tmp_mode |= S_ISUID;
+		tmp_mask |= S_IRWXU;
 	}
-#define MODE_READ(x, y, z) MODE_WHAT(0x04, R, x, y, z)
-#define MODE_WRITE(x, y, z) MODE_WHAT(0x02, W, x, y, z)
-#define MODE_EXEC(x, y, z) MODE_WHAT(0x01, X, x, y, z)
-#define MODE_SUID(x, y) MODE_WHAT(0x08, S, x, y, UID)
-#define MODE_SGID(x, y) MODE_WHAT(0x08, S, x, y, GID)
-#define MODE_SVTX(x, y) MODE_WHAT(0x10, S, x, y, VTX)
-
-mode_t
-apply_mode(int who, int how, int what, mode_t base)
-{
-	mode_t new_mode = 0;
-	mode_t tmp_mode = 0;
-	mode_t tmp_mask = USR_FLAGS | GRP_FLAGS | OTH_FLAGS | SGT_FLAGS;
-	if (who & 0x01) {
-		/* u */
-		MODE_READ(what, tmp_mode, USR);
-		MODE_WRITE(what, tmp_mode, USR);
-		MODE_EXEC(what, tmp_mode, USR);
-		MODE_SUID(what, tmp_mode)
-		tmp_mask &= (GRP_FLAGS | OTH_FLAGS | SGT_FLAGS);
+	if (who & 0x02) { // g
+		if (what & 0x01) tmp_mode |= S_IXGRP;
+		if (what & 0x02) tmp_mode |= S_IWUSR;
+		if (what & 0x04) tmp_mode |= S_IRUSR;
+		if (what & 0x08) tmp_mode |= S_ISGID;
+		tmp_mask |= S_IRWXG;
 	}
-	if (who & 0x02) {
-		/* g */
-		MODE_READ(what, tmp_mode, GRP);
-		MODE_WRITE(what, tmp_mode, GRP);
-		MODE_EXEC(what, tmp_mode, GRP);
-		MODE_SGID(what, tmp_mode)
-		tmp_mask &= (USR_FLAGS | OTH_FLAGS | SGT_FLAGS);
-	}
-	if (who & 0x04) {
-		/* o */
-		MODE_READ(what, tmp_mode, OTH);
-		MODE_WRITE(what, tmp_mode, OTH);
-		MODE_EXEC(what, tmp_mode, OTH);
-		tmp_mask &= (USR_FLAGS | GRP_FLAGS | SGT_FLAGS);
+	if (who & 0x04) { // o
+		if (what & 0x01) tmp_mode |= S_IXOTH;
+		if (what & 0x02) tmp_mode |= S_IWOTH;
+		if (what & 0x04) tmp_mode |= S_IROTH;
+		tmp_mask |= S_IRWXO;
 	}
 	/* check sticky */
-	MODE_SVTX(what, tmp_mode);
-	switch (how){
-	case 1:
-		/* set */
-		new_mode = tmp_mode | (base & tmp_mask);
-		break;
-	case 2:
-		/* add */
-		new_mode = base | tmp_mode;
-		break;
-	case 3:
-		/* remove */
-		new_mode = base & ~(tmp_mode);
-		break;
-	}
+	if (what & 0x10) tmp_mode |= S_ISVTX;
+
+	if (how == 1) new_mode = tmp_mode | (base & tmp_mask);
+	else if (how == 2) new_mode = base | tmp_mode;
+	else if (how == 3) new_mode = base & ~(tmp_mode);
+
 	return new_mode;
 }
 
 
-mode_t
-string_to_mode(char *mode_str, mode_t base)
+mode_t string_to_mode(char *mode_str, mode_t base)
 {
 	mode_t new_mode;
 	int what = 0;
@@ -977,9 +947,7 @@
 		return base;
 	if (isdigit(mode_str[0])) {
 		tmp = strtol(mode_str, &p, 8);
-		if (*p || tmp < 0 ||
-			(tmp & ~(OTH_FLAGS | SGT_FLAGS | GRP_FLAGS | USR_FLAGS)))
-			return base;
+		if (*p || tmp < 0 || (tmp & ~(07777))) return base;
 		new_mode = (mode_t) tmp;
 		return new_mode;
 	}