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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Helper functions to skip white spaces, find tokens, find separators and free
39 #include "commp_util.h"
43 * Skip to the next non-whitespace
46 commp_skip_white_space(const char **begin
, const char *end
)
48 while (*begin
< end
) {
49 if (!isspace(**begin
))
57 * Finds the token in the char buffer. *current will be pointing to the
58 * token when function returns. If the char buffer has leading token,
62 commp_find_token(const char **begin
, const char **current
, const char *end
,
63 char token
, boolean_t last
)
66 while (*current
< end
) {
67 if (!last
&& (**current
== token
))
69 else if (isspace(**current
))
73 /* Checks for leading white space */
74 if (*current
== *begin
)
84 commp_atoi(const char *begin
, const char *end
, int *num
)
86 boolean_t num_found
= B_FALSE
;
90 if (isdigit(*begin
)) {
91 *num
= (*num
* 10) + (*begin
- '0');
98 if (!num_found
|| (begin
!= end
))
104 * Given a string converts it to unsigned long long int.
107 commp_strtoull(const char *begin
, const char *end
, uint64_t *num
)
109 boolean_t num_found
= B_FALSE
;
112 while (begin
< end
) {
113 if (isdigit(*begin
)) {
114 *num
= (*num
* 10) + (*begin
- '0');
121 if (!num_found
|| (begin
!= end
))
127 * Given a string converts it to unsigned byte
130 commp_strtoub(const char *begin
, const char *end
, uint8_t *num
)
132 boolean_t num_found
= B_FALSE
;
135 while (begin
< end
) {
136 if (isdigit(*begin
)) {
137 *num
= (*num
* 10) + (*begin
- '0');
144 if (!num_found
|| (begin
!= end
))
150 * Given a string converts it to unsigned int
153 commp_atoui(const char *begin
, const char *end
, uint_t
*num
)
155 boolean_t num_found
= B_FALSE
;
158 while (begin
< end
) {
159 if (isdigit(*begin
)) {
160 *num
= (*num
* 10) + (*begin
- '0');
167 if (!num_found
|| (begin
!= end
))
173 * allocates memory and copies string to new memory
176 commp_add_str(char **dest
, const char *src
, int len
)
180 (*dest
) = calloc(1, len
+ 1);
183 (void) strncpy(*dest
, src
, len
);
188 * This function converts strings like "5d" to equivalent time in secs.
189 * For eg. 1h = 3600, 10d = 86400
192 commp_time_to_secs(const char *begin
, const char *end
, uint64_t *num
)
196 if (!isdigit(*(end
- 1))) {
197 switch (*(end
- 1)) {
199 factor
= COMMP_SECS_IN_DAY
;
202 factor
= COMMP_SECS_IN_HOUR
;
205 factor
= COMMP_SECS_IN_MIN
;
215 if (commp_strtoull(begin
, end
, num
) != 0)
218 (*num
) = (*num
) * factor
;