Path: blob/main/sys/cddl/compat/opensolaris/kern/opensolaris_string.c
48383 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2007 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/2425#include <sys/param.h>26#include <sys/string.h>27#include <sys/kmem.h>28#include <sys/stdarg.h>2930#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')3132#define IS_ALPHA(c) \33(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))3435char *36strpbrk(const char *s, const char *b)37{38const char *p;3940do {41for (p = b; *p != '\0' && *p != *s; ++p)42;43if (*p != '\0')44return ((char *)s);45} while (*s++);4647return (NULL);48}4950/*51* Convert a string into a valid C identifier by replacing invalid52* characters with '_'. Also makes sure the string is nul-terminated53* and takes up at most n bytes.54*/55void56strident_canon(char *s, size_t n)57{58char c;59char *end = s + n - 1;6061if ((c = *s) == 0)62return;6364if (!IS_ALPHA(c) && c != '_')65*s = '_';6667while (s < end && ((c = *(++s)) != 0)) {68if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')69*s = '_';70}71*s = 0;72}7374/*75* Do not change the length of the returned string; it must be freed76* with strfree().77*/78char *79kmem_asprintf(const char *fmt, ...)80{81int size;82va_list adx;83char *buf;8485va_start(adx, fmt);86size = vsnprintf(NULL, 0, fmt, adx) + 1;87va_end(adx);8889buf = kmem_alloc(size, KM_SLEEP);9091va_start(adx, fmt);92(void) vsnprintf(buf, size, fmt, adx);93va_end(adx);9495return (buf);96}9798void99strfree(char *str)100{101ASSERT(str != NULL);102kmem_free(str, strlen(str) + 1);103}104105106