1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #ifndef _UTILS_H 28*7c478bd9Sstevel@tonic-gate #define _UTILS_H 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus 33*7c478bd9Sstevel@tonic-gate extern "C" { 34*7c478bd9Sstevel@tonic-gate #endif 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate extern void warn(char *, ...); 39*7c478bd9Sstevel@tonic-gate extern char *setprogname(char *); 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate /* 42*7c478bd9Sstevel@tonic-gate * scale_t 43*7c478bd9Sstevel@tonic-gate * 44*7c478bd9Sstevel@tonic-gate * Used to describe string modifiers and integer scales. 45*7c478bd9Sstevel@tonic-gate * modifiers: NULL terminated array of modifier strings, such as 46*7c478bd9Sstevel@tonic-gate * { "K", "M", NULL }, for strings like "100KB" or "100MB" 47*7c478bd9Sstevel@tonic-gate * scales: array of scales for each modifer string, such as 48*7c478bd9Sstevel@tonic-gate * { 1000, 1000000 } 49*7c478bd9Sstevel@tonic-gate */ 50*7c478bd9Sstevel@tonic-gate typedef struct scale_struct { 51*7c478bd9Sstevel@tonic-gate char **modifers; 52*7c478bd9Sstevel@tonic-gate uint64_t *scales; 53*7c478bd9Sstevel@tonic-gate } scale_t; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * pointers to standard scales. 57*7c478bd9Sstevel@tonic-gate */ 58*7c478bd9Sstevel@tonic-gate extern scale_t *scale_binary; 59*7c478bd9Sstevel@tonic-gate extern scale_t *scale_metric; 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #define SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 0x01 62*7c478bd9Sstevel@tonic-gate #define SCALED_UNIT_CASE_INSENSITIVE_FLAG 0x02 63*7c478bd9Sstevel@tonic-gate #define SCALED_UNIT_OPTIONAL_FLAG 0x04 64*7c478bd9Sstevel@tonic-gate #define SCALED_PAD_WIDTH_FLAG 0x08 65*7c478bd9Sstevel@tonic-gate #define SCALED_ALL_FLAGS 0x0F 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate * 20 characters for UINT64_MAX, 1 character for modifer, 1 character for 69*7c478bd9Sstevel@tonic-gate * unit, 1 character for NULL, 1 extra. 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate #define SCALED_STRLEN (24) 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate #define SCALED_INVALID_MODIFIER 1 74*7c478bd9Sstevel@tonic-gate #define SCALED_INVALID_UNIT 2 75*7c478bd9Sstevel@tonic-gate #define SCALED_INVALID_NUMBER 3 76*7c478bd9Sstevel@tonic-gate #define SCALED_OVERFLOW 4 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate #define SCALED_UNIT_BYTES "B" 79*7c478bd9Sstevel@tonic-gate #define SCALED_UNIT_SECONDS "s" 80*7c478bd9Sstevel@tonic-gate #define SCALED_UNIT_NONE "" 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* 83*7c478bd9Sstevel@tonic-gate * scaledtouint64 84*7c478bd9Sstevel@tonic-gate * 85*7c478bd9Sstevel@tonic-gate * converts a string in one of the forms: 86*7c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 87*7c478bd9Sstevel@tonic-gate * "[integer number][unit]" 88*7c478bd9Sstevel@tonic-gate * 89*7c478bd9Sstevel@tonic-gate * to a uint64. As seen from the two forms, If no modifier is present, 90*7c478bd9Sstevel@tonic-gate * the number must be an integer. 91*7c478bd9Sstevel@tonic-gate * 92*7c478bd9Sstevel@tonic-gate * Inputs: 93*7c478bd9Sstevel@tonic-gate * 94*7c478bd9Sstevel@tonic-gate * scaledin: input string containing number string 95*7c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe scaling modifiers and scales 96*7c478bd9Sstevel@tonic-gate * unit: expected unit string, such as "B", for the number "100MB" 97*7c478bd9Sstevel@tonic-gate * flags: one of: 98*7c478bd9Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 99*7c478bd9Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 100*7c478bd9Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 101*7c478bd9Sstevel@tonic-gate * which are pretty self explainatory. 102*7c478bd9Sstevel@tonic-gate * Outputs: 103*7c478bd9Sstevel@tonic-gate * 104*7c478bd9Sstevel@tonic-gate * return value: 0 on success, on errors: 105*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 106*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 107*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 108*7c478bd9Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 109*7c478bd9Sstevel@tonic-gate * 110*7c478bd9Sstevel@tonic-gate * uint64out: uint64_t value of input string 111*7c478bd9Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 112*7c478bd9Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 113*7c478bd9Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 114*7c478bd9Sstevel@tonic-gate * was found in the input string. If no modifer was 115*7c478bd9Sstevel@tonic-gate * found, this well be set to NULL; 116*7c478bd9Sstevel@tonic-gate * unitout: If unit string was present in the input string, this 117*7c478bd9Sstevel@tonic-gate * will be set to point to unit, otherwise NULL. 118*7c478bd9Sstevel@tonic-gate */ 119*7c478bd9Sstevel@tonic-gate int scaledtouint64(char *scaledin, uint64_t *uint64out, 120*7c478bd9Sstevel@tonic-gate int *widthout, char **modifierout, char **unitout, 121*7c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate /* 124*7c478bd9Sstevel@tonic-gate * uint64toscaled 125*7c478bd9Sstevel@tonic-gate * 126*7c478bd9Sstevel@tonic-gate * converts a uint64 to a string in one of the forms: 127*7c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 128*7c478bd9Sstevel@tonic-gate * "[integer number][unit]" 129*7c478bd9Sstevel@tonic-gate * (no modifier means number will be an integer) 130*7c478bd9Sstevel@tonic-gate * 131*7c478bd9Sstevel@tonic-gate * Inputs: 132*7c478bd9Sstevel@tonic-gate * 133*7c478bd9Sstevel@tonic-gate * uint64in: input number to convert to scaled string 134*7c478bd9Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 135*7c478bd9Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 136*7c478bd9Sstevel@tonic-gate * unit. 137*7c478bd9Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 138*7c478bd9Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 139*7c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 140*7c478bd9Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 141*7c478bd9Sstevel@tonic-gate * flags: one of: 142*7c478bd9Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 143*7c478bd9Sstevel@tonic-gate * If the length of the scaled string is less than 144*7c478bd9Sstevel@tonic-gate * widthin, pad to the left with spaces. 145*7c478bd9Sstevel@tonic-gate * Outputs: 146*7c478bd9Sstevel@tonic-gate * 147*7c478bd9Sstevel@tonic-gate * return value: 0 on success, no error conditions. 148*7c478bd9Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 149*7c478bd9Sstevel@tonic-gate * widthout: Used to return the actual character length of the produced 150*7c478bd9Sstevel@tonic-gate * string, not including modifier and unit. 151*7c478bd9Sstevel@tonic-gate * modifierout: pointer to modifier used in scaled string. 152*7c478bd9Sstevel@tonic-gate */ 153*7c478bd9Sstevel@tonic-gate int uint64toscaled(uint64_t uint64in, int widthin, char *maxmodifier, 154*7c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char **modifierout, 155*7c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate /* 158*7c478bd9Sstevel@tonic-gate * scaledtoscaled 159*7c478bd9Sstevel@tonic-gate * 160*7c478bd9Sstevel@tonic-gate * Used to rescale a string from/to the following forms: 161*7c478bd9Sstevel@tonic-gate * "[decimal number]][modifier][unit]" 162*7c478bd9Sstevel@tonic-gate * "[integer number][unit]" 163*7c478bd9Sstevel@tonic-gate * 164*7c478bd9Sstevel@tonic-gate * This is used ensure the desired width and letter casing. 165*7c478bd9Sstevel@tonic-gate * 166*7c478bd9Sstevel@tonic-gate * As seen from the two forms, If no modifier is present, 167*7c478bd9Sstevel@tonic-gate * the number must be an integer. 168*7c478bd9Sstevel@tonic-gate * 169*7c478bd9Sstevel@tonic-gate * Inputs: 170*7c478bd9Sstevel@tonic-gate * scaledin: input string containing number string 171*7c478bd9Sstevel@tonic-gate * widthin: character width of desired string, not including modifier 172*7c478bd9Sstevel@tonic-gate * and unit. Eg: 1.00MB has a width of 4 for the "1.00". 173*7c478bd9Sstevel@tonic-gate * unit. 174*7c478bd9Sstevel@tonic-gate * maxmodifier: The maximium scaling to use. For instance, to limit the 175*7c478bd9Sstevel@tonic-gate * scaling to megabytes (no GB or higher), use "M" 176*7c478bd9Sstevel@tonic-gate * scale: pointer to scale_t to describe modifiers and scales 177*7c478bd9Sstevel@tonic-gate * unit: unit string, such as "B", for the number "100MB" 178*7c478bd9Sstevel@tonic-gate * flags: one of: 179*7c478bd9Sstevel@tonic-gate * SCALED_PAD_WIDTH_FLAG 180*7c478bd9Sstevel@tonic-gate * If the length of the scaled string is less than 181*7c478bd9Sstevel@tonic-gate * widthin, pad to the left with spaces. 182*7c478bd9Sstevel@tonic-gate * SCALED_MODIFIER_CASE_INSENSITIVE_FLAG 183*7c478bd9Sstevel@tonic-gate * SCALED_UNIT_CASE_INSENSITIVE_FLAG 184*7c478bd9Sstevel@tonic-gate * SCALED_UNIT_OPTIONAL_FLAG 185*7c478bd9Sstevel@tonic-gate * which are pretty self explainatory. 186*7c478bd9Sstevel@tonic-gate * 187*7c478bd9Sstevel@tonic-gate * Outputs: 188*7c478bd9Sstevel@tonic-gate * 189*7c478bd9Sstevel@tonic-gate * return value: 0 on success, on errors: 190*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_NUMBER - string contains no valid number 191*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_MODIFIER - string has unknown modifier 192*7c478bd9Sstevel@tonic-gate * SCALED_INVALID_UNIT - string has unknown or missing unit 193*7c478bd9Sstevel@tonic-gate * SCALED_OVERFLOW - number exceeds MAX_UINT64 194*7c478bd9Sstevel@tonic-gate * 195*7c478bd9Sstevel@tonic-gate * scaledout: Pointer to a string buffer to fill with the scaled string. 196*7c478bd9Sstevel@tonic-gate * widthout: width of number (not including modifier and unit) 197*7c478bd9Sstevel@tonic-gate * in the input string. "10.0MB" has a width of 4. 198*7c478bd9Sstevel@tonic-gate * modiferout: pointer to the string in the modifiers array which 199*7c478bd9Sstevel@tonic-gate * was found in the input string. If no modifer was 200*7c478bd9Sstevel@tonic-gate * found, this well be set to NULL; 201*7c478bd9Sstevel@tonic-gate */ 202*7c478bd9Sstevel@tonic-gate int scaledtoscaled(char *scaledin, int widthin, char *maxmodifier, 203*7c478bd9Sstevel@tonic-gate char *scaledout, int *widthout, char ** modifierout, 204*7c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate /* 207*7c478bd9Sstevel@tonic-gate * scaledeqscaled 208*7c478bd9Sstevel@tonic-gate * 209*7c478bd9Sstevel@tonic-gate * Determine if two scaled strings are equivalent. Flags are same as 210*7c478bd9Sstevel@tonic-gate * scaledtouint64. 211*7c478bd9Sstevel@tonic-gate */ 212*7c478bd9Sstevel@tonic-gate int scaledeqscaled(char *scale1, char *scale2, 213*7c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate /* 216*7c478bd9Sstevel@tonic-gate * scaledequint64 217*7c478bd9Sstevel@tonic-gate * 218*7c478bd9Sstevel@tonic-gate * Determine if a scaled number is equal to an uint64. The uint64 is scaled 219*7c478bd9Sstevel@tonic-gate * to the same scale and width as the scaled strings. If the resultant string 220*7c478bd9Sstevel@tonic-gate * is equal, then the numbers are considered equal. 221*7c478bd9Sstevel@tonic-gate * 222*7c478bd9Sstevel@tonic-gate * minwidth: minimum number width to scale string and number to for 223*7c478bd9Sstevel@tonic-gate * comparision. 224*7c478bd9Sstevel@tonic-gate * flags are same as scaledtouint64. 225*7c478bd9Sstevel@tonic-gate */ 226*7c478bd9Sstevel@tonic-gate int scaledequint64(char *scaled, uint64_t uint64, int minwidth, 227*7c478bd9Sstevel@tonic-gate scale_t *scale, char *unit, int flags); 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus 230*7c478bd9Sstevel@tonic-gate } 231*7c478bd9Sstevel@tonic-gate #endif 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate #endif /* _UTILS_H */ 234