11541Srgrimes/*
21541Srgrimes * CDDL HEADER START
31541Srgrimes *
41541Srgrimes * The contents of this file are subject to the terms of the
51541Srgrimes * Common Development and Distribution License (the "License").
61541Srgrimes * You may not use this file except in compliance with the License.
71541Srgrimes *
81541Srgrimes * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91541Srgrimes * or http://www.opensolaris.org/os/licensing.
101541Srgrimes * See the License for the specific language governing permissions
111541Srgrimes * and limitations under the License.
121541Srgrimes *
131541Srgrimes * When distributing Covered Code, include this CDDL HEADER in each
141541Srgrimes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151541Srgrimes * If applicable, add the following below this CDDL HEADER, with the
161541Srgrimes * fields enclosed by brackets "[]" replaced with your own identifying
171541Srgrimes * information: Portions Copyright [yyyy] [name of copyright owner]
181541Srgrimes *
191541Srgrimes * CDDL HEADER END
201541Srgrimes *
211541Srgrimes * $FreeBSD$
221541Srgrimes */
231541Srgrimes/*
241541Srgrimes * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
251541Srgrimes * Use is subject to license terms.
261541Srgrimes */
271541Srgrimes
281541Srgrimes#include <sys/param.h>
291541Srgrimes#include <sys/string.h>
301541Srgrimes#include <sys/kmem.h>
311541Srgrimes#include <machine/stdarg.h>
321541Srgrimes
331541Srgrimes#define	IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
341541Srgrimes
351541Srgrimes#define	IS_ALPHA(c)	\
3636503Speter	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
371541Srgrimes
381541Srgrimeschar *
3983651Speterstrpbrk(const char *s, const char *b)
4083651Speter{
4183651Speter	const char *p;
421541Srgrimes
431541Srgrimes	do {
441541Srgrimes		for (p = b; *p != '\0' && *p != *s; ++p)
451541Srgrimes			;
461541Srgrimes		if (*p != '\0')
4783651Speter			return ((char *)s);
481541Srgrimes	} while (*s++);
4948274Speter
5048274Speter	return (NULL);
5160041Sphk}
5231886Sbde
531541Srgrimes/*
541541Srgrimes * Convert a string into a valid C identifier by replacing invalid
551541Srgrimes * characters with '_'.  Also makes sure the string is nul-terminated
561541Srgrimes * and takes up at most n bytes.
571541Srgrimes */
581541Srgrimesvoid
591541Srgrimesstrident_canon(char *s, size_t n)
609336Sdfr{
612997Swollman	char c;
622997Swollman	char *end = s + n - 1;
6383651Speter
641541Srgrimes	if ((c = *s) == 0)
653305Sphk		return;
6612662Sdg
6712662Sdg	if (!IS_ALPHA(c) && c != '_')
6892783Sjeff		*s = '_';
693305Sphk
701541Srgrimes	while (s < end && ((c = *(++s)) != 0)) {
719336Sdfr		if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
7283651Speter			*s = '_';
7383651Speter	}
741541Srgrimes	*s = 0;
7583651Speter}
7683651Speter
771541Srgrimes/*
781541Srgrimes * Do not change the length of the returned string; it must be freed
791541Srgrimes * with strfree().
801541Srgrimes */
811541Srgrimeschar *
821541Srgrimeskmem_asprintf(const char *fmt, ...)
831541Srgrimes{
8483651Speter	int size;
8583651Speter	va_list adx;
8683651Speter	char *buf;
8783651Speter
881541Srgrimes	va_start(adx, fmt);
891541Srgrimes	size = vsnprintf(NULL, 0, fmt, adx) + 1;
9036541Speter	va_end(adx);
9112911Sphk
9283651Speter	buf = kmem_alloc(size, KM_SLEEP);
9312911Sphk
9412911Sphk	va_start(adx, fmt);
9583651Speter	(void) vsnprintf(buf, size, fmt, adx);
9683651Speter	va_end(adx);
979336Sdfr
9883651Speter	return (buf);
9983651Speter}
1009759Sbde
10183651Spetervoid
10283651Speterstrfree(char *str)
10338894Sbde{
1049336Sdfr	ASSERT(str != NULL);
1059336Sdfr	kmem_free(str, strlen(str) + 1);
1069336Sdfr}
1079336Sdfr