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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
36 #include <inj_string.h>
39 inj_strdup(const char *s
)
41 char *s1
= inj_alloc(strlen(s
) + 1);
48 inj_strndup(const char *s
, size_t n
)
50 char *s2
= inj_alloc(n
+ 1);
52 (void) strncpy(s2
, s
, n
+ 1);
58 inj_strfree(const char *s
)
60 inj_free((void *)s
, strlen(s
) + 1);
63 typedef struct type_desc
{
68 static const type_desc_t signed_types
[] = {
70 { INT8_MIN
, INT8_MAX
},
71 { INT16_MIN
, INT16_MAX
},
73 { INT32_MIN
, INT32_MAX
},
77 { INT64_MIN
, INT64_MAX
}
80 static const type_desc_t unsigned_types
[] = {
93 inj_strtoll(const char *str
, int width
, longlong_t
*valp
)
95 const type_desc_t
*desc
;
100 assert(width
/ 8 < (sizeof (signed_types
) /
101 sizeof (signed_types
[0])));
102 desc
= &signed_types
[width
/ 8];
103 assert(desc
->td_max
!= 0);
107 val
= strtoll(str
, &c
, 0);
108 if (*c
!= '\0' || errno
== EINVAL
)
109 return (inj_set_errno(EINVAL
));
111 if (errno
== ERANGE
|| (width
!= 0 && (val
< desc
->td_min
||
112 val
> (longlong_t
)desc
->td_max
)))
113 return (inj_set_errno(ERANGE
));
122 inj_strtoull(const char *str
, int width
, u_longlong_t
*valp
)
124 const type_desc_t
*desc
;
129 assert(width
/ 8 < (sizeof (unsigned_types
) /
130 sizeof (unsigned_types
[0])));
131 desc
= &unsigned_types
[width
/ 8];
132 assert(desc
->td_max
!= 0);
136 val
= strtoull(str
, &c
, 0);
137 if (*c
!= '\0' || errno
== EINVAL
)
138 return (inj_set_errno(EINVAL
));
140 if (errno
== ERANGE
|| (width
!= 0 && val
> desc
->td_max
))
141 return (inj_set_errno(ERANGE
));
150 inj_strtime(hrtime_t
*nsp
, const char *units
)
152 static const struct {
156 { "ns", NANOSEC
/ NANOSEC
},
157 { "nsec", NANOSEC
/ NANOSEC
},
158 { "us", NANOSEC
/ MICROSEC
},
159 { "usec", NANOSEC
/ MICROSEC
},
160 { "ms", NANOSEC
/ MILLISEC
},
161 { "msec", NANOSEC
/ MILLISEC
},
162 { "s", NANOSEC
/ SEC
},
163 { "sec", NANOSEC
/ SEC
},
164 { "m", NANOSEC
* (hrtime_t
)60 },
165 { "min", NANOSEC
* (hrtime_t
)60 },
166 { "h", NANOSEC
* (hrtime_t
)(60 * 60) },
167 { "hour", NANOSEC
* (hrtime_t
)(60 * 60) },
168 { "d", NANOSEC
* (hrtime_t
)(24 * 60 * 60) },
169 { "day", NANOSEC
* (hrtime_t
)(24 * 60 * 60) },
174 hrtime_t val
= *nsp
, mul
= 1;
177 for (i
= 0; suffix
[i
].name
!= NULL
; i
++) {
178 if (strcasecmp(suffix
[i
].name
, units
) == 0) {
184 if (suffix
[i
].name
== NULL
&& *units
!= '\0')
185 return (inj_set_errno(EINVAL
));
189 val
= NANOSEC
/ val
; /* compute val as value per sec */
198 inj_hashfn_string(void *key
)
205 for (p
= key
; *p
!= '\0'; p
++) {
208 if ((g
= (h
& 0xf0000000)) != 0) {
218 inj_hashcmp_string(void *k1
, void *k2
)
220 return (strcmp(k1
, k2
));
225 inj_hashfree_string(inj_var_t
*v
, void *arg
)
227 inj_strfree(inj_hash_get_key(v
));
231 inj_strhash_create(inj_hash_t
*h
)
233 inj_hash_create(h
, inj_hashfn_string
, inj_hashcmp_string
);
237 inj_strhash_insert(inj_hash_t
*h
, const char *str
, uintmax_t value
)
239 return (inj_hash_insert(h
, (void *)inj_strdup(str
), value
));
243 inj_strhash_lookup(inj_hash_t
*h
, const char *str
)
245 return (inj_hash_lookup(h
, (void *)str
));
249 inj_strhash_destroy(inj_hash_t
*h
)
251 inj_hash_destroy(h
, inj_hashfree_string
, NULL
);