diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-10-07 15:10:20 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-01-10 09:53:31 +0100 |
commit | c40e45b75eb76d79a05c7fa85c1fa9b5c728a12f (patch) | |
tree | ad4f2519067709f00ab98b3c591186c26dc3a21f /freebsd/lib/libutil | |
parent | userspace-header-gen.py: Simplify program ports (diff) | |
download | rtems-libbsd-c40e45b75eb76d79a05c7fa85c1fa9b5c728a12f.tar.bz2 |
Update to FreeBSD head 2016-08-23
Git mirror commit 9fe7c416e6abb28b1398fd3e5687099846800cfd.
Diffstat (limited to 'freebsd/lib/libutil')
-rw-r--r-- | freebsd/lib/libutil/expand_number.c | 30 | ||||
-rw-r--r-- | freebsd/lib/libutil/humanize_number.c | 74 | ||||
-rw-r--r-- | freebsd/lib/libutil/libutil.h | 17 |
3 files changed, 66 insertions, 55 deletions
diff --git a/freebsd/lib/libutil/expand_number.c b/freebsd/lib/libutil/expand_number.c index 893a1d77..b004f5fc 100644 --- a/freebsd/lib/libutil/expand_number.c +++ b/freebsd/lib/libutil/expand_number.c @@ -37,31 +37,24 @@ __FBSDID("$FreeBSD$"); #include <libutil.h> #include <stdint.h> -/* - * Convert an expression of the following forms to a uint64_t. - * 1) A positive decimal number. - * 2) A positive decimal number followed by a 'b' or 'B' (mult by 1). - * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). - * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). - * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). - * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). - * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). - * 8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60). - */ int expand_number(const char *buf, uint64_t *num) { + char *endptr; + uintmax_t umaxval; uint64_t number; unsigned shift; - char *endptr; - - number = strtoumax(buf, &endptr, 0); + int serrno; - if (endptr == buf) { - /* No valid digits. */ - errno = EINVAL; + serrno = errno; + errno = 0; + umaxval = strtoumax(buf, &endptr, 0); + if (umaxval > UINT64_MAX) + errno = ERANGE; + if (errno != 0) return (-1); - } + errno = serrno; + number = umaxval; switch (tolower((unsigned char)*endptr)) { case 'e': @@ -97,7 +90,6 @@ expand_number(const char *buf, uint64_t *num) errno = ERANGE; return (-1); } - *num = number << shift; return (0); } diff --git a/freebsd/lib/libutil/humanize_number.c b/freebsd/lib/libutil/humanize_number.c index da5c5197..3bb729a9 100644 --- a/freebsd/lib/libutil/humanize_number.c +++ b/freebsd/lib/libutil/humanize_number.c @@ -4,6 +4,7 @@ /* * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc. + * Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org> * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -52,15 +53,26 @@ humanize_number(char *buf, size_t len, int64_t quotient, { const char *prefixes, *sep; int i, r, remainder, s1, s2, sign; + int divisordeccut; int64_t divisor, max; size_t baselen; - assert(buf != NULL); - assert(suffix != NULL); - assert(scale >= 0); - assert(scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0))); - assert(!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))); + /* Since so many callers don't check -1, NUL terminate the buffer */ + if (len > 0) + buf[0] = '\0'; + /* validate args */ + if (buf == NULL || suffix == NULL) + return (-1); + if (scale < 0) + return (-1); + else if (scale >= maxscale && + ((scale & ~(HN_AUTOSCALE|HN_GETSCALE)) != 0)) + return (-1); + if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES)) + return (-1); + + /* setup parameters */ remainder = 0; if (flags & HN_IEC_PREFIXES) { @@ -75,34 +87,32 @@ humanize_number(char *buf, size_t len, int64_t quotient, * an assertion earlier). */ divisor = 1024; + divisordeccut = 973; /* ceil(.95 * 1024) */ if (flags & HN_B) prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei"; else prefixes = "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei"; } else { baselen = 1; - if (flags & HN_DIVISOR_1000) + if (flags & HN_DIVISOR_1000) { divisor = 1000; - else + divisordeccut = 950; + if (flags & HN_B) + prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; + else + prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; + } else { divisor = 1024; - - if (flags & HN_B) - prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; - else - prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E"; + divisordeccut = 973; /* ceil(.95 * 1024) */ + if (flags & HN_B) + prefixes = "B\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E"; + else + prefixes = "\0\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E"; + } } #define SCALE2PREFIX(scale) (&prefixes[(scale) * 3]) - if (scale < 0 || (scale >= maxscale && - (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)) - return (-1); - - if (buf == NULL || suffix == NULL) - return (-1); - - if (len > 0) - buf[0] = '\0'; if (quotient < 0) { sign = -1; quotient = -quotient; @@ -134,8 +144,8 @@ humanize_number(char *buf, size_t len, int64_t quotient, * divide once more. */ for (i = 0; - (quotient >= max || (quotient == max - 1 && remainder >= 950)) && - i < maxscale; i++) { + (quotient >= max || (quotient == max - 1 && + remainder >= divisordeccut)) && i < maxscale; i++) { remainder = quotient % divisor; quotient /= divisor; } @@ -150,20 +160,22 @@ humanize_number(char *buf, size_t len, int64_t quotient, } /* If a value <= 9.9 after rounding and ... */ - if (quotient <= 9 && remainder < 950 && i > 0 && flags & HN_DECIMAL) { - /* baselen + \0 + .N */ - if (len < baselen + 1 + 2) - return (-1); - s1 = (int)quotient + ((remainder + 50) / 1000); - s2 = ((remainder + 50) / 100) % 10; + /* + * XXX - should we make sure there is enough space for the decimal + * place and if not, don't do HN_DECIMAL? + */ + if (((quotient == 9 && remainder < divisordeccut) || quotient < 9) && + i > 0 && flags & HN_DECIMAL) { + s1 = (int)quotient + ((remainder * 10 + divisor / 2) / + divisor / 10); + s2 = ((remainder * 10 + divisor / 2) / divisor) % 10; r = snprintf(buf, len, "%d%s%d%s%s%s", sign * s1, localeconv()->decimal_point, s2, sep, SCALE2PREFIX(i), suffix); } else r = snprintf(buf, len, "%" PRId64 "%s%s%s", - sign * (quotient + (remainder + 50) / 1000), + sign * (quotient + (remainder + divisor / 2) / divisor), sep, SCALE2PREFIX(i), suffix); return (r); } - diff --git a/freebsd/lib/libutil/libutil.h b/freebsd/lib/libutil/libutil.h index 60ba3235..b20ffa29 100644 --- a/freebsd/lib/libutil/libutil.h +++ b/freebsd/lib/libutil/libutil.h @@ -102,6 +102,8 @@ struct kinfo_file * kinfo_getfile(pid_t _pid, int *_cntp); struct kinfo_vmentry * kinfo_getvmmap(pid_t _pid, int *_cntp); +struct kinfo_vmobject * + kinfo_getvmobject(int *_cntp); struct kinfo_proc * kinfo_getallproc(int *_cntp); struct kinfo_proc * @@ -162,16 +164,21 @@ int pw_tmp(int _mfd); #endif #ifdef _GRP_H_ -int gr_copy(int __ffd, int _tfd, const struct group *_gr, struct group *_old_gr); -struct group *gr_dup(const struct group *gr); -int gr_equal(const struct group *gr1, const struct group *gr2); +int gr_copy(int __ffd, int _tfd, const struct group *_gr, + struct group *_old_gr); +struct group * + gr_dup(const struct group *_gr); +struct group * + gr_add(const struct group *_gr, const char *_newmember); +int gr_equal(const struct group *_gr1, const struct group *_gr2); void gr_fini(void); int gr_init(const char *_dir, const char *_master); int gr_lock(void); -char *gr_make(const struct group *gr); +char *gr_make(const struct group *_gr); int gr_mkdb(void); +struct group * + gr_scan(const char *_line); int gr_tmp(int _mdf); -struct group *gr_scan(const char *line); #endif #ifdef _UFS_UFS_QUOTA_H_ |