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