1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20178414Sjb *
21178414Sjb * $FreeBSD$
22168404Spjd */
23168404Spjd/*
24168404Spjd * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25168404Spjd * Use is subject to license terms.
26168404Spjd */
27168404Spjd
28168404Spjd#include <sys/param.h>
29168404Spjd#include <sys/string.h>
30219089Spjd#include <sys/kmem.h>
31219089Spjd#include <machine/stdarg.h>
32168404Spjd
33168404Spjd#define	IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
34168404Spjd
35168404Spjd#define	IS_ALPHA(c)	\
36168404Spjd	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
37168404Spjd
38168404Spjdchar *
39168404Spjdstrpbrk(const char *s, const char *b)
40168404Spjd{
41168404Spjd	const char *p;
42168404Spjd
43168404Spjd	do {
44168404Spjd		for (p = b; *p != '\0' && *p != *s; ++p)
45168404Spjd			;
46168404Spjd		if (*p != '\0')
47168404Spjd			return ((char *)s);
48168404Spjd	} while (*s++);
49168404Spjd
50168404Spjd	return (NULL);
51168404Spjd}
52168404Spjd
53168404Spjd/*
54168404Spjd * Convert a string into a valid C identifier by replacing invalid
55168404Spjd * characters with '_'.  Also makes sure the string is nul-terminated
56168404Spjd * and takes up at most n bytes.
57168404Spjd */
58168404Spjdvoid
59168404Spjdstrident_canon(char *s, size_t n)
60168404Spjd{
61168404Spjd	char c;
62168404Spjd	char *end = s + n - 1;
63168404Spjd
64168404Spjd	if ((c = *s) == 0)
65168404Spjd		return;
66168404Spjd
67168404Spjd	if (!IS_ALPHA(c) && c != '_')
68168404Spjd		*s = '_';
69168404Spjd
70168404Spjd	while (s < end && ((c = *(++s)) != 0)) {
71168404Spjd		if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
72168404Spjd			*s = '_';
73168404Spjd	}
74168404Spjd	*s = 0;
75168404Spjd}
76219089Spjd
77219089Spjd/*
78219089Spjd * Do not change the length of the returned string; it must be freed
79219089Spjd * with strfree().
80219089Spjd */
81219089Spjdchar *
82219089Spjdkmem_asprintf(const char *fmt, ...)
83219089Spjd{
84219089Spjd	int size;
85219089Spjd	va_list adx;
86219089Spjd	char *buf;
87219089Spjd
88219089Spjd	va_start(adx, fmt);
89219089Spjd	size = vsnprintf(NULL, 0, fmt, adx) + 1;
90219089Spjd	va_end(adx);
91219089Spjd
92219089Spjd	buf = kmem_alloc(size, KM_SLEEP);
93219089Spjd
94219089Spjd	va_start(adx, fmt);
95219089Spjd	(void) vsnprintf(buf, size, fmt, adx);
96219089Spjd	va_end(adx);
97219089Spjd
98219089Spjd	return (buf);
99219089Spjd}
100219089Spjd
101219089Spjdvoid
102219089Spjdstrfree(char *str)
103219089Spjd{
104219089Spjd	ASSERT(str != NULL);
105219089Spjd	kmem_free(str, strlen(str) + 1);
106219089Spjd}
107