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