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 https://opensource.org/licenses/CDDL-1.0.
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]
24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/string.h>
32 #include <machine/stdarg.h>
34 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
37 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
40 strpbrk(const char *s
, const char *b
)
45 for (p
= b
; *p
!= '\0' && *p
!= *s
; ++p
)
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.
60 strident_canon(char *s
, size_t n
)
63 char *end
= s
+ n
- 1;
68 if (!IS_ALPHA(c
) && c
!= '_')
71 while (s
< end
&& ((c
= *(++s
)) != 0)) {
72 if (!IS_ALPHA(c
) && !IS_DIGIT(c
) && c
!= '_')
79 * Do not change the length of the returned string; it must be freed
83 kmem_asprintf(const char *fmt
, ...)
90 size
= vsnprintf(NULL
, 0, fmt
, adx
) + 1;
93 buf
= kmem_alloc(size
, KM_SLEEP
);
96 (void) vsnprintf(buf
, size
, fmt
, adx
);
103 kmem_strfree(char *str
)
105 ASSERT3P(str
, !=, NULL
);
106 kmem_free(str
, strlen(str
) + 1);
110 * kmem_scnprintf() will return the number of characters that it would have
111 * printed whenever it is limited by value of the size variable, rather than
112 * the number of characters that it did print. This can cause misbehavior on
113 * subsequent uses of the return value, so we define a safe version that will
114 * return the number of characters actually printed, minus the NULL format
115 * character. Subsequent use of this by the safe string functions is safe
116 * whether it is snprintf(), strlcat() or strlcpy().
120 kmem_scnprintf(char *restrict str
, size_t size
, const char *restrict fmt
, ...)
125 /* Make the 0 case a no-op so that we do not return -1 */
130 n
= vsnprintf(str
, size
, fmt
, ap
);