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 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <dt_string.h>
37 * Create a copy of string s, but only duplicate the first n bytes.
40 strndup(const char *s, size_t n)
42 char *s2 = malloc(n + 1);
44 (void) strncpy(s2, s, n);
50 * Transform string s inline, converting each embedded C escape sequence string
51 * to the corresponding character. For example, the substring "\n" is replaced
52 * by an inline '\n' character. The length of the resulting string is returned.
61 for (p = q = s; (c = *p) != '\0'; p++) {
75 if (*p >= '0' && *p <= '7') {
76 c = c * 8 + *p++ - '0';
78 if (*p >= '0' && *p <= '7')
111 for (x = 0; (c = *++p) != '\0'; ) {
112 if (c >= '0' && c <= '9')
113 x = x * 16 + c - '0';
114 else if (c >= 'a' && c <= 'f')
115 x = x * 16 + c - 'a' + 10;
116 else if (c >= 'A' && c <= 'F')
117 x = x * 16 + c - 'A' + 10;
137 if ((esc = c == '\\') == 0)
143 return ((size_t)(q - s));
147 * Create a copy of string s in which certain unprintable or special characters
148 * have been converted to the string representation of their C escape sequence.
149 * For example, the newline character is expanded to the string "\n".
152 strchr2esc(const char *s, size_t n)
158 for (p = s; p < s + n; p++) {
170 addl++; /* 1 add'l char needed to follow \ */
175 if (c < '!' || c > '~')
176 addl += 3; /* 3 add'l chars following \ */
180 if ((s2 = malloc(n + addl + 1)) == NULL)
183 for (p = s, q = s2; p < s + n; p++) {
229 if (c < '!' || c > '~') {
231 *q++ = ((c >> 6) & 3) + '0';
232 *q++ = ((c >> 3) & 7) + '0';
233 *q++ = (c & 7) + '0';
239 break; /* don't continue past \0 even if p < s + n */
247 * Return the basename (name after final /) of the given string. We use
248 * strbasename rather than basename to avoid conflicting with libgen.h's
249 * non-const function prototype.
252 strbasename(const char *s)
254 const char *p = strrchr(s, '/');
263 * This function tests a string against the regular expression used for idents
264 * and integers in the D lexer, and should match the superset of RGX_IDENT and
265 * RGX_INT in dt_lex.l. If an invalid character is found, the function returns
266 * a pointer to it. Otherwise NULL is returned for a valid string.
269 strbadidnum(const char *s)
278 (void) strtoull(s, &p, 0);
280 if (errno == 0 && *p == '\0')
281 return (NULL); /* matches RGX_INT */
283 while ((c = *s++) != '\0') {
284 if (isalnum(c) == 0 && c != '_' && c != '`')
288 return (NULL); /* matches RGX_IDENT */
292 * Determine whether the string contains a glob matching pattern or is just a
293 * simple string. See gmatch(3GEN) and sh(1) for the glob syntax definition.
296 strisglob(const char *s)
300 while ((c = *s++) != '\0') {
301 if (c == '[' || c == '?' || c == '*' || c == '\\')
309 * Hyphenate a string in-place by converting any instances of "__" to "-",
310 * which we use for probe names to improve readability, and return the string.
313 strhyphenate(char *s)
317 for (p = s, q = p + strlen(p); p < q; p++) {
318 if (p[0] == '_' && p[1] == '_') {
320 bcopy(p + 2, p + 1, (size_t)(q - p) - 1);