diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-07 17:55:33 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-07 17:55:33 +0200 |
commit | 5711a2a4ad51ad203a2ed4ffc72593e83920b36a (patch) | |
tree | ef650852b982768fa9c3e4065016e09cbf2d0a96 | |
parent | c1e2e005b4e99070f58a3545bad54ef41a634ad1 (diff) | |
download | busybox-w32-5711a2a4ad51ad203a2ed4ffc72593e83920b36a.tar.gz busybox-w32-5711a2a4ad51ad203a2ed4ffc72593e83920b36a.tar.bz2 busybox-w32-5711a2a4ad51ad203a2ed4ffc72593e83920b36a.zip |
libbb: more compact API for bb_parse_mode()
function old new delta
make_device 2182 2188 +6
parse_command 1440 1443 +3
parse_params 1497 1499 +2
install_main 773 769 -4
mkdir_main 168 160 -8
getoptscmd 641 632 -9
builtin_umask 158 147 -11
bb_parse_mode 431 410 -21
umaskcmd 286 258 -28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/6 up/down: 11/-81) Total: -70 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/chmod.c | 4 | ||||
-rw-r--r-- | coreutils/install.c | 2 | ||||
-rw-r--r-- | coreutils/libcoreutils/getopt_mk_fifo_nod.c | 4 | ||||
-rw-r--r-- | coreutils/mkdir.c | 4 | ||||
-rw-r--r-- | findutils/find.c | 3 | ||||
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/parse_mode.c | 16 | ||||
-rw-r--r-- | shell/ash.c | 3 | ||||
-rw-r--r-- | shell/hush.c | 7 | ||||
-rw-r--r-- | util-linux/mdev.c | 2 |
10 files changed, 26 insertions, 22 deletions
diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 5ee45b942..a21c6d501 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c | |||
@@ -69,9 +69,9 @@ static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void | |||
69 | if (S_ISLNK(statbuf->st_mode)) | 69 | if (S_ISLNK(statbuf->st_mode)) |
70 | return TRUE; | 70 | return TRUE; |
71 | } | 71 | } |
72 | newmode = statbuf->st_mode; | ||
73 | 72 | ||
74 | if (!bb_parse_mode((char *)param, &newmode)) | 73 | newmode = bb_parse_mode((char *)param, statbuf->st_mode); |
74 | if (newmode == (mode_t)-1) | ||
75 | bb_error_msg_and_die("invalid mode '%s'", (char *)param); | 75 | bb_error_msg_and_die("invalid mode '%s'", (char *)param); |
76 | 76 | ||
77 | if (chmod(fileName, newmode) == 0) { | 77 | if (chmod(fileName, newmode) == 0) { |
diff --git a/coreutils/install.c b/coreutils/install.c index 73f9c70d5..8aa51cc34 100644 --- a/coreutils/install.c +++ b/coreutils/install.c | |||
@@ -159,7 +159,7 @@ int install_main(int argc, char **argv) | |||
159 | } | 159 | } |
160 | mode = 0755; /* GNU coreutils 6.10 compat */ | 160 | mode = 0755; /* GNU coreutils 6.10 compat */ |
161 | if (opts & OPT_MODE) | 161 | if (opts & OPT_MODE) |
162 | bb_parse_mode(mode_str, &mode); | 162 | mode = bb_parse_mode(mode_str, mode); |
163 | uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); | 163 | uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); |
164 | gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); | 164 | gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); |
165 | 165 | ||
diff --git a/coreutils/libcoreutils/getopt_mk_fifo_nod.c b/coreutils/libcoreutils/getopt_mk_fifo_nod.c index 222717149..47375ff91 100644 --- a/coreutils/libcoreutils/getopt_mk_fifo_nod.c +++ b/coreutils/libcoreutils/getopt_mk_fifo_nod.c | |||
@@ -33,7 +33,9 @@ mode_t FAST_FUNC getopt_mk_fifo_nod(char **argv) | |||
33 | int opt; | 33 | int opt; |
34 | opt = getopt32(argv, "m:" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); | 34 | opt = getopt32(argv, "m:" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); |
35 | if (opt & 1) { | 35 | if (opt & 1) { |
36 | if (bb_parse_mode(smode, &mode)) | 36 | mode = bb_parse_mode(smode, mode); |
37 | if (mode != (mode_t)-1) /* if mode is valid */ | ||
38 | /* make future mknod/mkfifo set mode bits exactly */ | ||
37 | umask(0); | 39 | umask(0); |
38 | } | 40 | } |
39 | 41 | ||
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c index 864edfb0a..6f7b004dd 100644 --- a/coreutils/mkdir.c +++ b/coreutils/mkdir.c | |||
@@ -71,8 +71,8 @@ int mkdir_main(int argc UNUSED_PARAM, char **argv) | |||
71 | #endif | 71 | #endif |
72 | opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); | 72 | opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); |
73 | if (opt & 1) { | 73 | if (opt & 1) { |
74 | mode_t mmode = 0777; | 74 | mode_t mmode = bb_parse_mode(smode, 0777); |
75 | if (!bb_parse_mode(smode, &mmode)) { | 75 | if (mmode == (mode_t)-1) { |
76 | bb_error_msg_and_die("invalid mode '%s'", smode); | 76 | bb_error_msg_and_die("invalid mode '%s'", smode); |
77 | } | 77 | } |
78 | mode = mmode; | 78 | mode = mmode; |
diff --git a/findutils/find.c b/findutils/find.c index ced8922e7..f72cad7d1 100644 --- a/findutils/find.c +++ b/findutils/find.c | |||
@@ -1261,7 +1261,8 @@ static action*** parse_params(char **argv) | |||
1261 | ap->perm_char = arg1[0]; | 1261 | ap->perm_char = arg1[0]; |
1262 | arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1)); | 1262 | arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1)); |
1263 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */ | 1263 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */ |
1264 | if (!bb_parse_mode(arg1, &ap->perm_mask)) | 1264 | ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask); |
1265 | if (ap->perm_mask == (mode_t)-1) | ||
1265 | bb_error_msg_and_die("invalid mode '%s'", arg1); | 1266 | bb_error_msg_and_die("invalid mode '%s'", arg1); |
1266 | } | 1267 | } |
1267 | #endif | 1268 | #endif |
diff --git a/include/libbb.h b/include/libbb.h index 543214ea4..d79843a2d 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1251,7 +1251,8 @@ char *bb_ask_stdin(const char * prompt) FAST_FUNC; | |||
1251 | char *bb_ask(const int fd, int timeout, const char * prompt) FAST_FUNC; | 1251 | char *bb_ask(const int fd, int timeout, const char * prompt) FAST_FUNC; |
1252 | int bb_ask_confirmation(void) FAST_FUNC; | 1252 | int bb_ask_confirmation(void) FAST_FUNC; |
1253 | 1253 | ||
1254 | int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC; | 1254 | /* Returns -1 if input is invalid. current_mode is a base for e.g. "u+rw" */ |
1255 | int bb_parse_mode(const char* s, unsigned cur_mode) FAST_FUNC; | ||
1255 | 1256 | ||
1256 | /* | 1257 | /* |
1257 | * Config file parser | 1258 | * Config file parser |
diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c index 5a4e1c579..bddd39bca 100644 --- a/libbb/parse_mode.c +++ b/libbb/parse_mode.c | |||
@@ -15,7 +15,7 @@ | |||
15 | 15 | ||
16 | #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) | 16 | #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) |
17 | 17 | ||
18 | int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) | 18 | int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode) |
19 | { | 19 | { |
20 | static const mode_t who_mask[] = { | 20 | static const mode_t who_mask[] = { |
21 | S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */ | 21 | S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */ |
@@ -46,13 +46,12 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) | |||
46 | 46 | ||
47 | tmp = strtoul(s, &e, 8); | 47 | tmp = strtoul(s, &e, 8); |
48 | if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ | 48 | if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ |
49 | return 0; | 49 | return -1; |
50 | } | 50 | } |
51 | *current_mode = tmp; | 51 | return tmp; |
52 | return 1; | ||
53 | } | 52 | } |
54 | 53 | ||
55 | new_mode = *current_mode; | 54 | new_mode = current_mode; |
56 | 55 | ||
57 | /* Note: we allow empty clauses, and hence empty modes. | 56 | /* Note: we allow empty clauses, and hence empty modes. |
58 | * We treat an empty mode as no change to perms. */ | 57 | * We treat an empty mode as no change to perms. */ |
@@ -71,7 +70,7 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) | |||
71 | if (*p == *s) { | 70 | if (*p == *s) { |
72 | wholist |= who_mask[(int)(p-who_chars)]; | 71 | wholist |= who_mask[(int)(p-who_chars)]; |
73 | if (!*++s) { | 72 | if (!*++s) { |
74 | return 0; | 73 | return -1; |
75 | } | 74 | } |
76 | goto WHO_LIST; | 75 | goto WHO_LIST; |
77 | } | 76 | } |
@@ -80,7 +79,7 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) | |||
80 | do { /* Process action list. */ | 79 | do { /* Process action list. */ |
81 | if ((*s != '+') && (*s != '-')) { | 80 | if ((*s != '+') && (*s != '-')) { |
82 | if (*s != '=') { | 81 | if (*s != '=') { |
83 | return 0; | 82 | return -1; |
84 | } | 83 | } |
85 | /* Since op is '=', clear all bits corresponding to the | 84 | /* Since op is '=', clear all bits corresponding to the |
86 | * wholist, or all file bits if wholist is empty. */ | 85 | * wholist, or all file bits if wholist is empty. */ |
@@ -145,6 +144,5 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) | |||
145 | } while (*s && (*s != ',')); | 144 | } while (*s && (*s != ',')); |
146 | } | 145 | } |
147 | 146 | ||
148 | *current_mode = new_mode; | 147 | return new_mode; |
149 | return 1; | ||
150 | } | 148 | } |
diff --git a/shell/ash.c b/shell/ash.c index 80dfc1d6a..ab8ec006f 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -12862,7 +12862,8 @@ umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | |||
12862 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ | 12862 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
12863 | if (!isdigit(modestr[0])) | 12863 | if (!isdigit(modestr[0])) |
12864 | mask ^= 0777; | 12864 | mask ^= 0777; |
12865 | if (!bb_parse_mode(modestr, &mask) || (unsigned)mask > 0777) { | 12865 | mask = bb_parse_mode(modestr, mask); |
12866 | if ((unsigned)mask > 0777) { | ||
12866 | ash_msg_and_raise_error("illegal mode: %s", modestr); | 12867 | ash_msg_and_raise_error("illegal mode: %s", modestr); |
12867 | } | 12868 | } |
12868 | if (!isdigit(modestr[0])) | 12869 | if (!isdigit(modestr[0])) |
diff --git a/shell/hush.c b/shell/hush.c index 8b8d5fc8b..bccd9c1e9 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -8965,6 +8965,7 @@ static int FAST_FUNC builtin_umask(char **argv) | |||
8965 | int rc; | 8965 | int rc; |
8966 | mode_t mask; | 8966 | mode_t mask; |
8967 | 8967 | ||
8968 | rc = 1; | ||
8968 | mask = umask(0); | 8969 | mask = umask(0); |
8969 | argv = skip_dash_dash(argv); | 8970 | argv = skip_dash_dash(argv); |
8970 | if (argv[0]) { | 8971 | if (argv[0]) { |
@@ -8974,19 +8975,19 @@ static int FAST_FUNC builtin_umask(char **argv) | |||
8974 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ | 8975 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
8975 | if (!isdigit(argv[0][0])) | 8976 | if (!isdigit(argv[0][0])) |
8976 | mask ^= 0777; | 8977 | mask ^= 0777; |
8977 | rc = bb_parse_mode(argv[0], &mask); | 8978 | mask = bb_parse_mode(argv[0], mask); |
8978 | if (!isdigit(argv[0][0])) | 8979 | if (!isdigit(argv[0][0])) |
8979 | mask ^= 0777; | 8980 | mask ^= 0777; |
8980 | if (rc == 0 || (unsigned)mask > 0777) { | 8981 | if ((unsigned)mask > 0777) { |
8981 | mask = old_mask; | 8982 | mask = old_mask; |
8982 | /* bash messages: | 8983 | /* bash messages: |
8983 | * bash: umask: 'q': invalid symbolic mode operator | 8984 | * bash: umask: 'q': invalid symbolic mode operator |
8984 | * bash: umask: 999: octal number out of range | 8985 | * bash: umask: 999: octal number out of range |
8985 | */ | 8986 | */ |
8986 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); | 8987 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
8988 | rc = 0; | ||
8987 | } | 8989 | } |
8988 | } else { | 8990 | } else { |
8989 | rc = 1; | ||
8990 | /* Mimic bash */ | 8991 | /* Mimic bash */ |
8991 | printf("%04o\n", (unsigned) mask); | 8992 | printf("%04o\n", (unsigned) mask); |
8992 | /* fall through and restore mask which we set to 0 */ | 8993 | /* fall through and restore mask which we set to 0 */ |
diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 662e8ab38..51781d597 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c | |||
@@ -406,7 +406,7 @@ static void parse_next_rule(void) | |||
406 | } | 406 | } |
407 | 407 | ||
408 | /* 3rd field: mode - device permissions */ | 408 | /* 3rd field: mode - device permissions */ |
409 | bb_parse_mode(tokens[2], &G.cur_rule.mode); | 409 | G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode); |
410 | 410 | ||
411 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ | 411 | /* 4th field (opt): ">|=alias" or "!" to not create the node */ |
412 | val = tokens[3]; | 412 | val = tokens[3]; |