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