xref: /onnv-gate/usr/src/cmd/lvm/metassist/common/volume_string.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef _VOLUME_STRING_H
28*0Sstevel@tonic-gate #define	_VOLUME_STRING_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #ifdef __cplusplus
35*0Sstevel@tonic-gate extern "C" {
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * The length of the string when the longest long long is converted to
40*0Sstevel@tonic-gate  * a string
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate #define	LONG_LONG_STR_SIZE	128
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	BYTES_PER_BLOCK 512
45*0Sstevel@tonic-gate #define	BYTES_PER_KILOBYTE 1024
46*0Sstevel@tonic-gate #define	BYTES_PER_MEGABYTE 1024 * 1024
47*0Sstevel@tonic-gate #define	BYTES_PER_GIGABYTE 1024 * 1024 * 1024
48*0Sstevel@tonic-gate #define	BYTES_PER_TERABYTE (uint64_t)1024 * 1024 * 1024 * 1024
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Describes units when converting from bytes to string and back.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate typedef struct {
54*0Sstevel@tonic-gate     char *unit_str;
55*0Sstevel@tonic-gate     uint64_t bytes_per_unit;
56*0Sstevel@tonic-gate } units_t;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /* All-inclusive valid size units */
59*0Sstevel@tonic-gate extern units_t universal_units[];
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * Concatenates a list of strings.  The result must be free()d.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  * @param       numargs
65*0Sstevel@tonic-gate  *              The number of strings to concatenate.
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * @param       ...
68*0Sstevel@tonic-gate  *              The strings (type char *) to concatenate.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * @return      the concatenated string
71*0Sstevel@tonic-gate  *              if succesful
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * @return      NULL
74*0Sstevel@tonic-gate  *              if memory could not be allocated
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate extern char *stralloccat(int numargs, ...);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Convert the given string to a uint16_t, verifying that the value
80*0Sstevel@tonic-gate  * does not exceed the lower or upper bounds of a uint16_t.
81*0Sstevel@tonic-gate  *
82*0Sstevel@tonic-gate  * @param       str
83*0Sstevel@tonic-gate  *              the string to convert
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * @param       num
86*0Sstevel@tonic-gate  *              the addr of the uint16_t
87*0Sstevel@tonic-gate  *
88*0Sstevel@tonic-gate  * @return      0
89*0Sstevel@tonic-gate  *              if the given string was converted to a uint16_t
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  * @return      -1
92*0Sstevel@tonic-gate  *              if the string could could not be converted to a number
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  * @return      -2
95*0Sstevel@tonic-gate  *              if the converted number exceeds the lower or upper
96*0Sstevel@tonic-gate  *              bounds of a uint16_t
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate extern int str_to_uint16(char *str, uint16_t *num);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Converts the given long long into a string.  This string must be
102*0Sstevel@tonic-gate  * freed.
103*0Sstevel@tonic-gate  *
104*0Sstevel@tonic-gate  * @param       num
105*0Sstevel@tonic-gate  *              the long long to convert
106*0Sstevel@tonic-gate  *
107*0Sstevel@tonic-gate  * @param       str
108*0Sstevel@tonic-gate  *              the addr of the string
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  * @return      0
111*0Sstevel@tonic-gate  *              if successful
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  * @return      ENOMEM
114*0Sstevel@tonic-gate  *              if the physical limits of the system are exceeded by
115*0Sstevel@tonic-gate  *              size bytes of memory which cannot be allocated
116*0Sstevel@tonic-gate  *
117*0Sstevel@tonic-gate  * @return      EAGAIN
118*0Sstevel@tonic-gate  *              if there is not enough memory available to allocate
119*0Sstevel@tonic-gate  *              size bytes of memory
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate extern int ll_to_str(long long num, char **str);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * Convert a size specification to bytes.
125*0Sstevel@tonic-gate  *
126*0Sstevel@tonic-gate  * @param       str
127*0Sstevel@tonic-gate  *              a size specification strings of the form
128*0Sstevel@tonic-gate  *              <value><units>, where valid <units> are specified by
129*0Sstevel@tonic-gate  *              the units argument and <value> is the (floating-point)
130*0Sstevel@tonic-gate  *              multiplier of the units
131*0Sstevel@tonic-gate  *
132*0Sstevel@tonic-gate  * @param       bytes
133*0Sstevel@tonic-gate  *              RETURN: the result of converting the given size string
134*0Sstevel@tonic-gate  *              to bytes
135*0Sstevel@tonic-gate  *
136*0Sstevel@tonic-gate  * @return      0
137*0Sstevel@tonic-gate  *              if successful
138*0Sstevel@tonic-gate  *
139*0Sstevel@tonic-gate  * @return      non-zero
140*0Sstevel@tonic-gate  *              if an error occurred.  Use get_error_string() to
141*0Sstevel@tonic-gate  *              retrieve the associated error message.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate extern int sizestr_to_bytes(char *str, uint64_t *bytes, units_t units[]);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate  * Convert bytes to a size specification string.
147*0Sstevel@tonic-gate  *
148*0Sstevel@tonic-gate  * @param       bytes
149*0Sstevel@tonic-gate  *              the number of bytes
150*0Sstevel@tonic-gate  *
151*0Sstevel@tonic-gate  * @param       str
152*0Sstevel@tonic-gate  *              RETURN: a size specification strings of the form
153*0Sstevel@tonic-gate  *              <value><units>, where valid <units> are specified by
154*0Sstevel@tonic-gate  *              the units argument and <value> is the (floating-point)
155*0Sstevel@tonic-gate  *              multiplier of the units.  This string must be freed.
156*0Sstevel@tonic-gate  *
157*0Sstevel@tonic-gate  * @return      0
158*0Sstevel@tonic-gate  *              if successful
159*0Sstevel@tonic-gate  *
160*0Sstevel@tonic-gate  * @return      non-zero
161*0Sstevel@tonic-gate  *              if an error occurred.  Use get_error_string() to
162*0Sstevel@tonic-gate  *              retrieve the associated error message.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate extern int bytes_to_sizestr(
165*0Sstevel@tonic-gate 	uint64_t bytes, char **str, units_t units[], boolean_t round);
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  * Appends a copy of the given string to the given string array,
169*0Sstevel@tonic-gate  * ensuring that the last element in the array is NULL.  This array
170*0Sstevel@tonic-gate  * must be freed via free_string_array.
171*0Sstevel@tonic-gate  *
172*0Sstevel@tonic-gate  * Note when an error occurs and NULL is returned, array is not freed.
173*0Sstevel@tonic-gate  * Subsequently callers should save a pointer to the original array
174*0Sstevel@tonic-gate  * until success is verified.
175*0Sstevel@tonic-gate  *
176*0Sstevel@tonic-gate  * @param       array
177*0Sstevel@tonic-gate  *              the array to append to, or NULL to create a new array
178*0Sstevel@tonic-gate  *
179*0Sstevel@tonic-gate  * @param       str
180*0Sstevel@tonic-gate  *              the string to copy and add to the array
181*0Sstevel@tonic-gate  *
182*0Sstevel@tonic-gate  * @return      a pointer to the realloc'd (and possibly moved) array
183*0Sstevel@tonic-gate  *              if succesful
184*0Sstevel@tonic-gate  *
185*0Sstevel@tonic-gate  * @return      NULL
186*0Sstevel@tonic-gate  *              if unsuccesful
187*0Sstevel@tonic-gate  */
188*0Sstevel@tonic-gate extern char ** append_to_string_array(char **array, char *str);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate  * Frees each element of the given string array, then frees the array
192*0Sstevel@tonic-gate  * itself.
193*0Sstevel@tonic-gate  *
194*0Sstevel@tonic-gate  * @param       array
195*0Sstevel@tonic-gate  *              a NULL-terminated string array
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate extern void free_string_array(char **array);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate #ifdef __cplusplus
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #endif /* _VOLUME_STRING_H */
204