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