From 0f296a3a56b52842057e5a2bc653621a3a6c7bec Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 14 Oct 2015 13:21:01 +0200 Subject: libiproute: rename invarg(a,b) to invarg_1_to_2(a,b) invarg(a,b) - "invalid argument", but how a and b enter the message? invarg_1_to_2(a,b) is somewhat easier to read: "invalid argument 'a' to 'b'" Audit of usage revealed a number of bad uses, with too long messages. text data bss dec hex filename 938848 932 17448 957228 e9b2c busybox_old 938788 932 17448 957168 e9af0 busybox_unstripped Signed-off-by: Denys Vlasenko --- libbb/messages.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/messages.c b/libbb/messages.c index c1b7ba252..23e440bcd 100644 --- a/libbb/messages.c +++ b/libbb/messages.c @@ -29,7 +29,7 @@ const char bb_msg_can_not_create_raw_socket[] ALIGN1 = "can't create raw socket" const char bb_msg_perm_denied_are_you_root[] ALIGN1 = "permission denied (are you root?)"; const char bb_msg_you_must_be_root[] ALIGN1 = "you must be root"; const char bb_msg_requires_arg[] ALIGN1 = "%s requires an argument"; -const char bb_msg_invalid_arg[] ALIGN1 = "invalid argument '%s' to '%s'"; +const char bb_msg_invalid_arg_to[] ALIGN1 = "invalid argument '%s' to '%s'"; const char bb_msg_standard_input[] ALIGN1 = "standard input"; const char bb_msg_standard_output[] ALIGN1 = "standard output"; -- cgit v1.2.3-55-g6feb From 93dd9fd90ae284e7878767fe14bcb17e3edd9cf8 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 15 Oct 2015 21:33:34 +0200 Subject: du: extra compat: with -k and -m, round sizes up function old new delta print 36 65 +29 Signed-off-by: Denys Vlasenko --- coreutils/du.c | 37 +++++++++++++++++++++++-------------- libbb/human_readable.c | 11 +++-------- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'libbb') diff --git a/coreutils/du.c b/coreutils/du.c index 9c6ff8800..1889c16bb 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -75,7 +75,7 @@ enum { struct globals { #if ENABLE_FEATURE_HUMAN_READABLE - unsigned long disp_hr; + unsigned long disp_unit; #else unsigned disp_k; #endif @@ -89,18 +89,27 @@ struct globals { #define INIT_G() do { } while (0) -/* FIXME? coreutils' du rounds sizes up: - * for example, 1025k file is shown as "2" by du -m. - * We round to nearest. - */ static void print(unsigned long long size, const char *filename) { /* TODO - May not want to defer error checking here. */ #if ENABLE_FEATURE_HUMAN_READABLE +# if ENABLE_DESKTOP + /* ~30 bytes of code for extra comtat: + * coreutils' du rounds sizes up: + * for example, 1025k file is shown as "2" by du -m. + * We round to nearest if human-readable [too hard to fix], + * else (fixed scale such as -m), we round up. To that end, + * add yet another half of the unit before displaying: + */ + if (G.disp_unit) + size += (G.disp_unit-1) / (unsigned)(512 * 2); +# endif printf("%s\t%s\n", - /* size x 512 / G.disp_hr, show one fractional, - * use suffixes if G.disp_hr == 0 */ - make_human_readable_str(size, 512, G.disp_hr), + /* size x 512 / G.disp_unit. + * If G.disp_unit == 0, show one fractional + * and use suffixes + */ + make_human_readable_str(size, 512, G.disp_unit), filename); #else if (G.disp_k) { @@ -199,10 +208,10 @@ int du_main(int argc UNUSED_PARAM, char **argv) INIT_G(); #if ENABLE_FEATURE_HUMAN_READABLE - IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;) - IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;) + IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;) + IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;) if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ - G.disp_hr = 512; + G.disp_unit = 512; #else IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;) /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */ @@ -220,13 +229,13 @@ int du_main(int argc UNUSED_PARAM, char **argv) opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth); argv += optind; if (opt & OPT_h_for_humans) { - G.disp_hr = 0; + G.disp_unit = 0; } if (opt & OPT_m_mbytes) { - G.disp_hr = 1024*1024; + G.disp_unit = 1024*1024; } if (opt & OPT_k_kbytes) { - G.disp_hr = 1024; + G.disp_unit = 1024; } #else opt_complementary = "H-L:L-H:s-d:d-s:d+"; diff --git a/libbb/human_readable.c b/libbb/human_readable.c index 5c7fc076f..b4e0ef181 100644 --- a/libbb/human_readable.c +++ b/libbb/human_readable.c @@ -14,16 +14,11 @@ * representations (say, powers of 1024) and manipulating coefficients. * The base ten "bytes" output could be handled similarly. * - * 2) This routine always outputs a decimal point and a tenths digit when - * display_unit != 0. Hence, it isn't uncommon for the returned string + * 2) This routine outputs a decimal point and a tenths digit when + * display_unit == 0. Hence, it isn't uncommon for the returned string * to have a length of 5 or 6. * - * It might be nice to add a flag to indicate no decimal digits in - * that case. This could be either an additional parameter, or a - * special value of display_unit. Such a flag would also be nice for du. - * - * Some code to omit the decimal point and tenths digit is sketched out - * and "#if 0"'d below. + * If block_size is also 0, no decimal digits are printed. * * Licensed under GPLv2, see file LICENSE in this source tree. */ -- cgit v1.2.3-55-g6feb From 526d85831e7480b9c7a3673d8dd356a438e6dd74 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 19 Oct 2015 04:27:17 +0200 Subject: libbb: get_uidgid() always called with allow_numeric=1 function old new delta xget_uidgid 30 25 -5 make_device 2188 2183 -5 main 797 792 -5 get_uidgid 240 225 -15 Signed-off-by: Denys Vlasenko --- include/libbb.h | 7 +++---- libbb/appletlib.c | 2 +- libpwdgrp/uidgid_get.c | 42 +++++++++++++++++++----------------------- util-linux/mdev.c | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index f04f555c0..1a3f6d8ce 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -920,14 +920,13 @@ long xuname2uid(const char *name) FAST_FUNC; long xgroup2gid(const char *name) FAST_FUNC; /* wrapper: allows string to contain numeric uid or gid */ unsigned long get_ug_id(const char *s, long FAST_FUNC (*xname2id)(const char *)) FAST_FUNC; -/* from chpst. Does not die, returns 0 on failure */ struct bb_uidgid_t { uid_t uid; gid_t gid; }; -/* always sets uid and gid */ -int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC; -/* always sets uid and gid, allows numeric; exits on failure */ +/* always sets uid and gid; returns 0 on failure */ +int get_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC; +/* always sets uid and gid; exits on failure */ void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC; /* chown-like handling of "user[:[group]" */ void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC; diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 24253cf27..0f83eda4b 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -437,7 +437,7 @@ static void parse_config_file(void) goto pe_label; } *e = ':'; /* get_uidgid needs USER:GROUP syntax */ - if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) { + if (get_uidgid(&sct->m_ugid, s) == 0) { errmsg = "unknown user/group"; goto pe_label; } diff --git a/libpwdgrp/uidgid_get.c b/libpwdgrp/uidgid_get.c index 8388be0da..eeb65191f 100644 --- a/libpwdgrp/uidgid_get.c +++ b/libpwdgrp/uidgid_get.c @@ -28,7 +28,7 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "libbb.h" /* Always sets uid and gid */ -int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok) +int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug) { struct passwd *pwd; struct group *gr; @@ -43,18 +43,16 @@ int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok) /* copies sz-1 bytes, stores terminating '\0' */ safe_strncpy(user, ug, sz); } - if (numeric_ok) { - n = bb_strtou(user, NULL, 10); - if (!errno) { - u->uid = n; - pwd = getpwuid(n); - /* If we have e.g. "500" string without user */ - /* with uid 500 in /etc/passwd, we set gid == uid */ - u->gid = pwd ? pwd->pw_gid : n; - goto skip; - } + n = bb_strtou(user, NULL, 10); + if (!errno) { + u->uid = n; + pwd = getpwuid(n); + /* If we have e.g. "500" string without user */ + /* with uid 500 in /etc/passwd, we set gid == uid */ + u->gid = pwd ? pwd->pw_gid : n; + goto skip; } - /* Either it is not numeric, or caller disallows numeric username */ + /* it is not numeric */ pwd = getpwnam(user); if (!pwd) return 0; @@ -63,12 +61,10 @@ int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok) skip: if (group) { - if (numeric_ok) { - n = bb_strtou(group, NULL, 10); - if (!errno) { - u->gid = n; - return 1; - } + n = bb_strtou(group, NULL, 10); + if (!errno) { + u->gid = n; + return 1; } gr = getgrnam(group); if (!gr) @@ -79,7 +75,7 @@ int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok) } void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug) { - if (!get_uidgid(u, ug, 1)) + if (!get_uidgid(u, ug)) bb_error_msg_and_die("unknown user/group %s", ug); } @@ -119,16 +115,16 @@ int main() { unsigned u; struct bb_uidgid_t ug; - u = get_uidgid(&ug, "apache", 0); + u = get_uidgid(&ug, "apache"); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = get_uidgid(&ug, "apache", 0); + u = get_uidgid(&ug, "apache"); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = get_uidgid(&ug, "apache:users", 0); + u = get_uidgid(&ug, "apache:users"); printf("%u = %u:%u\n", u, ug.uid, ug.gid); ug.uid = ug.gid = 1111; - u = get_uidgid(&ug, "apache:users", 0); + u = get_uidgid(&ug, "apache:users"); printf("%u = %u:%u\n", u, ug.uid, ug.gid); return 0; } diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 51781d597..37fa56827 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -400,7 +400,7 @@ static void parse_next_rule(void) } /* 2nd field: uid:gid - device ownership */ - if (get_uidgid(&G.cur_rule.ugid, tokens[1], /*allow_numeric:*/ 1) == 0) { + if (get_uidgid(&G.cur_rule.ugid, tokens[1]) == 0) { bb_error_msg("unknown user/group '%s' on line %d", tokens[1], G.parser->lineno); goto next_rule; } -- cgit v1.2.3-55-g6feb