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