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