xref: /titanic_53/usr/src/common/util/string.c (revision b811a51ae52cf3dd141a03c928fdcfbd6160c008)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5049fa28aSdr146992  * Common Development and Distribution License (the "License").
6049fa28aSdr146992  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227637daddSmyers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Implementations of the functions described in vsnprintf(3C) and string(3C),
287c478bd9Sstevel@tonic-gate  * for use by the kernel, the standalone, and kmdb.  Unless otherwise specified,
297c478bd9Sstevel@tonic-gate  * these functions match the section 3C manpages.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
34ae115bc7Smrj 
35ae115bc7Smrj #if defined(_KERNEL)
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
38ae115bc7Smrj #elif !defined(_BOOT)
39ae115bc7Smrj #include <string.h>
40ae115bc7Smrj #endif
41ae115bc7Smrj 
42ae115bc7Smrj #ifndef	NULL
43ae115bc7Smrj #define	NULL	0l
44ae115bc7Smrj #endif
45ae115bc7Smrj 
46ae115bc7Smrj #include "memcpy.h"
47ae115bc7Smrj #include "string.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
50ae115bc7Smrj  * We don't need these for x86 boot or kmdb.
517c478bd9Sstevel@tonic-gate  */
52ae115bc7Smrj #if !defined(_KMDB) && (!defined(_BOOT) || defined(__sparc))
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	ADDCHAR(c)	if (bufp++ - buf < buflen) bufp[-1] = (c)
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * Given a buffer 'buf' of size 'buflen', render as much of the string
587c478bd9Sstevel@tonic-gate  * described by <fmt, args> as possible.  The string will always be
597c478bd9Sstevel@tonic-gate  * null-terminated, so the maximum string length is 'buflen - 1'.
607c478bd9Sstevel@tonic-gate  * Returns the number of bytes that would be necessary to render the
617c478bd9Sstevel@tonic-gate  * entire string, not including null terminator (just like vsnprintf(3S)).
627c478bd9Sstevel@tonic-gate  * To determine buffer size in advance, use vsnprintf(NULL, 0, fmt, args) + 1.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * There is no support for floating point, and the C locale is assumed.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate size_t
677c478bd9Sstevel@tonic-gate vsnprintf(char *buf, size_t buflen, const char *fmt, va_list aargs)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	uint64_t ul, tmp;
707c478bd9Sstevel@tonic-gate 	char *bufp = buf;	/* current buffer pointer */
71186507a7Smyers 	int pad, width, base, sign, c, num;
72186507a7Smyers 	int prec, h_count, l_count, dot_count;
73186507a7Smyers 	int pad_count, transfer_count, left_align;
747c478bd9Sstevel@tonic-gate 	char *digits, *sp, *bs;
757c478bd9Sstevel@tonic-gate 	char numbuf[65];	/* sufficient for a 64-bit binary value */
767c478bd9Sstevel@tonic-gate 	va_list args;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/*
797c478bd9Sstevel@tonic-gate 	 * Make a copy so that all our callers don't have to make a copy
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	va_copy(args, aargs);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((ssize_t)buflen < 0)
847c478bd9Sstevel@tonic-gate 		buflen = 0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	while ((c = *fmt++) != '\0') {
877c478bd9Sstevel@tonic-gate 		if (c != '%') {
887c478bd9Sstevel@tonic-gate 			ADDCHAR(c);
897c478bd9Sstevel@tonic-gate 			continue;
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 
92186507a7Smyers 		width = prec = 0;
93186507a7Smyers 		left_align = base = sign = 0;
94186507a7Smyers 		h_count = l_count = dot_count = 0;
95186507a7Smyers 		pad = ' ';
96186507a7Smyers 		digits = "0123456789abcdef";
97186507a7Smyers next_fmt:
987c478bd9Sstevel@tonic-gate 		if ((c = *fmt++) == '\0')
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		if (c >= 'A' && c <= 'Z') {
1027c478bd9Sstevel@tonic-gate 			c += 'a' - 'A';
1037c478bd9Sstevel@tonic-gate 			digits = "0123456789ABCDEF";
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		switch (c) {
107186507a7Smyers 		case '-':
108186507a7Smyers 			left_align++;
109186507a7Smyers 			goto next_fmt;
110186507a7Smyers 		case '0':
111186507a7Smyers 			if (dot_count == 0)
112186507a7Smyers 				pad = '0';
113186507a7Smyers 			/*FALLTHROUGH*/
114186507a7Smyers 		case '1':
115186507a7Smyers 		case '2':
116186507a7Smyers 		case '3':
117186507a7Smyers 		case '4':
118186507a7Smyers 		case '5':
119186507a7Smyers 		case '6':
120186507a7Smyers 		case '7':
121186507a7Smyers 		case '8':
122186507a7Smyers 		case '9':
123186507a7Smyers 			num = 0;
124186507a7Smyers 			for (;;) {
125186507a7Smyers 				num = 10 * num + c - '0';
126186507a7Smyers 				c = *fmt;
127186507a7Smyers 				if (c < '0' || c > '9')
128186507a7Smyers 					break;
129186507a7Smyers 				else
130186507a7Smyers 					fmt++;
131186507a7Smyers 			}
132186507a7Smyers 			if (dot_count > 0)
133186507a7Smyers 				prec = num;
134186507a7Smyers 			else
135186507a7Smyers 				width = num;
136186507a7Smyers 
137186507a7Smyers 			goto next_fmt;
138186507a7Smyers 		case '.':
139186507a7Smyers 			dot_count++;
140186507a7Smyers 			goto next_fmt;
141186507a7Smyers 		case '*':
142049fa28aSdr146992 			if (dot_count > 0)
143049fa28aSdr146992 				prec = (int)va_arg(args, int);
144049fa28aSdr146992 			else
145186507a7Smyers 				width = (int)va_arg(args, int);
146186507a7Smyers 			goto next_fmt;
147186507a7Smyers 		case 'l':
148186507a7Smyers 			l_count++;
149186507a7Smyers 			goto next_fmt;
150186507a7Smyers 		case 'h':
151186507a7Smyers 			h_count++;
152186507a7Smyers 			goto next_fmt;
1537c478bd9Sstevel@tonic-gate 		case 'd':
1547c478bd9Sstevel@tonic-gate 			sign = 1;
1557c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1567c478bd9Sstevel@tonic-gate 		case 'u':
1577c478bd9Sstevel@tonic-gate 			base = 10;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 		case 'p':
160186507a7Smyers 			l_count = 1;
1617c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1627c478bd9Sstevel@tonic-gate 		case 'x':
1637c478bd9Sstevel@tonic-gate 			base = 16;
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		case 'o':
1667c478bd9Sstevel@tonic-gate 			base = 8;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		case 'b':
169186507a7Smyers 			l_count = 0;
1707c478bd9Sstevel@tonic-gate 			base = 1;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		case 'c':
173fc77c268Smyers 			c = (char)va_arg(args, int);
174186507a7Smyers 			ADDCHAR(c);
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		case 's':
1777c478bd9Sstevel@tonic-gate 			sp = va_arg(args, char *);
178186507a7Smyers 			if (sp == NULL) {
1797c478bd9Sstevel@tonic-gate 				sp = "<null string>";
180186507a7Smyers 				/* avoid truncation */
181186507a7Smyers 				prec = strlen(sp);
182186507a7Smyers 			}
183186507a7Smyers 			/*
184186507a7Smyers 			 * Handle simple case specially to avoid
185186507a7Smyers 			 * performance hit of strlen()
186186507a7Smyers 			 */
187186507a7Smyers 			if (prec == 0 && width == 0) {
1887c478bd9Sstevel@tonic-gate 				while ((c = *sp++) != 0)
1897c478bd9Sstevel@tonic-gate 					ADDCHAR(c);
1907c478bd9Sstevel@tonic-gate 				break;
191186507a7Smyers 			}
192186507a7Smyers 			if (prec > 0) {
1937637daddSmyers 				transfer_count = strnlen(sp, prec);
194186507a7Smyers 				/* widen field if too narrow */
195186507a7Smyers 				if (prec > width)
196186507a7Smyers 					width = prec;
1977637daddSmyers 			} else
1987637daddSmyers 				transfer_count = strlen(sp);
199186507a7Smyers 			if (width > transfer_count)
200186507a7Smyers 				pad_count = width - transfer_count;
201186507a7Smyers 			else
202186507a7Smyers 				pad_count = 0;
203186507a7Smyers 			while ((!left_align) && (pad_count-- > 0))
204186507a7Smyers 				ADDCHAR(' ');
205186507a7Smyers 			/* ADDCHAR() evaluates arg at most once */
206186507a7Smyers 			while (transfer_count-- > 0)
207186507a7Smyers 				ADDCHAR(*sp++);
208186507a7Smyers 			while ((left_align) && (pad_count-- > 0))
209186507a7Smyers 				ADDCHAR(' ');
210186507a7Smyers 			break;
2117c478bd9Sstevel@tonic-gate 		case '%':
2127c478bd9Sstevel@tonic-gate 			ADDCHAR('%');
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if (base == 0)
2177c478bd9Sstevel@tonic-gate 			continue;
2187c478bd9Sstevel@tonic-gate 
219186507a7Smyers 		if (h_count == 0 && l_count == 0)
220186507a7Smyers 			if (sign)
2217c478bd9Sstevel@tonic-gate 				ul = (int64_t)va_arg(args, int);
222186507a7Smyers 			else
223186507a7Smyers 				ul = (int64_t)va_arg(args, unsigned int);
224186507a7Smyers 		else if (l_count > 1)
225186507a7Smyers 			if (sign)
226186507a7Smyers 				ul = (int64_t)va_arg(args, int64_t);
227186507a7Smyers 			else
228186507a7Smyers 				ul = (int64_t)va_arg(args, uint64_t);
229186507a7Smyers 		else if (l_count > 0)
230186507a7Smyers 			if (sign)
2317c478bd9Sstevel@tonic-gate 				ul = (int64_t)va_arg(args, long);
2327c478bd9Sstevel@tonic-gate 			else
233186507a7Smyers 				ul = (int64_t)va_arg(args, unsigned long);
234186507a7Smyers 		else if (h_count > 1)
235186507a7Smyers 			if (sign)
236fc77c268Smyers 				ul = (int64_t)((char)va_arg(args, int));
237186507a7Smyers 			else
238fc77c268Smyers 				ul = (int64_t)((unsigned char)va_arg(args,
239fc77c268Smyers 				    int));
240186507a7Smyers 		else if (h_count > 0)
241186507a7Smyers 			if (sign)
242fc77c268Smyers 				ul = (int64_t)((short)va_arg(args, int));
243186507a7Smyers 			else
244fc77c268Smyers 				ul = (int64_t)((unsigned short)va_arg(args,
245fc77c268Smyers 				    int));
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		if (sign && (int64_t)ul < 0)
2487c478bd9Sstevel@tonic-gate 			ul = -ul;
2497c478bd9Sstevel@tonic-gate 		else
2507c478bd9Sstevel@tonic-gate 			sign = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		if (c == 'b') {
2537c478bd9Sstevel@tonic-gate 			bs = va_arg(args, char *);
2547c478bd9Sstevel@tonic-gate 			base = *bs++;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 
257186507a7Smyers 		/* avoid repeated division if width is 0 */
258186507a7Smyers 		if (width > 0) {
2597c478bd9Sstevel@tonic-gate 			tmp = ul;
2607c478bd9Sstevel@tonic-gate 			do {
2617c478bd9Sstevel@tonic-gate 				width--;
2627c478bd9Sstevel@tonic-gate 			} while ((tmp /= base) != 0);
263186507a7Smyers 		}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (sign && pad == '0')
2667c478bd9Sstevel@tonic-gate 			ADDCHAR('-');
2677c478bd9Sstevel@tonic-gate 		while (width-- > sign)
2687c478bd9Sstevel@tonic-gate 			ADDCHAR(pad);
2697c478bd9Sstevel@tonic-gate 		if (sign && pad == ' ')
2707c478bd9Sstevel@tonic-gate 			ADDCHAR('-');
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		sp = numbuf;
2737c478bd9Sstevel@tonic-gate 		tmp = ul;
2747c478bd9Sstevel@tonic-gate 		do {
2757c478bd9Sstevel@tonic-gate 			*sp++ = digits[tmp % base];
2767c478bd9Sstevel@tonic-gate 		} while ((tmp /= base) != 0);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		while (sp > numbuf) {
2797c478bd9Sstevel@tonic-gate 			sp--;
2807c478bd9Sstevel@tonic-gate 			ADDCHAR(*sp);
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		if (c == 'b' && ul != 0) {
2847c478bd9Sstevel@tonic-gate 			int any = 0;
2857c478bd9Sstevel@tonic-gate 			c = *bs++;
2867c478bd9Sstevel@tonic-gate 			while (c != 0) {
2877c478bd9Sstevel@tonic-gate 				if (ul & (1 << (c - 1))) {
2887c478bd9Sstevel@tonic-gate 					if (any++ == 0)
2897c478bd9Sstevel@tonic-gate 						ADDCHAR('<');
2907c478bd9Sstevel@tonic-gate 					while ((c = *bs++) >= 32)
2917c478bd9Sstevel@tonic-gate 						ADDCHAR(c);
2927c478bd9Sstevel@tonic-gate 					ADDCHAR(',');
2937c478bd9Sstevel@tonic-gate 				} else {
2947c478bd9Sstevel@tonic-gate 					while ((c = *bs++) >= 32)
2957c478bd9Sstevel@tonic-gate 						continue;
2967c478bd9Sstevel@tonic-gate 				}
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 			if (any) {
2997c478bd9Sstevel@tonic-gate 				bufp--;
3007c478bd9Sstevel@tonic-gate 				ADDCHAR('>');
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	if (bufp - buf < buflen)
3057c478bd9Sstevel@tonic-gate 		bufp[0] = c;
3067c478bd9Sstevel@tonic-gate 	else if (buflen != 0)
3077c478bd9Sstevel@tonic-gate 		buf[buflen - 1] = c;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	va_end(args);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	return (bufp - buf);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
3157c478bd9Sstevel@tonic-gate size_t
3167c478bd9Sstevel@tonic-gate snprintf(char *buf, size_t buflen, const char *fmt, ...)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	va_list args;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
3217c478bd9Sstevel@tonic-gate 	buflen = vsnprintf(buf, buflen, fmt, args);
3227c478bd9Sstevel@tonic-gate 	va_end(args);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (buflen);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
327ae115bc7Smrj #if defined(_BOOT) && defined(__sparc)
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * The sprintf() and vsprintf() routines aren't shared with the kernel because
3307c478bd9Sstevel@tonic-gate  * the DDI mandates that they return the buffer rather than its length.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
3337c478bd9Sstevel@tonic-gate int
3347c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *fmt, ...)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	va_list args;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
3397c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, INT_MAX, fmt, args);
3407c478bd9Sstevel@tonic-gate 	va_end(args);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	return (strlen(buf));
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate int
3467c478bd9Sstevel@tonic-gate vsprintf(char *buf, const char *fmt, va_list args)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, INT_MAX, fmt, args);
3497c478bd9Sstevel@tonic-gate 	return (strlen(buf));
3507c478bd9Sstevel@tonic-gate }
351ae115bc7Smrj #endif /* _BOOT && __sparc */
3527c478bd9Sstevel@tonic-gate 
353ae115bc7Smrj #endif /* !_KMDB && (!_BOOT || __sparc) */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate char *
3567c478bd9Sstevel@tonic-gate strcat(char *s1, const char *s2)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	char *os1 = s1;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	while (*s1++ != '\0')
3617c478bd9Sstevel@tonic-gate 		;
3627c478bd9Sstevel@tonic-gate 	s1--;
3637c478bd9Sstevel@tonic-gate 	while ((*s1++ = *s2++) != '\0')
3647c478bd9Sstevel@tonic-gate 		;
3657c478bd9Sstevel@tonic-gate 	return (os1);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate char *
3697c478bd9Sstevel@tonic-gate strchr(const char *sp, int c)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	do {
3727c478bd9Sstevel@tonic-gate 		if (*sp == (char)c)
3737c478bd9Sstevel@tonic-gate 			return ((char *)sp);
3747c478bd9Sstevel@tonic-gate 	} while (*sp++);
3757c478bd9Sstevel@tonic-gate 	return (NULL);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate int
3797c478bd9Sstevel@tonic-gate strcmp(const char *s1, const char *s2)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	while (*s1 == *s2++)
3827c478bd9Sstevel@tonic-gate 		if (*s1++ == '\0')
3837c478bd9Sstevel@tonic-gate 			return (0);
3847c478bd9Sstevel@tonic-gate 	return (*(unsigned char *)s1 - *(unsigned char *)--s2);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate int
3887c478bd9Sstevel@tonic-gate strncmp(const char *s1, const char *s2, size_t n)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	if (s1 == s2)
3917c478bd9Sstevel@tonic-gate 		return (0);
3927c478bd9Sstevel@tonic-gate 	n++;
3937c478bd9Sstevel@tonic-gate 	while (--n != 0 && *s1 == *s2++)
3947c478bd9Sstevel@tonic-gate 		if (*s1++ == '\0')
3957c478bd9Sstevel@tonic-gate 			return (0);
3967c478bd9Sstevel@tonic-gate 	return ((n == 0) ? 0 : *(unsigned char *)s1 - *(unsigned char *)--s2);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static const char charmap[] = {
4007c478bd9Sstevel@tonic-gate 	'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
4017c478bd9Sstevel@tonic-gate 	'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
4027c478bd9Sstevel@tonic-gate 	'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
4037c478bd9Sstevel@tonic-gate 	'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
4047c478bd9Sstevel@tonic-gate 	'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
4057c478bd9Sstevel@tonic-gate 	'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
4067c478bd9Sstevel@tonic-gate 	'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
4077c478bd9Sstevel@tonic-gate 	'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
4087c478bd9Sstevel@tonic-gate 	'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
4097c478bd9Sstevel@tonic-gate 	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
4107c478bd9Sstevel@tonic-gate 	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
4117c478bd9Sstevel@tonic-gate 	'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
4127c478bd9Sstevel@tonic-gate 	'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
4137c478bd9Sstevel@tonic-gate 	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
4147c478bd9Sstevel@tonic-gate 	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
4157c478bd9Sstevel@tonic-gate 	'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
4167c478bd9Sstevel@tonic-gate 	'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
4177c478bd9Sstevel@tonic-gate 	'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
4187c478bd9Sstevel@tonic-gate 	'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
4197c478bd9Sstevel@tonic-gate 	'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
4207c478bd9Sstevel@tonic-gate 	'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
4217c478bd9Sstevel@tonic-gate 	'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
4227c478bd9Sstevel@tonic-gate 	'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
4237c478bd9Sstevel@tonic-gate 	'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
4247c478bd9Sstevel@tonic-gate 	'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
4257c478bd9Sstevel@tonic-gate 	'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
4267c478bd9Sstevel@tonic-gate 	'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
4277c478bd9Sstevel@tonic-gate 	'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
4287c478bd9Sstevel@tonic-gate 	'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
4297c478bd9Sstevel@tonic-gate 	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
4307c478bd9Sstevel@tonic-gate 	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
4317c478bd9Sstevel@tonic-gate 	'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
4327c478bd9Sstevel@tonic-gate };
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate int
4357c478bd9Sstevel@tonic-gate strcasecmp(const char *s1, const char *s2)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	const unsigned char *cm = (const unsigned char *)charmap;
4387c478bd9Sstevel@tonic-gate 	const unsigned char *us1 = (const unsigned char *)s1;
4397c478bd9Sstevel@tonic-gate 	const unsigned char *us2 = (const unsigned char *)s2;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	while (cm[*us1] == cm[*us2++])
4427c478bd9Sstevel@tonic-gate 		if (*us1++ == '\0')
4437c478bd9Sstevel@tonic-gate 			return (0);
4447c478bd9Sstevel@tonic-gate 	return (cm[*us1] - cm[*(us2 - 1)]);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate int
4487c478bd9Sstevel@tonic-gate strncasecmp(const char *s1, const char *s2, size_t n)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	const unsigned char *cm = (const unsigned char *)charmap;
4517c478bd9Sstevel@tonic-gate 	const unsigned char *us1 = (const unsigned char *)s1;
4527c478bd9Sstevel@tonic-gate 	const unsigned char *us2 = (const unsigned char *)s2;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	while (n != 0 && cm[*us1] == cm[*us2++]) {
4557c478bd9Sstevel@tonic-gate 		if (*us1++ == '\0')
4567c478bd9Sstevel@tonic-gate 			return (0);
4577c478bd9Sstevel@tonic-gate 		n--;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	return (n == 0 ? 0 : cm[*us1] - cm[*(us2 - 1)]);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate char *
4637c478bd9Sstevel@tonic-gate strcpy(char *s1, const char *s2)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	char *os1 = s1;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	while ((*s1++ = *s2++) != '\0')
4687c478bd9Sstevel@tonic-gate 		;
4697c478bd9Sstevel@tonic-gate 	return (os1);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate char *
4737c478bd9Sstevel@tonic-gate strncpy(char *s1, const char *s2, size_t n)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	char *os1 = s1;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	n++;
4787c478bd9Sstevel@tonic-gate 	while (--n != 0 && (*s1++ = *s2++) != '\0')
4797c478bd9Sstevel@tonic-gate 		;
4807c478bd9Sstevel@tonic-gate 	if (n != 0)
4817c478bd9Sstevel@tonic-gate 		while (--n != 0)
4827c478bd9Sstevel@tonic-gate 			*s1++ = '\0';
4837c478bd9Sstevel@tonic-gate 	return (os1);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate char *
4877c478bd9Sstevel@tonic-gate strrchr(const char *sp, int c)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	char *r = NULL;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	do {
4927c478bd9Sstevel@tonic-gate 		if (*sp == (char)c)
4937c478bd9Sstevel@tonic-gate 			r = (char *)sp;
4947c478bd9Sstevel@tonic-gate 	} while (*sp++);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	return (r);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate char *
5007c478bd9Sstevel@tonic-gate strstr(const char *as1, const char *as2)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	const char *s1, *s2;
5037c478bd9Sstevel@tonic-gate 	const char *tptr;
5047c478bd9Sstevel@tonic-gate 	char c;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	s1 = as1;
5077c478bd9Sstevel@tonic-gate 	s2 = as2;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (s2 == NULL || *s2 == '\0')
5107c478bd9Sstevel@tonic-gate 		return ((char *)s1);
5117c478bd9Sstevel@tonic-gate 	c = *s2;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	while (*s1)
5147c478bd9Sstevel@tonic-gate 		if (*s1++ == c) {
5157c478bd9Sstevel@tonic-gate 			tptr = s1;
5167c478bd9Sstevel@tonic-gate 			while ((c = *++s2) == *s1++ && c)
5177c478bd9Sstevel@tonic-gate 				;
5187c478bd9Sstevel@tonic-gate 			if (c == 0)
5197c478bd9Sstevel@tonic-gate 				return ((char *)tptr - 1);
5207c478bd9Sstevel@tonic-gate 			s1 = tptr;
5217c478bd9Sstevel@tonic-gate 			s2 = as2;
5227c478bd9Sstevel@tonic-gate 			c = *s2;
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	return (NULL);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate char *
5297c478bd9Sstevel@tonic-gate strpbrk(const char *string, const char *brkset)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	const char *p;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	do {
5347c478bd9Sstevel@tonic-gate 		for (p = brkset; *p != '\0' && *p != *string; ++p)
5357c478bd9Sstevel@tonic-gate 			;
5367c478bd9Sstevel@tonic-gate 		if (*p != '\0')
5377c478bd9Sstevel@tonic-gate 			return ((char *)string);
5387c478bd9Sstevel@tonic-gate 	} while (*string++);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	return (NULL);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate char *
5447c478bd9Sstevel@tonic-gate strncat(char *s1, const char *s2, size_t n)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	char *os1 = s1;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	n++;
5497c478bd9Sstevel@tonic-gate 	while (*s1++ != '\0')
5507c478bd9Sstevel@tonic-gate 		;
5517c478bd9Sstevel@tonic-gate 	--s1;
5527c478bd9Sstevel@tonic-gate 	while ((*s1++ = *s2++) != '\0') {
5537c478bd9Sstevel@tonic-gate 		if (--n == 0) {
5547c478bd9Sstevel@tonic-gate 			s1[-1] = '\0';
5557c478bd9Sstevel@tonic-gate 			break;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	return (os1);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate #if defined(_BOOT) || defined(_KMDB)
5627c478bd9Sstevel@tonic-gate #define	bcopy(src, dst, n)	(void) memcpy((dst), (src), (n))
5637c478bd9Sstevel@tonic-gate #endif
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate size_t
5667c478bd9Sstevel@tonic-gate strlcat(char *dst, const char *src, size_t dstsize)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	char *df = dst;
5697c478bd9Sstevel@tonic-gate 	size_t left = dstsize;
5707c478bd9Sstevel@tonic-gate 	size_t l1;
5717c478bd9Sstevel@tonic-gate 	size_t l2 = strlen(src);
5727c478bd9Sstevel@tonic-gate 	size_t copied;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	while (left-- != 0 && *df != '\0')
5757c478bd9Sstevel@tonic-gate 		df++;
576ae115bc7Smrj 	/*LINTED: possible ptrdiff_t overflow*/
577ae115bc7Smrj 	l1 = (size_t)(df - dst);
5787c478bd9Sstevel@tonic-gate 	if (dstsize == l1)
5797c478bd9Sstevel@tonic-gate 		return (l1 + l2);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
5827c478bd9Sstevel@tonic-gate 	bcopy(src, dst + l1, copied);
5837c478bd9Sstevel@tonic-gate 	dst[l1+copied] = '\0';
5847c478bd9Sstevel@tonic-gate 	return (l1 + l2);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate size_t
5887c478bd9Sstevel@tonic-gate strlcpy(char *dst, const char *src, size_t len)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	size_t slen = strlen(src);
5917c478bd9Sstevel@tonic-gate 	size_t copied;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (len == 0)
5947c478bd9Sstevel@tonic-gate 		return (slen);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (slen >= len)
5977c478bd9Sstevel@tonic-gate 		copied = len - 1;
5987c478bd9Sstevel@tonic-gate 	else
5997c478bd9Sstevel@tonic-gate 		copied = slen;
6007c478bd9Sstevel@tonic-gate 	bcopy(src, dst, copied);
6017c478bd9Sstevel@tonic-gate 	dst[copied] = '\0';
6027c478bd9Sstevel@tonic-gate 	return (slen);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate size_t
6067c478bd9Sstevel@tonic-gate strspn(const char *string, const char *charset)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	const char *p, *q;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	for (q = string; *q != '\0'; ++q) {
6117c478bd9Sstevel@tonic-gate 		for (p = charset; *p != '\0' && *p != *q; ++p)
6127c478bd9Sstevel@tonic-gate 			;
6137c478bd9Sstevel@tonic-gate 		if (*p == '\0')
6147c478bd9Sstevel@tonic-gate 			break;
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
617ae115bc7Smrj 	/*LINTED: possible ptrdiff_t overflow*/
618ae115bc7Smrj 	return ((size_t)(q - string));
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
621da6c28aaSamw size_t
622da6c28aaSamw strcspn(const char *string, const char *charset)
623da6c28aaSamw {
624da6c28aaSamw 	const char *p, *q;
625da6c28aaSamw 
626da6c28aaSamw 	for (q = string; *q != '\0'; ++q) {
627da6c28aaSamw 		for (p = charset; *p != '\0' && *p != *q; ++p)
628da6c28aaSamw 			;
629da6c28aaSamw 		if (*p != '\0')
630da6c28aaSamw 			break;
631da6c28aaSamw 	}
632da6c28aaSamw 
633da6c28aaSamw 	/*LINTED E_PTRDIFF_OVERFLOW*/
634da6c28aaSamw 	return ((size_t)(q - string));
635da6c28aaSamw }
636da6c28aaSamw 
637da6c28aaSamw /*
638da6c28aaSamw  * strsep
639da6c28aaSamw  *
640da6c28aaSamw  * The strsep() function locates, in the string referenced by *stringp, the
641da6c28aaSamw  * first occurrence of any character in the string delim (or the terminating
642da6c28aaSamw  * `\0' character) and replaces it with a `\0'.  The location of the next
643da6c28aaSamw  * character after the delimiter character (or NULL, if the end of the
644da6c28aaSamw  * string was reached) is stored in *stringp.  The original value of
645da6c28aaSamw  * *stringp is returned.
646da6c28aaSamw  *
647da6c28aaSamw  * If *stringp is initially NULL, strsep() returns NULL.
648*b811a51aSVladimir Kotal  *
649*b811a51aSVladimir Kotal  * NOTE: This instance is left for in-kernel use. Libraries and programs
650*b811a51aSVladimir Kotal  *       should use strsep from libc.
651da6c28aaSamw  */
652da6c28aaSamw char *
653da6c28aaSamw strsep(char **stringp, const char *delim)
654da6c28aaSamw {
655da6c28aaSamw 	char *s;
656da6c28aaSamw 	const char *spanp;
657da6c28aaSamw 	int c, sc;
658da6c28aaSamw 	char *tok;
659da6c28aaSamw 
660da6c28aaSamw 	if ((s = *stringp) == NULL)
661da6c28aaSamw 		return (NULL);
662da6c28aaSamw 
663da6c28aaSamw 	for (tok = s; ; ) {
664da6c28aaSamw 		c = *s++;
665da6c28aaSamw 		spanp = delim;
666da6c28aaSamw 		do {
667da6c28aaSamw 			if ((sc = *spanp++) == c) {
668da6c28aaSamw 				if (c == 0)
669da6c28aaSamw 					s = NULL;
670da6c28aaSamw 				else
671da6c28aaSamw 					s[-1] = 0;
672da6c28aaSamw 				*stringp = s;
673da6c28aaSamw 				return (tok);
674da6c28aaSamw 			}
675da6c28aaSamw 		} while (sc != 0);
676da6c28aaSamw 	}
677da6c28aaSamw 	/* NOTREACHED */
678da6c28aaSamw }
679da6c28aaSamw 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * Unless mentioned otherwise, all of the routines below should be added to
6827c478bd9Sstevel@tonic-gate  * the Solaris DDI as necessary.  For now, only provide them to standalone.
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate #if defined(_BOOT) || defined(_KMDB)
6857c478bd9Sstevel@tonic-gate char *
6867c478bd9Sstevel@tonic-gate strtok(char *string, const char *sepset)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	char		*p, *q, *r;
6897c478bd9Sstevel@tonic-gate 	static char	*savept;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Set `p' to our current location in the string.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	p = (string == NULL) ? savept : string;
6957c478bd9Sstevel@tonic-gate 	if (p == NULL)
6967c478bd9Sstevel@tonic-gate 		return (NULL);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * Skip leading separators; bail if no tokens remain.
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	q = p + strspn(p, sepset);
7027c478bd9Sstevel@tonic-gate 	if (*q == '\0')
7037c478bd9Sstevel@tonic-gate 		return (NULL);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Mark the end of the token and set `savept' for the next iteration.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	if ((r = strpbrk(q, sepset)) == NULL)
7097c478bd9Sstevel@tonic-gate 		savept = NULL;
7107c478bd9Sstevel@tonic-gate 	else {
7117c478bd9Sstevel@tonic-gate 		*r = '\0';
7127c478bd9Sstevel@tonic-gate 		savept = ++r;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	return (q);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate  * The strlen() routine isn't shared with the kernel because it has its own
7207c478bd9Sstevel@tonic-gate  * hand-tuned assembly version.
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate size_t
7237c478bd9Sstevel@tonic-gate strlen(const char *s)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	size_t n = 0;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	while (*s++)
7287c478bd9Sstevel@tonic-gate 		n++;
7297c478bd9Sstevel@tonic-gate 	return (n);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate #endif /* _BOOT || _KMDB */
7337c478bd9Sstevel@tonic-gate 
7347637daddSmyers /*
7357637daddSmyers  * Returns the number of non-NULL bytes in string argument,
7367637daddSmyers  * but not more than maxlen.  Does not look past str + maxlen.
7377637daddSmyers  */
7387637daddSmyers size_t
7397637daddSmyers strnlen(const char *s, size_t maxlen)
7407637daddSmyers {
7417637daddSmyers 	size_t n = 0;
7427637daddSmyers 
7437637daddSmyers 	while (maxlen != 0 && *s != 0) {
7447637daddSmyers 		s++;
7457637daddSmyers 		maxlen--;
7467637daddSmyers 		n++;
7477637daddSmyers 	}
7487637daddSmyers 
7497637daddSmyers 	return (n);
7507637daddSmyers }
7517637daddSmyers 
7527637daddSmyers 
7537c478bd9Sstevel@tonic-gate #ifdef _KERNEL
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * Check for a valid C identifier:
7567c478bd9Sstevel@tonic-gate  *	a letter or underscore, followed by
7577c478bd9Sstevel@tonic-gate  *	zero or more letters, digits and underscores.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate #define	IS_DIGIT(c)	((c) >= '0' && (c) <= '9')
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate #define	IS_ALPHA(c)	\
7637c478bd9Sstevel@tonic-gate 	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate int
7667c478bd9Sstevel@tonic-gate strident_valid(const char *id)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	int c = *id++;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (!IS_ALPHA(c) && c != '_')
7717c478bd9Sstevel@tonic-gate 		return (0);
7727c478bd9Sstevel@tonic-gate 	while ((c = *id++) != 0) {
7737c478bd9Sstevel@tonic-gate 		if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
7747c478bd9Sstevel@tonic-gate 			return (0);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	return (1);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * Convert a string into a valid C identifier by replacing invalid
7817c478bd9Sstevel@tonic-gate  * characters with '_'.  Also makes sure the string is nul-terminated
7827c478bd9Sstevel@tonic-gate  * and takes up at most n bytes.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate void
7857c478bd9Sstevel@tonic-gate strident_canon(char *s, size_t n)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	char c;
7887c478bd9Sstevel@tonic-gate 	char *end = s + n - 1;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	ASSERT(n > 0);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if ((c = *s) == 0)
7937c478bd9Sstevel@tonic-gate 		return;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (!IS_ALPHA(c) && c != '_')
7967c478bd9Sstevel@tonic-gate 		*s = '_';
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	while (s < end && ((c = *(++s)) != 0)) {
7997c478bd9Sstevel@tonic-gate 		if (!IS_ALPHA(c) && !IS_DIGIT(c) && c != '_')
8007c478bd9Sstevel@tonic-gate 			*s = '_';
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	*s = 0;
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
806