dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libcommputil / common / commp_util.c
blob8ed34648222671eb7efa51c132cd93091166d979
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
31 * memory.
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
39 #include "commp_util.h"
43 * Skip to the next non-whitespace
45 int
46 commp_skip_white_space(const char **begin, const char *end)
48 while (*begin < end) {
49 if (!isspace(**begin))
50 return (0);
51 (*begin)++;
53 return (1);
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,
59 * it returns 1.
61 int
62 commp_find_token(const char **begin, const char **current, const char *end,
63 char token, boolean_t last)
65 *current = *begin;
66 while (*current < end) {
67 if (!last && (**current == token))
68 break;
69 else if (isspace(**current))
70 return (1);
71 (*current)++;
73 /* Checks for leading white space */
74 if (*current == *begin)
75 return (1);
76 else
77 return (0);
81 * atoi function
83 int
84 commp_atoi(const char *begin, const char *end, int *num)
86 boolean_t num_found = B_FALSE;
88 *num = 0;
89 while (begin < end) {
90 if (isdigit(*begin)) {
91 *num = (*num * 10) + (*begin - '0');
92 num_found = B_TRUE;
93 begin++;
94 } else {
95 break;
98 if (!num_found || (begin != end))
99 return (EINVAL);
100 return (0);
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;
111 *num = 0;
112 while (begin < end) {
113 if (isdigit(*begin)) {
114 *num = (*num * 10) + (*begin - '0');
115 num_found = B_TRUE;
116 begin++;
117 } else {
118 break;
121 if (!num_found || (begin != end))
122 return (EINVAL);
123 return (0);
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;
134 *num = 0;
135 while (begin < end) {
136 if (isdigit(*begin)) {
137 *num = (*num * 10) + (*begin - '0');
138 num_found = B_TRUE;
139 begin++;
140 } else {
141 break;
144 if (!num_found || (begin != end))
145 return (EINVAL);
146 return (0);
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;
157 *num = 0;
158 while (begin < end) {
159 if (isdigit(*begin)) {
160 *num = (*num * 10) + (*begin - '0');
161 num_found = B_TRUE;
162 begin++;
163 } else {
164 break;
167 if (!num_found || (begin != end))
168 return (EINVAL);
169 return (0);
173 * allocates memory and copies string to new memory
176 commp_add_str(char **dest, const char *src, int len)
178 if (len == 0)
179 return (EINVAL);
180 (*dest) = calloc(1, len + 1);
181 if (*dest == NULL)
182 return (ENOMEM);
183 (void) strncpy(*dest, src, len);
184 return (0);
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)
194 uint_t factor = 0;
196 if (!isdigit(*(end - 1))) {
197 switch (*(end - 1)) {
198 case 'd':
199 factor = COMMP_SECS_IN_DAY;
200 break;
201 case 'h':
202 factor = COMMP_SECS_IN_HOUR;
203 break;
204 case 'm':
205 factor = COMMP_SECS_IN_MIN;
206 break;
207 case 's':
208 factor = 1;
209 break;
210 default:
211 return (EINVAL);
213 --end;
215 if (commp_strtoull(begin, end, num) != 0)
216 return (EINVAL);
217 if (factor != 0)
218 (*num) = (*num) * factor;
219 return (0);