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