17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5d362b749Svk199839 * Common Development and Distribution License (the "License").
6d362b749Svk199839 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
22*23a1cceaSRoger A. Faulkner /*
23*23a1cceaSRoger A. Faulkner * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
24*23a1cceaSRoger A. Faulkner */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <libintl.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdarg.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include "utils.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static char PNAME_FMT[] = "%s: ";
397c478bd9Sstevel@tonic-gate static char ERRNO_FMT[] = ": %s\n";
407c478bd9Sstevel@tonic-gate static char EOL_FMT[] = "\n";
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate static char *pname;
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate char *
setpname(char * arg0)45*23a1cceaSRoger A. Faulkner setpname(char *arg0)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate char *p = strrchr(arg0, '/');
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate if (p == NULL)
507c478bd9Sstevel@tonic-gate p = arg0;
517c478bd9Sstevel@tonic-gate else
527c478bd9Sstevel@tonic-gate p++;
537c478bd9Sstevel@tonic-gate pname = p;
547c478bd9Sstevel@tonic-gate return (pname);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
587c478bd9Sstevel@tonic-gate void
warn(const char * format,...)59d362b749Svk199839 warn(const char *format, ...)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate int err = errno;
627c478bd9Sstevel@tonic-gate va_list alist;
637c478bd9Sstevel@tonic-gate if (pname != NULL)
647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(PNAME_FMT), pname);
657c478bd9Sstevel@tonic-gate va_start(alist, format);
667c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
677c478bd9Sstevel@tonic-gate va_end(alist);
687c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
697c478bd9Sstevel@tonic-gate if (err)
707c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
717c478bd9Sstevel@tonic-gate gettext(ERRNO_FMT), strerror(err));
727c478bd9Sstevel@tonic-gate else
737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(EOL_FMT));
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate static char *__metric_modifiers[] = { "K", "M", "G", "T", "P", "E", NULL };
797c478bd9Sstevel@tonic-gate static uint64_t __metric_scales[] = {
807c478bd9Sstevel@tonic-gate 1000LLU,
817c478bd9Sstevel@tonic-gate 1000LLU * 1000,
827c478bd9Sstevel@tonic-gate 1000LLU * 1000 * 1000,
837c478bd9Sstevel@tonic-gate 1000LLU * 1000 * 1000 * 1000,
847c478bd9Sstevel@tonic-gate 1000LLU * 1000 * 1000 * 1000 * 1000,
857c478bd9Sstevel@tonic-gate 1000LLU * 1000 * 1000 * 1000 * 1000 * 1000
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate static scale_t __metric_scale = { __metric_modifiers, __metric_scales };
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static char *__binary_modifiers[] = {"K", "M", "G", "T", "P", "E", NULL};
907c478bd9Sstevel@tonic-gate static uint64_t __binary_scales[] = {
917c478bd9Sstevel@tonic-gate 1024LLU,
927c478bd9Sstevel@tonic-gate 1024LLU * 1024,
937c478bd9Sstevel@tonic-gate 1024LLU * 1024 * 1024,
947c478bd9Sstevel@tonic-gate 1024LLU * 1024 * 1024 * 1024,
957c478bd9Sstevel@tonic-gate 1024LLU * 1024 * 1024 * 1024 * 1024,
967c478bd9Sstevel@tonic-gate 1024LLU * 1024 * 1024 * 1024 * 1024 * 1024
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate static scale_t __binary_scale = { __binary_modifiers, __binary_scales };
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate scale_t *scale_metric = &__metric_scale;
1017c478bd9Sstevel@tonic-gate scale_t *scale_binary = &__binary_scale;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate int
scaledtouint64(char * scaledin,uint64_t * uint64out,int * widthout,char ** modifierout,char ** unitout,scale_t * scale,char * unit,int flags)1047c478bd9Sstevel@tonic-gate scaledtouint64(char *scaledin,
1057c478bd9Sstevel@tonic-gate uint64_t *uint64out,
1067c478bd9Sstevel@tonic-gate int *widthout, char **modifierout, char **unitout,
1077c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags) {
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate double result;
1107c478bd9Sstevel@tonic-gate double value;
1117c478bd9Sstevel@tonic-gate int index = 0;
1127c478bd9Sstevel@tonic-gate uint64_t multiplier = 1;
1137c478bd9Sstevel@tonic-gate char string[SCALED_STRLEN];
1147c478bd9Sstevel@tonic-gate char *endptr;
1157c478bd9Sstevel@tonic-gate int cmp;
1167c478bd9Sstevel@tonic-gate int hasmodifier = 0;
1177c478bd9Sstevel@tonic-gate char **modifiers = scale->modifers;
1187c478bd9Sstevel@tonic-gate uint64_t *scales = scale->scales;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate if (modifierout)
1217c478bd9Sstevel@tonic-gate *modifierout = NULL;
1227c478bd9Sstevel@tonic-gate if (unitout)
1237c478bd9Sstevel@tonic-gate *unitout = NULL;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * first check for hex value, which cannot be scaled, as
1277c478bd9Sstevel@tonic-gate * hex letters cannot be disserned from modifier or unit letters
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate if ((strncmp("0x", scaledin, 2) == 0) ||
1307c478bd9Sstevel@tonic-gate (strncmp("0X", scaledin, 2) == 0)) {
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* unit cannot be required on hex values */
1337c478bd9Sstevel@tonic-gate if ((unit && *unit != '\0') &&
1347c478bd9Sstevel@tonic-gate !(flags & SCALED_UNIT_OPTIONAL_FLAG))
1357c478bd9Sstevel@tonic-gate return (SCALED_INVALID_UNIT);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate errno = 0;
1387c478bd9Sstevel@tonic-gate *uint64out = strtoull(scaledin, &endptr, 16);
1397c478bd9Sstevel@tonic-gate if (errno) {
1407c478bd9Sstevel@tonic-gate if (errno == ERANGE)
1417c478bd9Sstevel@tonic-gate return (SCALED_OVERFLOW);
1427c478bd9Sstevel@tonic-gate else
1437c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (*endptr != '\0')
1467c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /* compute width of decimal equivalent */
1497c478bd9Sstevel@tonic-gate if (widthout) {
1507c478bd9Sstevel@tonic-gate (void) snprintf(
1517c478bd9Sstevel@tonic-gate string, SCALED_STRLEN, "%llu", *uint64out);
1527c478bd9Sstevel@tonic-gate *widthout = strlen(string);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate return (0);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /* scan out numeric value */
1587c478bd9Sstevel@tonic-gate errno = 0;
1597c478bd9Sstevel@tonic-gate value = strtod(scaledin, &endptr);
1607c478bd9Sstevel@tonic-gate if (errno) {
1617c478bd9Sstevel@tonic-gate if (errno == ERANGE)
1627c478bd9Sstevel@tonic-gate return (SCALED_OVERFLOW);
1637c478bd9Sstevel@tonic-gate else
1647c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate if (endptr == scaledin)
1687c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate /* no negative values */
1717c478bd9Sstevel@tonic-gate if (strchr(scaledin, '-'))
1727c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1737c478bd9Sstevel@tonic-gate if (value < 0.0)
1747c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /* compute width of number string */
1787c478bd9Sstevel@tonic-gate if (widthout)
1797c478bd9Sstevel@tonic-gate *widthout = (int)(endptr - scaledin);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /* check possible modifier */
1827c478bd9Sstevel@tonic-gate if (*endptr != '\0') {
1837c478bd9Sstevel@tonic-gate index = 0;
1847c478bd9Sstevel@tonic-gate while (modifiers[index] != NULL) {
1857c478bd9Sstevel@tonic-gate if (flags & SCALED_MODIFIER_CASE_INSENSITIVE_FLAG)
1867c478bd9Sstevel@tonic-gate cmp = strncasecmp(modifiers[index], endptr,
1877c478bd9Sstevel@tonic-gate strlen(modifiers[index]));
1887c478bd9Sstevel@tonic-gate else
1897c478bd9Sstevel@tonic-gate cmp = strncmp(modifiers[index], endptr,
1907c478bd9Sstevel@tonic-gate strlen(modifiers[index]));
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate if (cmp == 0) {
1937c478bd9Sstevel@tonic-gate if (modifierout)
1947c478bd9Sstevel@tonic-gate *modifierout = modifiers[index];
1957c478bd9Sstevel@tonic-gate endptr += strlen(modifiers[index]);
1967c478bd9Sstevel@tonic-gate multiplier = scales[index];
1977c478bd9Sstevel@tonic-gate result = value * multiplier;
1987c478bd9Sstevel@tonic-gate if (result > UINT64_MAX)
1997c478bd9Sstevel@tonic-gate return (SCALED_OVERFLOW);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate *uint64out = (uint64_t)result;
2027c478bd9Sstevel@tonic-gate hasmodifier = 1;
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate index++;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate /* if there is no modifier, value must be an integer */
2097c478bd9Sstevel@tonic-gate if (!hasmodifier) {
2107c478bd9Sstevel@tonic-gate errno = 0;
2117c478bd9Sstevel@tonic-gate *uint64out = strtoull(scaledin, &endptr, 0);
2127c478bd9Sstevel@tonic-gate if (errno) {
2137c478bd9Sstevel@tonic-gate if (errno == ERANGE)
2147c478bd9Sstevel@tonic-gate return (SCALED_OVERFLOW);
2157c478bd9Sstevel@tonic-gate else
2167c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate if (endptr == scaledin)
2197c478bd9Sstevel@tonic-gate return (SCALED_INVALID_NUMBER);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate /* if unit is present when no unit is allowed, fail */
2237c478bd9Sstevel@tonic-gate if ((unit == NULL || *unit == '\0') && (*endptr != '\0'))
2247c478bd9Sstevel@tonic-gate return (SCALED_INVALID_UNIT);
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /* check for missing unit when unit is required */
2277c478bd9Sstevel@tonic-gate if ((unit && *unit != '\0') &&
2287c478bd9Sstevel@tonic-gate !(flags & SCALED_UNIT_OPTIONAL_FLAG) &&
2297c478bd9Sstevel@tonic-gate (*endptr == '\0'))
2307c478bd9Sstevel@tonic-gate return (SCALED_INVALID_UNIT);
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /* validate unit */
2337c478bd9Sstevel@tonic-gate if (unit && *unit != '\0') {
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate /* allow for missing unit if it is optional */
2367c478bd9Sstevel@tonic-gate if ((flags & SCALED_UNIT_OPTIONAL_FLAG) &&
2377c478bd9Sstevel@tonic-gate (*endptr == '\0'))
2387c478bd9Sstevel@tonic-gate return (0);
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (flags & SCALED_UNIT_CASE_INSENSITIVE_FLAG)
2417c478bd9Sstevel@tonic-gate cmp = strncasecmp(unit, endptr, strlen(unit));
2427c478bd9Sstevel@tonic-gate else
2437c478bd9Sstevel@tonic-gate cmp = strncmp(unit, endptr, strlen(unit));
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if (cmp != 0)
2467c478bd9Sstevel@tonic-gate return (SCALED_INVALID_UNIT);
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if (*(endptr + strlen(unit)) != '\0')
2497c478bd9Sstevel@tonic-gate return (SCALED_INVALID_UNIT);
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if (unitout)
2527c478bd9Sstevel@tonic-gate *unitout = unit;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate return (0);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate int
uint64toscaled(uint64_t uint64in,int widthin,char * maxmodifierin,char * scaledout,int * widthout,char ** modifierout,scale_t * scale,char * unit,int flags)2597c478bd9Sstevel@tonic-gate uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifierin,
2607c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char **modifierout,
2617c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags) {
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate int index = 0;
2647c478bd9Sstevel@tonic-gate int count;
2657c478bd9Sstevel@tonic-gate int width;
2667c478bd9Sstevel@tonic-gate int decimals = 0;
2677c478bd9Sstevel@tonic-gate char string[SCALED_STRLEN];
2687c478bd9Sstevel@tonic-gate double value;
2697c478bd9Sstevel@tonic-gate char **modifiers = scale->modifers;
2707c478bd9Sstevel@tonic-gate uint64_t *scales = scale->scales;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate /* don't scale if there is no reason to */
2737c478bd9Sstevel@tonic-gate if (uint64in < scales[0] || maxmodifierin == NULL) {
2747c478bd9Sstevel@tonic-gate if (flags & SCALED_PAD_WIDTH_FLAG)
2757c478bd9Sstevel@tonic-gate width = widthin;
2767c478bd9Sstevel@tonic-gate else
2777c478bd9Sstevel@tonic-gate width = 0;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate (void) snprintf(string, SCALED_STRLEN, "%%%dllu", width);
2807c478bd9Sstevel@tonic-gate /* LINTED */
2817c478bd9Sstevel@tonic-gate count = snprintf(scaledout, SCALED_STRLEN, string, uint64in);
2827c478bd9Sstevel@tonic-gate if (unit && *unit != '\0')
2837c478bd9Sstevel@tonic-gate (void) strcat(scaledout, unit);
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate if (widthout)
2867c478bd9Sstevel@tonic-gate *widthout = count;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate if (modifierout)
2897c478bd9Sstevel@tonic-gate *modifierout = NULL;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate return (0);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate for (index = 0; modifiers[index + 1] != NULL; index++) {
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate if (uint64in >= scales[index] &&
2977c478bd9Sstevel@tonic-gate uint64in < scales[index + 1])
2987c478bd9Sstevel@tonic-gate break;
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate if ((strncmp(modifiers[index], maxmodifierin,
3017c478bd9Sstevel@tonic-gate strlen(modifiers[index])) == 0) &&
3027c478bd9Sstevel@tonic-gate (strlen(modifiers[index]) == strlen(maxmodifierin)))
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate value = ((double)(uint64in)) / scales[index];
3087c478bd9Sstevel@tonic-gate if (modifierout)
3097c478bd9Sstevel@tonic-gate *modifierout = modifiers[index];
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate count = snprintf(string, SCALED_STRLEN, "%0.0lf", value);
3127c478bd9Sstevel@tonic-gate while (count < widthin) {
3137c478bd9Sstevel@tonic-gate decimals++;
3147c478bd9Sstevel@tonic-gate (void) snprintf(string, SCALED_STRLEN, "%%0.%dlf", decimals);
3157c478bd9Sstevel@tonic-gate /* LINTED */
3167c478bd9Sstevel@tonic-gate count = snprintf(scaledout, SCALED_STRLEN, string, value);
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate /* reduce decimal places if we've overshot the desired width */
3197c478bd9Sstevel@tonic-gate if (count > widthin) {
3207c478bd9Sstevel@tonic-gate decimals--;
3217c478bd9Sstevel@tonic-gate break;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate if (flags & SCALED_PAD_WIDTH_FLAG)
3267c478bd9Sstevel@tonic-gate width = widthin;
3277c478bd9Sstevel@tonic-gate else
3287c478bd9Sstevel@tonic-gate width = 0;
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate (void) snprintf(string, SCALED_STRLEN, "%%%d.%dlf", width, decimals);
3317c478bd9Sstevel@tonic-gate /* LINTED */
3327c478bd9Sstevel@tonic-gate count = snprintf(scaledout, SCALED_STRLEN, string, value);
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate (void) strcat(scaledout, modifiers[index]);
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate if (unit && *unit != '\0')
3377c478bd9Sstevel@tonic-gate (void) strcat(scaledout, unit);
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate if (widthout)
3407c478bd9Sstevel@tonic-gate *widthout = count;
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate int
scaledtoscaled(char * scaledin,int widthin,char * maxmodifierin,char * scaledout,int * widthout,char ** modifierout,scale_t * scale,char * unit,int flags)3467c478bd9Sstevel@tonic-gate scaledtoscaled(char *scaledin, int widthin, char *maxmodifierin,
3477c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char **modifierout,
3487c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags) {
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate int ret;
3517c478bd9Sstevel@tonic-gate uint64_t val;
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate ret = scaledtouint64(scaledin, &val, NULL, NULL, NULL,
3547c478bd9Sstevel@tonic-gate scale, unit, flags);
3557c478bd9Sstevel@tonic-gate if (ret)
3567c478bd9Sstevel@tonic-gate return (ret);
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate ret = uint64toscaled(val, widthin, maxmodifierin,
3597c478bd9Sstevel@tonic-gate scaledout, widthout, modifierout,
3607c478bd9Sstevel@tonic-gate scale, unit, flags);
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate return (ret);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate int
scaledeqscaled(char * scaled1,char * scaled2,scale_t * scale,char * unit,int flags)3667c478bd9Sstevel@tonic-gate scaledeqscaled(char *scaled1, char *scaled2,
3677c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags) {
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate int ret;
3707c478bd9Sstevel@tonic-gate uint64_t uint64;
3717c478bd9Sstevel@tonic-gate char *modifier1;
3727c478bd9Sstevel@tonic-gate char *modifier2;
3737c478bd9Sstevel@tonic-gate char *modifier = NULL;
3747c478bd9Sstevel@tonic-gate int i;
3757c478bd9Sstevel@tonic-gate int width;
3767c478bd9Sstevel@tonic-gate int width1;
3777c478bd9Sstevel@tonic-gate int width2;
3787c478bd9Sstevel@tonic-gate char scaledA[SCALED_STRLEN];
3797c478bd9Sstevel@tonic-gate char scaledB[SCALED_STRLEN];
3807c478bd9Sstevel@tonic-gate char **modifiers = scale->modifers;
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * remove padding flag, so strings to compare will not have
3847c478bd9Sstevel@tonic-gate * whitespace
3857c478bd9Sstevel@tonic-gate */
3867c478bd9Sstevel@tonic-gate flags = flags & (~SCALED_PAD_WIDTH_FLAG);
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate /* determine each number's width and modifier */
3897c478bd9Sstevel@tonic-gate ret = scaledtouint64(scaled1, &uint64, &width1, &modifier1, NULL,
3907c478bd9Sstevel@tonic-gate scale, unit, flags);
3917c478bd9Sstevel@tonic-gate if (ret)
3927c478bd9Sstevel@tonic-gate return (0);
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate ret = scaledtouint64(scaled2, &uint64, &width2, &modifier2, NULL,
3957c478bd9Sstevel@tonic-gate scale, unit, flags);
3967c478bd9Sstevel@tonic-gate if (ret)
3977c478bd9Sstevel@tonic-gate return (0);
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate * determine the width and modifier to use for comparison.
4017c478bd9Sstevel@tonic-gate * Use widest width and smallest modifier.
4027c478bd9Sstevel@tonic-gate * Rescale to new width and modifier
4037c478bd9Sstevel@tonic-gate */
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate if (modifier1 == NULL || modifier2 == NULL)
4067c478bd9Sstevel@tonic-gate modifier = NULL;
4077c478bd9Sstevel@tonic-gate else {
4087c478bd9Sstevel@tonic-gate for (i = 0; modifiers[i] != NULL; i++) {
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate if (strcmp(modifier1, modifiers[i]) == 0) {
4117c478bd9Sstevel@tonic-gate modifier = modifiers[i];
4127c478bd9Sstevel@tonic-gate break;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate if (strcmp(modifier2, modifiers[i]) == 0) {
4157c478bd9Sstevel@tonic-gate modifier = modifiers[i];
4167c478bd9Sstevel@tonic-gate break;
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate width = 0;
4217c478bd9Sstevel@tonic-gate if (width1 > width)
4227c478bd9Sstevel@tonic-gate width = width1;
4237c478bd9Sstevel@tonic-gate if (width2 > width)
4247c478bd9Sstevel@tonic-gate width = width2;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate * Convert first number to width and modifier.
4287c478bd9Sstevel@tonic-gate * This is done for the following reasons:
4297c478bd9Sstevel@tonic-gate * 1. In case first number is hecadecimal. This will convert
4307c478bd9Sstevel@tonic-gate * it to decimal
4317c478bd9Sstevel@tonic-gate * 2. In case the first number has < the minimum number of
4327c478bd9Sstevel@tonic-gate * columns.
4337c478bd9Sstevel@tonic-gate * 3. The first number is missing an optional unit string.
4347c478bd9Sstevel@tonic-gate * 4. Fix casing of modifier and unit.
4357c478bd9Sstevel@tonic-gate */
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate ret = scaledtoscaled(scaled1, width, modifier,
4387c478bd9Sstevel@tonic-gate scaledA, NULL, NULL, scale, unit, flags);
4397c478bd9Sstevel@tonic-gate if (ret)
4407c478bd9Sstevel@tonic-gate return (0);
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate /* convert second number to width and modifier matching first number */
4437c478bd9Sstevel@tonic-gate ret = scaledtoscaled(scaled2, width, modifier,
4447c478bd9Sstevel@tonic-gate scaledB, NULL, NULL, scale, unit, flags);
4457c478bd9Sstevel@tonic-gate if (ret)
4467c478bd9Sstevel@tonic-gate return (0);
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate /* numbers are equal if strings match */
4497c478bd9Sstevel@tonic-gate return ((strncmp(scaledA, scaledB, strlen(scaledA)) == 0) &&
4507c478bd9Sstevel@tonic-gate (strlen(scaledA) == strlen(scaledB)));
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate int
scaledequint64(char * scaled,uint64_t uint64,int minwidth,scale_t * scale,char * unit,int flags)4557c478bd9Sstevel@tonic-gate scaledequint64(char *scaled, uint64_t uint64, int minwidth,
4567c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags) {
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate int ret;
4597c478bd9Sstevel@tonic-gate uint64_t tmpuint64;
4607c478bd9Sstevel@tonic-gate char *modifier;
4617c478bd9Sstevel@tonic-gate int width;
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate char scaledA[SCALED_STRLEN];
4647c478bd9Sstevel@tonic-gate char scaledB[SCALED_STRLEN];
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /* determine for number's width and modifier */
4677c478bd9Sstevel@tonic-gate ret = scaledtouint64(scaled, &tmpuint64, &width, &modifier, NULL,
4687c478bd9Sstevel@tonic-gate scale, unit, flags);
4697c478bd9Sstevel@tonic-gate if (ret)
4707c478bd9Sstevel@tonic-gate return (0);
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate if (width < minwidth)
4737c478bd9Sstevel@tonic-gate width = minwidth;
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate * Convert first number to width and modifier.
4777c478bd9Sstevel@tonic-gate * This is done for the following reasons:
4787c478bd9Sstevel@tonic-gate * 1. In case first number is hecadecimal. This will convert
4797c478bd9Sstevel@tonic-gate * it to decimal
4807c478bd9Sstevel@tonic-gate * 2. In case the first number has < the minimum number of
4817c478bd9Sstevel@tonic-gate * columns.
4827c478bd9Sstevel@tonic-gate * 3. The first number is missing an optional unit string.
4837c478bd9Sstevel@tonic-gate * 4. Fix casing of modifier and unit.
4847c478bd9Sstevel@tonic-gate */
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate ret = scaledtoscaled(scaled, width, modifier,
4877c478bd9Sstevel@tonic-gate scaledA, NULL, NULL, scale, unit, flags);
4887c478bd9Sstevel@tonic-gate if (ret)
4897c478bd9Sstevel@tonic-gate return (0);
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* convert second number to width and modifier matching first number */
4927c478bd9Sstevel@tonic-gate ret = uint64toscaled(uint64, width, modifier,
4937c478bd9Sstevel@tonic-gate scaledB, NULL, NULL, scale, unit, flags);
4947c478bd9Sstevel@tonic-gate if (ret)
4957c478bd9Sstevel@tonic-gate return (0);
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate /* numbers are equal if strings match */
4987c478bd9Sstevel@tonic-gate return ((strncmp(scaledA, scaledB, strlen(scaledA)) == 0) &&
4997c478bd9Sstevel@tonic-gate (strlen(scaledA) == strlen(scaledB)));
5007c478bd9Sstevel@tonic-gate }
501