1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
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]
19 *
20 * CDDL HEADER END
21 *
22 * $FreeBSD$
23 */
24 /*
25 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/string.h>
32 #include <sys/kmem.h>
33 #include <sys/stdarg.h>
34
35 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
36
37 #define IS_ALPHA(c) \
38 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
39
40 char *
strpbrk(const char * s,const char * b)41 strpbrk(const char *s, const char *b)
42 {
43 const char *p;
44
45 do {
46 for (p = b; *p != '\0' && *p != *s; ++p)
47 ;
48 if (*p != '\0')
49 return ((char *)s);
50 } while (*s++);
51
52 return (NULL);
53 }
54
55 /*
56 * Convert a string into a valid C identifier by replacing invalid
57 * characters with '_'. Also makes sure the string is nul-terminated
58 * and takes up at most n bytes.
59 */
60 void
strident_canon(char * s,size_t n)61 strident_canon(char *s, size_t n)
62 {
63 char c;
64 char *end = s + n - 1;
65
66 if ((c = *s) == 0)
67 return;
68
69 if (!IS_ALPHA(c) && c != '_')
70 *s = '_';
71
72 while (s < end && ((c = *(++s)) != 0)) {
73 if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
74 *s = '_';
75 }
76 *s = 0;
77 }
78
79 /*
80 * Do not change the length of the returned string; it must be freed
81 * with strfree().
82 */
83 char *
kmem_asprintf(const char * fmt,...)84 kmem_asprintf(const char *fmt, ...)
85 {
86 int size;
87 va_list adx;
88 char *buf;
89
90 va_start(adx, fmt);
91 size = vsnprintf(NULL, 0, fmt, adx) + 1;
92 va_end(adx);
93
94 buf = kmem_alloc(size, KM_SLEEP);
95
96 va_start(adx, fmt);
97 (void) vsnprintf(buf, size, fmt, adx);
98 va_end(adx);
99
100 return (buf);
101 }
102
103 void
kmem_strfree(char * str)104 kmem_strfree(char *str)
105 {
106 ASSERT3P(str, !=, NULL);
107 kmem_free(str, strlen(str) + 1);
108 }
109
110 /*
111 * kmem_scnprintf() will return the number of characters that it would have
112 * printed whenever it is limited by value of the size variable, rather than
113 * the number of characters that it did print. This can cause misbehavior on
114 * subsequent uses of the return value, so we define a safe version that will
115 * return the number of characters actually printed, minus the NULL format
116 * character. Subsequent use of this by the safe string functions is safe
117 * whether it is snprintf(), strlcat() or strlcpy().
118 */
119
120 int
kmem_scnprintf(char * restrict str,size_t size,const char * restrict fmt,...)121 kmem_scnprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
122 {
123 int n;
124 va_list ap;
125
126 /* Make the 0 case a no-op so that we do not return -1 */
127 if (size == 0)
128 return (0);
129
130 va_start(ap, fmt);
131 n = vsnprintf(str, size, fmt, ap);
132 va_end(ap);
133
134 if (n >= size)
135 n = size - 1;
136
137 return (n);
138 }
139