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 #ifndef _UTILS_H 277c478bd9Sstevel@tonic-gate #define _UTILS_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifdef __cplusplus 327c478bd9Sstevel@tonic-gate extern "C" { 337c478bd9Sstevel@tonic-gate #endif 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate 37*d362b749Svk199839 extern void warn(const char *, ...); 387c478bd9Sstevel@tonic-gate extern char *setprogname(char *); 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * scale_t 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * Used to describe string modifiers and integer scales. 447c478bd9Sstevel@tonic-gate * modifiers: NULL terminated array of modifier strings, such as 457c478bd9Sstevel@tonic-gate * { "K", "M", NULL }, for strings like "100KB" or "100MB" 467c478bd9Sstevel@tonic-gate * scales: array of scales for each modifer string, such as 477c478bd9Sstevel@tonic-gate * { 1000, 1000000 } 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate typedef struct scale_struct { 507c478bd9Sstevel@tonic-gate char **modifers; 517c478bd9Sstevel@tonic-gate uint64_t *scales; 527c478bd9Sstevel@tonic-gate } scale_t; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* 557c478bd9Sstevel@tonic-gate * pointers to standard scales. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate extern scale_t *scale_binary; 587c478bd9Sstevel@tonic-gate extern scale_t *scale_metric; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 0x01 617c478bd9Sstevel@tonic-gate #define SCALED_UNIT_CASE_INSENSITIVE_FLAG 0x02 627c478bd9Sstevel@tonic-gate #define SCALED_UNIT_OPTIONAL_FLAG 0x04 637c478bd9Sstevel@tonic-gate #define SCALED_PAD_WIDTH_FLAG 0x08 647c478bd9Sstevel@tonic-gate #define SCALED_ALL_FLAGS 0x0F 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * 20 characters for UINT64_MAX, 1 character for modifer, 1 character for 687c478bd9Sstevel@tonic-gate * unit, 1 character for NULL, 1 extra. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate #define SCALED_STRLEN (24) 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define SCALED_INVALID_MODIFIER 1 737c478bd9Sstevel@tonic-gate #define SCALED_INVALID_UNIT 2 747c478bd9Sstevel@tonic-gate #define SCALED_INVALID_NUMBER 3 757c478bd9Sstevel@tonic-gate #define SCALED_OVERFLOW 4 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #define SCALED_UNIT_BYTES "B" 787c478bd9Sstevel@tonic-gate #define SCALED_UNIT_SECONDS "s" 797c478bd9Sstevel@tonic-gate #define SCALED_UNIT_NONE "" 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * scaledtouint64 837c478bd9Sstevel@tonic-gate * 847c478bd9Sstevel@tonic-gate * converts a string in one of the forms: 857c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 867c478bd9Sstevel@tonic-gate * "[integer number][unit]" 877c478bd9Sstevel@tonic-gate * 887c478bd9Sstevel@tonic-gate * to a uint64. As seen from the two forms, If no modifier is present, 897c478bd9Sstevel@tonic-gate * the number must be an integer. 907c478bd9Sstevel@tonic-gate * 917c478bd9Sstevel@tonic-gate * Inputs: 927c478bd9Sstevel@tonic-gate * 937c478bd9Sstevel@tonic-gate * scaledin: input string containing number string 947c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe scaling modifiers and scales 957c478bd9Sstevel@tonic-gate * unit: expected unit string, such as "B", for the number "100MB" 967c478bd9Sstevel@tonic-gate * flags: one of: 977c478bd9Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 987c478bd9Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 997c478bd9Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 1007c478bd9Sstevel@tonic-gate * which are pretty self explainatory. 1017c478bd9Sstevel@tonic-gate * Outputs: 1027c478bd9Sstevel@tonic-gate * 1037c478bd9Sstevel@tonic-gate * return value: 0 on success, on errors: 1047c478bd9Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 1057c478bd9Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 1067c478bd9Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 1077c478bd9Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 1087c478bd9Sstevel@tonic-gate * 1097c478bd9Sstevel@tonic-gate * uint64out: uint64_t value of input string 1107c478bd9Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 1117c478bd9Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 1127c478bd9Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 1137c478bd9Sstevel@tonic-gate * was found in the input string. If no modifer was 1147c478bd9Sstevel@tonic-gate * found, this well be set to NULL; 1157c478bd9Sstevel@tonic-gate * unitout: If unit string was present in the input string, this 1167c478bd9Sstevel@tonic-gate * will be set to point to unit, otherwise NULL. 1177c478bd9Sstevel@tonic-gate */ 1187c478bd9Sstevel@tonic-gate int scaledtouint64(char *scaledin, uint64_t *uint64out, 1197c478bd9Sstevel@tonic-gate int *widthout, char **modifierout, char **unitout, 1207c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * uint64toscaled 1247c478bd9Sstevel@tonic-gate * 1257c478bd9Sstevel@tonic-gate * converts a uint64 to a string in one of the forms: 1267c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 1277c478bd9Sstevel@tonic-gate * "[integer number][unit]" 1287c478bd9Sstevel@tonic-gate * (no modifier means number will be an integer) 1297c478bd9Sstevel@tonic-gate * 1307c478bd9Sstevel@tonic-gate * Inputs: 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * uint64in: input number to convert to scaled string 1337c478bd9Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 1347c478bd9Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 1357c478bd9Sstevel@tonic-gate * unit. 1367c478bd9Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 1377c478bd9Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 1387c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 1397c478bd9Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 1407c478bd9Sstevel@tonic-gate * flags: one of: 1417c478bd9Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 1427c478bd9Sstevel@tonic-gate * If the length of the scaled string is less than 1437c478bd9Sstevel@tonic-gate * widthin, pad to the left with spaces. 1447c478bd9Sstevel@tonic-gate * Outputs: 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * return value: 0 on success, no error conditions. 1477c478bd9Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 1487c478bd9Sstevel@tonic-gate * widthout: Used to return the actual character length of the produced 1497c478bd9Sstevel@tonic-gate * string, not including modifier and unit. 1507c478bd9Sstevel@tonic-gate * modifierout: pointer to modifier used in scaled string. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate int uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifier, 1537c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char **modifierout, 1547c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * scaledtoscaled 1587c478bd9Sstevel@tonic-gate * 1597c478bd9Sstevel@tonic-gate * Used to rescale a string from/to the following forms: 1607c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 1617c478bd9Sstevel@tonic-gate * "[integer number][unit]" 1627c478bd9Sstevel@tonic-gate * 1637c478bd9Sstevel@tonic-gate * This is used ensure the desired width and letter casing. 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * As seen from the two forms, If no modifier is present, 1667c478bd9Sstevel@tonic-gate * the number must be an integer. 1677c478bd9Sstevel@tonic-gate * 1687c478bd9Sstevel@tonic-gate * Inputs: 1697c478bd9Sstevel@tonic-gate * scaledin: input string containing number string 1707c478bd9Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 1717c478bd9Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 1727c478bd9Sstevel@tonic-gate * unit. 1737c478bd9Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 1747c478bd9Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 1757c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 1767c478bd9Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 1777c478bd9Sstevel@tonic-gate * flags: one of: 1787c478bd9Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 1797c478bd9Sstevel@tonic-gate * If the length of the scaled string is less than 1807c478bd9Sstevel@tonic-gate * widthin, pad to the left with spaces. 1817c478bd9Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 1827c478bd9Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 1837c478bd9Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 1847c478bd9Sstevel@tonic-gate * which are pretty self explainatory. 1857c478bd9Sstevel@tonic-gate * 1867c478bd9Sstevel@tonic-gate * Outputs: 1877c478bd9Sstevel@tonic-gate * 1887c478bd9Sstevel@tonic-gate * return value: 0 on success, on errors: 1897c478bd9Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 1907c478bd9Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 1917c478bd9Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 1927c478bd9Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 1937c478bd9Sstevel@tonic-gate * 1947c478bd9Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 1957c478bd9Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 1967c478bd9Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 1977c478bd9Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 1987c478bd9Sstevel@tonic-gate * was found in the input string. If no modifer was 1997c478bd9Sstevel@tonic-gate * found, this well be set to NULL; 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate int scaledtoscaled(char *scaledin, int widthin, char *maxmodifier, 2027c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char ** modifierout, 2037c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * scaledeqscaled 2077c478bd9Sstevel@tonic-gate * 2087c478bd9Sstevel@tonic-gate * Determine if two scaled strings are equivalent. Flags are same as 2097c478bd9Sstevel@tonic-gate * scaledtouint64. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate int scaledeqscaled(char *scale1, char *scale2, 2127c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * scaledequint64 2167c478bd9Sstevel@tonic-gate * 2177c478bd9Sstevel@tonic-gate * Determine if a scaled number is equal to an uint64. The uint64 is scaled 2187c478bd9Sstevel@tonic-gate * to the same scale and width as the scaled strings. If the resultant string 2197c478bd9Sstevel@tonic-gate * is equal, then the numbers are considered equal. 2207c478bd9Sstevel@tonic-gate * 2217c478bd9Sstevel@tonic-gate * minwidth: minimum number width to scale string and number to for 2227c478bd9Sstevel@tonic-gate * comparision. 2237c478bd9Sstevel@tonic-gate * flags are same as scaledtouint64. 2247c478bd9Sstevel@tonic-gate */ 2257c478bd9Sstevel@tonic-gate int scaledequint64(char *scaled, uint64_t uint64, int minwidth, 2267c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate #endif 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate #endif /* _UTILS_H */ 233