1 /* $NetBSD: string.c,v 1.3 2016/06/23 05:19:42 pgoyette Exp $ */ 2 3 /* 4 * CDDL HEADER START 5 * 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License (the "License"). 8 * You may not use this file except in compliance with the License. 9 * 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11 * or http://www.opensolaris.org/os/licensing. 12 * See the License for the specific language governing permissions 13 * and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL HEADER in each 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17 * If applicable, add the following below this CDDL HEADER, with the 18 * fields enclosed by brackets "[]" replaced with your own identifying 19 * information: Portions Copyright [yyyy] [name of copyright owner] 20 * 21 * CDDL HEADER END 22 */ 23 /* 24 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/string.h> 30 31 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9') 32 33 #define IS_ALPHA(c) \ 34 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 35 36 #ifndef __NetBSD__ 37 char * 38 strpbrk(const char *s, const char *b) 39 { 40 const char *p; 41 42 do { 43 for (p = b; *p != '\0' && *p != *s; ++p) 44 ; 45 if (*p != '\0') 46 return ((char *)s); 47 } while (*s++); 48 49 return (NULL); 50 } 51 #endif 52 53 /* 54 * Convert a string into a valid C identifier by replacing invalid 55 * characters with '_'. Also makes sure the string is nul-terminated 56 * and takes up at most n bytes. 57 */ 58 void 59 strident_canon(char *s, size_t n) 60 { 61 char c; 62 char *end = s + n - 1; 63 64 if ((c = *s) == 0) 65 return; 66 67 if (!IS_ALPHA(c) && c != '_') 68 *s = '_'; 69 70 while (s < end && ((c = *(++s)) != 0)) { 71 if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_') 72 *s = '_'; 73 } 74 *s = 0; 75 } 76 77 /* 78 * Simple-minded conversion of a long into a null-terminated character 79 * string. Caller must ensure there's enough space to hold the result. 80 */ 81 void 82 numtos(unsigned long num, char *s) 83 { 84 char prbuf[40]; 85 86 char *cp = prbuf; 87 88 do { 89 *cp++ = "0123456789"[num % 10]; 90 num /= 10; 91 } while (num); 92 93 do { 94 *s++ = *--cp; 95 } while (cp > prbuf); 96 *s = '\0'; 97 } 98