aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-03-19 20:56:18 -0500
committerRob Landley <rob@landley.net>2012-03-19 20:56:18 -0500
commitcf6bcb27fb3dc6d196adce31d774229ea5d56dec (patch)
treeb60d2b3104cd26e1758c3b999ab4d96677df9c00 /lib
parentf9fdd3c2ffe6cb4e1450cfcedad66a1e69f90bb6 (diff)
downloadtoybox-cf6bcb27fb3dc6d196adce31d774229ea5d56dec.tar.gz
Unwind gratuitous macros.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c90
1 files changed, 29 insertions, 61 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 4665beb9..f4b81f5e 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -899,73 +899,43 @@ char *num_to_sig(int sig)
/* 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)
-
-#define MODE_WHAT(v, w, x, y, z) { \
- if (x & v) \
- y |= S_I##w##z;\
- }
-#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 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);
+ mode_t new_mode = 0, tmp_mode = 0, tmp_mask = 0;
+
+ 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;
}
- 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 & 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 & 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 @@ string_to_mode(char *mode_str, mode_t base)
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;
}