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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
31 #include <dt_string.h>
35 * Create a copy of string s, but only duplicate the first n bytes.
38 strndup(const char *s, size_t n)
40 char *s2 = malloc(n + 1);
43 longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
45 (void) strncpy(s2, s, n);
51 * Transform string s inline, converting each embedded C escape sequence string
52 * to the corresponding character. For example, the substring "\n" is replaced
53 * by an inline '\n' character. The length of the resulting string is returned.
62 for (p = q = s; (c = *p) != '\0'; p++) {
76 if (*p >= '0' && *p <= '7') {
77 c = c * 8 + *p++ - '0';
79 if (*p >= '0' && *p <= '7')
112 for (x = 0; (c = *++p) != '\0'; ) {
113 if (c >= '0' && c <= '9')
114 x = x * 16 + c - '0';
115 else if (c >= 'a' && c <= 'f')
116 x = x * 16 + c - 'a' + 10;
117 else if (c >= 'A' && c <= 'F')
118 x = x * 16 + c - 'A' + 10;
138 if ((esc = c == '\\') == 0)
144 return ((size_t)(q - s));
148 * Create a copy of string s in which certain unprintable or special characters
149 * have been converted to the string representation of their C escape sequence.
150 * For example, the newline character is expanded to the string "\n".
153 strchr2esc(const char *s, size_t n)
159 for (p = s; p < s + n; p++) {
171 addl++; /* 1 add'l char needed to follow \ */
176 if (c < '!' || c > '~')
177 addl += 3; /* 3 add'l chars following \ */
181 if ((s2 = malloc(n + addl + 1)) == NULL)
184 for (p = s, q = s2; p < s + n; p++) {
230 if (c < '!' || c > '~') {
232 *q++ = ((c >> 6) & 3) + '0';
233 *q++ = ((c >> 3) & 7) + '0';
234 *q++ = (c & 7) + '0';
240 break; /* don't continue past \0 even if p < s + n */
248 * Return the basename (name after final /) of the given string. We use
249 * strbasename rather than basename to avoid conflicting with libgen.h's
250 * non-const function prototype.
253 strbasename(const char *s)
255 const char *p = strrchr(s, '/');
264 * This function tests a string against the regular expression used for idents
265 * and integers in the D lexer, and should match the superset of RGX_IDENT and
266 * RGX_INT in dt_lex.l. If an invalid character is found, the function returns
267 * a pointer to it. Otherwise NULL is returned for a valid string.
270 strbadidnum(const char *s)
279 (void) strtoull(s, &p, 0);
281 if (errno == 0 && *p == '\0')
282 return (NULL); /* matches RGX_INT */
284 while ((c = *s++) != '\0') {
285 if (isalnum(c) == 0 && c != '_' && c != '`')
289 return (NULL); /* matches RGX_IDENT */
293 * Determine whether the string contains a glob matching pattern or is just a
294 * simple string. See gmatch(3GEN) and sh(1) for the glob syntax definition.
297 strisglob(const char *s)
301 while ((c = *s++) != '\0') {
302 if (c == '[' || c == '?' || c == '*' || c == '\\')
310 * Hyphenate a string in-place by converting any instances of "__" to "-",
311 * which we use for probe names to improve readability, and return the string.
314 strhyphenate(char *s)
318 for (p = s, q = p + strlen(p); p < q; p++) {
319 if (p[0] == '_' && p[1] == '_') {
321 bcopy(p + 2, p + 1, (size_t)(q - p) - 1);