1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 *
21 * $FreeBSD$
22 */
23 /*
24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/string.h>
31 #include <sys/kmem.h>
32 #include <machine/stdarg.h>
33
34 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
35
36 #define IS_ALPHA(c) \
37 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
38
39 char *
strpbrk(const char * s,const char * b)40 strpbrk(const char *s, const char *b)
41 {
42 const char *p;
43
44 do {
45 for (p = b; *p != '\0' && *p != *s; ++p)
46 ;
47 if (*p != '\0')
48 return ((char *)s);
49 } while (*s++);
50
51 return (NULL);
52 }
53
54 /*
55 * Convert a string into a valid C identifier by replacing invalid
56 * characters with '_'. Also makes sure the string is nul-terminated
57 * and takes up at most n bytes.
58 */
59 void
strident_canon(char * s,size_t n)60 strident_canon(char *s, size_t n)
61 {
62 char c;
63 char *end = s + n - 1;
64
65 if ((c = *s) == 0)
66 return;
67
68 if (!IS_ALPHA(c) && c != '_')
69 *s = '_';
70
71 while (s < end && ((c = *(++s)) != 0)) {
72 if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
73 *s = '_';
74 }
75 *s = 0;
76 }
77
78 /*
79 * Do not change the length of the returned string; it must be freed
80 * with strfree().
81 */
82 char *
kmem_asprintf(const char * fmt,...)83 kmem_asprintf(const char *fmt, ...)
84 {
85 int size;
86 va_list adx;
87 char *buf;
88
89 va_start(adx, fmt);
90 size = vsnprintf(NULL, 0, fmt, adx) + 1;
91 va_end(adx);
92
93 buf = kmem_alloc(size, KM_SLEEP);
94
95 va_start(adx, fmt);
96 (void) vsnprintf(buf, size, fmt, adx);
97 va_end(adx);
98
99 return (buf);
100 }
101
102 void
kmem_strfree(char * str)103 kmem_strfree(char *str)
104 {
105 ASSERT(str != NULL);
106 kmem_free(str, strlen(str) + 1);
107 }
108