10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54891Svk199839 * Common Development and Distribution License (the "License"). 64891Svk199839 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21*13093SRoger.Faulkner@Oracle.COM 220Sstevel@tonic-gate /* 23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _UTILS_H 270Sstevel@tonic-gate #define _UTILS_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate #ifdef __cplusplus 300Sstevel@tonic-gate extern "C" { 310Sstevel@tonic-gate #endif 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate 354891Svk199839 extern void warn(const char *, ...); 36*13093SRoger.Faulkner@Oracle.COM extern char *setpname(char *); 370Sstevel@tonic-gate 380Sstevel@tonic-gate /* 390Sstevel@tonic-gate * scale_t 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * Used to describe string modifiers and integer scales. 420Sstevel@tonic-gate * modifiers: NULL terminated array of modifier strings, such as 430Sstevel@tonic-gate * { "K", "M", NULL }, for strings like "100KB" or "100MB" 440Sstevel@tonic-gate * scales: array of scales for each modifer string, such as 450Sstevel@tonic-gate * { 1000, 1000000 } 460Sstevel@tonic-gate */ 470Sstevel@tonic-gate typedef struct scale_struct { 480Sstevel@tonic-gate char **modifers; 490Sstevel@tonic-gate uint64_t *scales; 500Sstevel@tonic-gate } scale_t; 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * pointers to standard scales. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate extern scale_t *scale_binary; 560Sstevel@tonic-gate extern scale_t *scale_metric; 570Sstevel@tonic-gate 580Sstevel@tonic-gate #define SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 0x01 590Sstevel@tonic-gate #define SCALED_UNIT_CASE_INSENSITIVE_FLAG 0x02 600Sstevel@tonic-gate #define SCALED_UNIT_OPTIONAL_FLAG 0x04 610Sstevel@tonic-gate #define SCALED_PAD_WIDTH_FLAG 0x08 620Sstevel@tonic-gate #define SCALED_ALL_FLAGS 0x0F 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * 20 characters for UINT64_MAX, 1 character for modifer, 1 character for 660Sstevel@tonic-gate * unit, 1 character for NULL, 1 extra. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate #define SCALED_STRLEN (24) 690Sstevel@tonic-gate 700Sstevel@tonic-gate #define SCALED_INVALID_MODIFIER 1 710Sstevel@tonic-gate #define SCALED_INVALID_UNIT 2 720Sstevel@tonic-gate #define SCALED_INVALID_NUMBER 3 730Sstevel@tonic-gate #define SCALED_OVERFLOW 4 740Sstevel@tonic-gate 750Sstevel@tonic-gate #define SCALED_UNIT_BYTES "B" 760Sstevel@tonic-gate #define SCALED_UNIT_SECONDS "s" 770Sstevel@tonic-gate #define SCALED_UNIT_NONE "" 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * scaledtouint64 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * converts a string in one of the forms: 830Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 840Sstevel@tonic-gate * "[integer number][unit]" 850Sstevel@tonic-gate * 860Sstevel@tonic-gate * to a uint64. As seen from the two forms, If no modifier is present, 870Sstevel@tonic-gate * the number must be an integer. 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * Inputs: 900Sstevel@tonic-gate * 910Sstevel@tonic-gate * scaledin: input string containing number string 920Sstevel@tonic-gate * scale: pointer to scale_t to describe scaling modifiers and scales 930Sstevel@tonic-gate * unit: expected unit string, such as "B", for the number "100MB" 940Sstevel@tonic-gate * flags: one of: 950Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 960Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 970Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 980Sstevel@tonic-gate * which are pretty self explainatory. 990Sstevel@tonic-gate * Outputs: 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * return value: 0 on success, on errors: 1020Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 1030Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 1040Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 1050Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 1060Sstevel@tonic-gate * 1070Sstevel@tonic-gate * uint64out: uint64_t value of input string 1080Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 1090Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 1100Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 1110Sstevel@tonic-gate * was found in the input string. If no modifer was 1120Sstevel@tonic-gate * found, this well be set to NULL; 1130Sstevel@tonic-gate * unitout: If unit string was present in the input string, this 1140Sstevel@tonic-gate * will be set to point to unit, otherwise NULL. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate int scaledtouint64(char *scaledin, uint64_t *uint64out, 1170Sstevel@tonic-gate int *widthout, char **modifierout, char **unitout, 1180Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * uint64toscaled 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * converts a uint64 to a string in one of the forms: 1240Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 1250Sstevel@tonic-gate * "[integer number][unit]" 1260Sstevel@tonic-gate * (no modifier means number will be an integer) 1270Sstevel@tonic-gate * 1280Sstevel@tonic-gate * Inputs: 1290Sstevel@tonic-gate * 1300Sstevel@tonic-gate * uint64in: input number to convert to scaled string 1310Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 1320Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 1330Sstevel@tonic-gate * unit. 1340Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 1350Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 1360Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 1370Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 1380Sstevel@tonic-gate * flags: one of: 1390Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 1400Sstevel@tonic-gate * If the length of the scaled string is less than 1410Sstevel@tonic-gate * widthin, pad to the left with spaces. 1420Sstevel@tonic-gate * Outputs: 1430Sstevel@tonic-gate * 1440Sstevel@tonic-gate * return value: 0 on success, no error conditions. 1450Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 1460Sstevel@tonic-gate * widthout: Used to return the actual character length of the produced 1470Sstevel@tonic-gate * string, not including modifier and unit. 1480Sstevel@tonic-gate * modifierout: pointer to modifier used in scaled string. 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate int uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifier, 1510Sstevel@tonic-gate char *scaledout, int *widthout, char **modifierout, 1520Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * scaledtoscaled 1560Sstevel@tonic-gate * 1570Sstevel@tonic-gate * Used to rescale a string from/to the following forms: 1580Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 1590Sstevel@tonic-gate * "[integer number][unit]" 1600Sstevel@tonic-gate * 1610Sstevel@tonic-gate * This is used ensure the desired width and letter casing. 1620Sstevel@tonic-gate * 1630Sstevel@tonic-gate * As seen from the two forms, If no modifier is present, 1640Sstevel@tonic-gate * the number must be an integer. 1650Sstevel@tonic-gate * 1660Sstevel@tonic-gate * Inputs: 1670Sstevel@tonic-gate * scaledin: input string containing number string 1680Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 1690Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 1700Sstevel@tonic-gate * unit. 1710Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 1720Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 1730Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 1740Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 1750Sstevel@tonic-gate * flags: one of: 1760Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 1770Sstevel@tonic-gate * If the length of the scaled string is less than 1780Sstevel@tonic-gate * widthin, pad to the left with spaces. 1790Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 1800Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 1810Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 1820Sstevel@tonic-gate * which are pretty self explainatory. 1830Sstevel@tonic-gate * 1840Sstevel@tonic-gate * Outputs: 1850Sstevel@tonic-gate * 1860Sstevel@tonic-gate * return value: 0 on success, on errors: 1870Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 1880Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 1890Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 1900Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 1930Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 1940Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 1950Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 1960Sstevel@tonic-gate * was found in the input string. If no modifer was 1970Sstevel@tonic-gate * found, this well be set to NULL; 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate int scaledtoscaled(char *scaledin, int widthin, char *maxmodifier, 2000Sstevel@tonic-gate char *scaledout, int *widthout, char ** modifierout, 2010Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* 2040Sstevel@tonic-gate * scaledeqscaled 2050Sstevel@tonic-gate * 2060Sstevel@tonic-gate * Determine if two scaled strings are equivalent. Flags are same as 2070Sstevel@tonic-gate * scaledtouint64. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate int scaledeqscaled(char *scale1, char *scale2, 2100Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * scaledequint64 2140Sstevel@tonic-gate * 2150Sstevel@tonic-gate * Determine if a scaled number is equal to an uint64. The uint64 is scaled 2160Sstevel@tonic-gate * to the same scale and width as the scaled strings. If the resultant string 2170Sstevel@tonic-gate * is equal, then the numbers are considered equal. 2180Sstevel@tonic-gate * 2190Sstevel@tonic-gate * minwidth: minimum number width to scale string and number to for 2200Sstevel@tonic-gate * comparision. 2210Sstevel@tonic-gate * flags are same as scaledtouint64. 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate int scaledequint64(char *scaled, uint64_t uint64, int minwidth, 2240Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate #ifdef __cplusplus 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate #endif 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate #endif /* _UTILS_H */ 231