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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <netinet/in.h>
287c478bd9Sstevel@tonic-gate #include <limits.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
337c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
347c478bd9Sstevel@tonic-gate #include <mdb/mdb_lex.h>
357c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
367c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Convert the specified integer value to a string represented in the given
407c478bd9Sstevel@tonic-gate  * base.  The flags parameter is a bitfield of the formatting flags defined in
417c478bd9Sstevel@tonic-gate  * mdb_string.h.  A pointer to a static conversion buffer is returned.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate const char *
numtostr(uintmax_t uvalue,int base,uint_t flags)447c478bd9Sstevel@tonic-gate numtostr(uintmax_t uvalue, int base, uint_t flags)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	static const char ldigits[] = "0123456789abcdef";
477c478bd9Sstevel@tonic-gate 	static const char udigits[] = "0123456789ABCDEF";
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	static char buf[68]; /* Enough for ULLONG_MAX in binary plus prefixes */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	const char *digits = (flags & NTOS_UPCASE) ? udigits : ldigits;
527c478bd9Sstevel@tonic-gate 	int i = sizeof (buf);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	intmax_t value = (intmax_t)uvalue;
557c478bd9Sstevel@tonic-gate 	int neg = (flags & NTOS_UNSIGNED) == 0 && value < 0;
567c478bd9Sstevel@tonic-gate 	uintmax_t rem = neg ? -value : value;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	buf[--i] = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	do {
617c478bd9Sstevel@tonic-gate 		buf[--i] = digits[rem % base];
627c478bd9Sstevel@tonic-gate 		rem /= base;
637c478bd9Sstevel@tonic-gate 	} while (rem != 0);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (flags & NTOS_SHOWBASE) {
667c478bd9Sstevel@tonic-gate 		uintmax_t lim;
677c478bd9Sstevel@tonic-gate 		char c = 0;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		switch (base) {
707c478bd9Sstevel@tonic-gate 		case 2:
717c478bd9Sstevel@tonic-gate 			lim = 1;
727c478bd9Sstevel@tonic-gate 			c = 'i';
737c478bd9Sstevel@tonic-gate 			break;
747c478bd9Sstevel@tonic-gate 		case 8:
757c478bd9Sstevel@tonic-gate 			lim = 7;
767c478bd9Sstevel@tonic-gate 			c = 'o';
777c478bd9Sstevel@tonic-gate 			break;
787c478bd9Sstevel@tonic-gate 		case 10:
797c478bd9Sstevel@tonic-gate 			lim = 9;
807c478bd9Sstevel@tonic-gate 			c = 't';
817c478bd9Sstevel@tonic-gate 			break;
827c478bd9Sstevel@tonic-gate 		case 16:
837c478bd9Sstevel@tonic-gate 			lim = 9;
847c478bd9Sstevel@tonic-gate 			c = 'x';
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		if (c != 0 && uvalue > lim) {
897c478bd9Sstevel@tonic-gate 			buf[--i] = c;
907c478bd9Sstevel@tonic-gate 			buf[--i] = '0';
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (neg)
957c478bd9Sstevel@tonic-gate 		buf[--i] = '-';
967c478bd9Sstevel@tonic-gate 	else if (flags & NTOS_SIGNPOS)
977c478bd9Sstevel@tonic-gate 		buf[--i] = '+';
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return ((const char *)(&buf[i]));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	CTOI(x)	(((x) >= '0' && (x) <= '9') ? (x) - '0' : \
1037c478bd9Sstevel@tonic-gate 	((x) >= 'a' && (x) <= 'z') ? (x) + 10 - 'a' : (x) + 10 - 'A')
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Convert a string to an unsigned integer value using the specified base.
1077c478bd9Sstevel@tonic-gate  * In the event of overflow or an invalid character, we generate an
1087c478bd9Sstevel@tonic-gate  * error message and longjmp back to the main loop using yyerror().
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate uintmax_t
mdb_strtonum(const char * s,int base)111*4585130bSYuri Pankov mdb_strtonum(const char *s, int base)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	uintmax_t multmax = (uintmax_t)ULLONG_MAX / (uintmax_t)(uint_t)base;
1147c478bd9Sstevel@tonic-gate 	uintmax_t val = 0;
1157c478bd9Sstevel@tonic-gate 	int c, i, neg = 0;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	switch (c = *s) {
1187c478bd9Sstevel@tonic-gate 	case '-':
1197c478bd9Sstevel@tonic-gate 		neg++;
1207c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
1217c478bd9Sstevel@tonic-gate 	case '+':
1227c478bd9Sstevel@tonic-gate 		c = *++s;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (c == '\0')
1267c478bd9Sstevel@tonic-gate 		goto done;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if ((val = CTOI(c)) >= base)
1297c478bd9Sstevel@tonic-gate 		yyerror("digit '%c' is invalid in current base\n", c);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	for (c = *++s; c != '\0'; c = *++s) {
1327c478bd9Sstevel@tonic-gate 		if (val > multmax)
1337c478bd9Sstevel@tonic-gate 			goto oflow;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		if ((i = CTOI(c)) >= base)
1367c478bd9Sstevel@tonic-gate 			yyerror("digit '%c' is invalid in current base\n", c);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		val *= base;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		if ((uintmax_t)ULLONG_MAX - val < (uintmax_t)i)
1417c478bd9Sstevel@tonic-gate 			goto oflow;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		val += i;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate done:
1467c478bd9Sstevel@tonic-gate 	return (neg ? -val : val);
1477c478bd9Sstevel@tonic-gate oflow:
1487c478bd9Sstevel@tonic-gate 	yyerror("specified value exceeds maximum immediate value\n");
1497c478bd9Sstevel@tonic-gate 	return ((uintmax_t)ULLONG_MAX);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Quick string to unsigned long conversion function.  This function performs
1547c478bd9Sstevel@tonic-gate  * no overflow checking and is only meant for internal mdb use.  It allows
1557c478bd9Sstevel@tonic-gate  * the caller to specify the length of the string in bytes and a base.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate ulong_t
strntoul(const char * s,size_t nbytes,int base)1587c478bd9Sstevel@tonic-gate strntoul(const char *s, size_t nbytes, int base)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	ulong_t n;
1617c478bd9Sstevel@tonic-gate 	int c;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	for (n = 0; nbytes != 0 && (c = *s) != '\0'; s++, nbytes--)
1647c478bd9Sstevel@tonic-gate 		n = n * base + CTOI(c);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (n);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Return a boolean value indicating whether or not a string consists
1717c478bd9Sstevel@tonic-gate  * solely of characters which are digits 0..9.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
strisnum(const char * s)1747c478bd9Sstevel@tonic-gate strisnum(const char *s)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	for (; *s != '\0'; s++) {
1777c478bd9Sstevel@tonic-gate 		if (*s < '0' || *s > '9')
1787c478bd9Sstevel@tonic-gate 			return (0);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return (1);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Return a boolean value indicating whether or not a string contains a
1867c478bd9Sstevel@tonic-gate  * number.  The number may be in the current radix, or it may have an
1877c478bd9Sstevel@tonic-gate  * explicit radix qualifier.  The number will be validated against the
1887c478bd9Sstevel@tonic-gate  * legal characters for the given radix.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate int
strisbasenum(const char * s)1917c478bd9Sstevel@tonic-gate strisbasenum(const char *s)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	char valid[] = "0123456789aAbBcCdDeEfF";
1947c478bd9Sstevel@tonic-gate 	int radix = mdb.m_radix;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (s[0] == '0') {
1977c478bd9Sstevel@tonic-gate 		switch (s[1]) {
1987c478bd9Sstevel@tonic-gate 		case 'I':
1997c478bd9Sstevel@tonic-gate 		case 'i':
2007c478bd9Sstevel@tonic-gate 			radix = 2;
2017c478bd9Sstevel@tonic-gate 			s += 2;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 		case 'O':
2047c478bd9Sstevel@tonic-gate 		case 'o':
2057c478bd9Sstevel@tonic-gate 			radix = 8;
2067c478bd9Sstevel@tonic-gate 			s += 2;
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 		case 'T':
2097c478bd9Sstevel@tonic-gate 		case 't':
2107c478bd9Sstevel@tonic-gate 			radix = 10;
2117c478bd9Sstevel@tonic-gate 			s += 2;
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 		case 'x':
2147c478bd9Sstevel@tonic-gate 		case 'X':
2157c478bd9Sstevel@tonic-gate 			radix = 16;
2167c478bd9Sstevel@tonic-gate 			s += 2;
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* limit `valid' to the digits valid for this base */
2227c478bd9Sstevel@tonic-gate 	valid[radix > 10 ? 10 + (radix - 10) * 2 : radix] = '\0';
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	do {
2257c478bd9Sstevel@tonic-gate 		if (!strchr(valid, *s))
2267c478bd9Sstevel@tonic-gate 			return (0);
2277c478bd9Sstevel@tonic-gate 	} while (*++s != '\0');
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (1);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Quick string to integer (base 10) conversion function.  This performs
2347c478bd9Sstevel@tonic-gate  * no overflow checking and is only meant for internal mdb use.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate int
strtoi(const char * s)2377c478bd9Sstevel@tonic-gate strtoi(const char *s)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	int c, n;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	for (n = 0; (c = *s) >= '0' && c <= '9'; s++)
2427c478bd9Sstevel@tonic-gate 		n = n * 10 + c - '0';
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	return (n);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Create a copy of string s using the mdb allocator interface.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate char *
strdup(const char * s)2517c478bd9Sstevel@tonic-gate strdup(const char *s)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	char *s1 = mdb_alloc(strlen(s) + 1, UM_SLEEP);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	(void) strcpy(s1, s);
2567c478bd9Sstevel@tonic-gate 	return (s1);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * Create a copy of string s, but only duplicate the first n bytes.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate char *
strndup(const char * s,size_t n)2637c478bd9Sstevel@tonic-gate strndup(const char *s, size_t n)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	char *s2 = mdb_alloc(n + 1, UM_SLEEP);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	(void) strncpy(s2, s, n);
2687c478bd9Sstevel@tonic-gate 	s2[n] = '\0';
2697c478bd9Sstevel@tonic-gate 	return (s2);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Convenience routine for freeing strings.
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate void
strfree(char * s)2767c478bd9Sstevel@tonic-gate strfree(char *s)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	mdb_free(s, strlen(s) + 1);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Transform string s inline, converting each embedded C escape sequence string
2837c478bd9Sstevel@tonic-gate  * to the corresponding character.  For example, the substring "\n" is replaced
2847c478bd9Sstevel@tonic-gate  * by an inline '\n' character.  The length of the resulting string is returned.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate size_t
stresc2chr(char * s)2877c478bd9Sstevel@tonic-gate stresc2chr(char *s)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	char *p, *q, c;
2907c478bd9Sstevel@tonic-gate 	int esc = 0;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	for (p = q = s; (c = *p) != '\0'; p++) {
2937c478bd9Sstevel@tonic-gate 		if (esc) {
2947c478bd9Sstevel@tonic-gate 			switch (c) {
2957c478bd9Sstevel@tonic-gate 				case '0':
2967c478bd9Sstevel@tonic-gate 				case '1':
2977c478bd9Sstevel@tonic-gate 				case '2':
2987c478bd9Sstevel@tonic-gate 				case '3':
2997c478bd9Sstevel@tonic-gate 				case '4':
3007c478bd9Sstevel@tonic-gate 				case '5':
3017c478bd9Sstevel@tonic-gate 				case '6':
3027c478bd9Sstevel@tonic-gate 				case '7':
3037c478bd9Sstevel@tonic-gate 					c -= '0';
3047c478bd9Sstevel@tonic-gate 					p++;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 					if (*p >= '0' && *p <= '7') {
3077c478bd9Sstevel@tonic-gate 						c = c * 8 + *p++ - '0';
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 						if (*p >= '0' && *p <= '7')
3107c478bd9Sstevel@tonic-gate 							c = c * 8 + *p - '0';
3117c478bd9Sstevel@tonic-gate 						else
3127c478bd9Sstevel@tonic-gate 							p--;
3137c478bd9Sstevel@tonic-gate 					} else
3147c478bd9Sstevel@tonic-gate 						p--;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 					*q++ = c;
3177c478bd9Sstevel@tonic-gate 					break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 				case 'a':
3207c478bd9Sstevel@tonic-gate 					*q++ = '\a';
3217c478bd9Sstevel@tonic-gate 					break;
3227c478bd9Sstevel@tonic-gate 				case 'b':
3237c478bd9Sstevel@tonic-gate 					*q++ = '\b';
3247c478bd9Sstevel@tonic-gate 					break;
3257c478bd9Sstevel@tonic-gate 				case 'f':
3267c478bd9Sstevel@tonic-gate 					*q++ = '\f';
3277c478bd9Sstevel@tonic-gate 					break;
3287c478bd9Sstevel@tonic-gate 				case 'n':
3297c478bd9Sstevel@tonic-gate 					*q++ = '\n';
3307c478bd9Sstevel@tonic-gate 					break;
3317c478bd9Sstevel@tonic-gate 				case 'r':
3327c478bd9Sstevel@tonic-gate 					*q++ = '\r';
3337c478bd9Sstevel@tonic-gate 					break;
3347c478bd9Sstevel@tonic-gate 				case 't':
3357c478bd9Sstevel@tonic-gate 					*q++ = '\t';
3367c478bd9Sstevel@tonic-gate 					break;
3377c478bd9Sstevel@tonic-gate 				case 'v':
3387c478bd9Sstevel@tonic-gate 					*q++ = '\v';
3397c478bd9Sstevel@tonic-gate 					break;
3407c478bd9Sstevel@tonic-gate 				case '"':
3417c478bd9Sstevel@tonic-gate 				case '\\':
3427c478bd9Sstevel@tonic-gate 					*q++ = c;
3437c478bd9Sstevel@tonic-gate 					break;
3447c478bd9Sstevel@tonic-gate 				default:
3457c478bd9Sstevel@tonic-gate 					*q++ = '\\';
3467c478bd9Sstevel@tonic-gate 					*q++ = c;
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 			esc = 0;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		} else {
3527c478bd9Sstevel@tonic-gate 			if ((esc = c == '\\') == 0)
3537c478bd9Sstevel@tonic-gate 				*q++ = c;
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	*q = '\0';
3587c478bd9Sstevel@tonic-gate 	return ((size_t)(q - s));
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate  * Create a copy of string s in which certain unprintable or special characters
3637c478bd9Sstevel@tonic-gate  * have been converted to the string representation of their C escape sequence.
3647c478bd9Sstevel@tonic-gate  * For example, the newline character is expanded to the string "\n".
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate char *
strchr2esc(const char * s,size_t n)3677c478bd9Sstevel@tonic-gate strchr2esc(const char *s, size_t n)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	const char *p;
3707c478bd9Sstevel@tonic-gate 	char *q, *s2, c;
3717c478bd9Sstevel@tonic-gate 	size_t addl = 0;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	for (p = s; p < s + n; p++) {
3747c478bd9Sstevel@tonic-gate 		switch (c = *p) {
3757c478bd9Sstevel@tonic-gate 		case '\0':
3767c478bd9Sstevel@tonic-gate 		case '\a':
3777c478bd9Sstevel@tonic-gate 		case '\b':
3787c478bd9Sstevel@tonic-gate 		case '\f':
3797c478bd9Sstevel@tonic-gate 		case '\n':
3807c478bd9Sstevel@tonic-gate 		case '\r':
3817c478bd9Sstevel@tonic-gate 		case '\t':
3827c478bd9Sstevel@tonic-gate 		case '\v':
3837c478bd9Sstevel@tonic-gate 		case '"':
3847c478bd9Sstevel@tonic-gate 		case '\\':
3857c478bd9Sstevel@tonic-gate 			addl++;		/* 1 add'l char needed to follow \ */
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		case ' ':
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 		default:
3907c478bd9Sstevel@tonic-gate 			if (c < '!' || c > '~')
3917c478bd9Sstevel@tonic-gate 				addl += 3; /* 3 add'l chars following \ */
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	s2 = mdb_alloc(n + addl + 1, UM_SLEEP);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	for (p = s, q = s2; p < s + n; p++) {
3987c478bd9Sstevel@tonic-gate 		switch (c = *p) {
3997c478bd9Sstevel@tonic-gate 		case '\0':
4007c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4017c478bd9Sstevel@tonic-gate 			*q++ = '0';
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		case '\a':
4047c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4057c478bd9Sstevel@tonic-gate 			*q++ = 'a';
4067c478bd9Sstevel@tonic-gate 			break;
4077c478bd9Sstevel@tonic-gate 		case '\b':
4087c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4097c478bd9Sstevel@tonic-gate 			*q++ = 'b';
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 		case '\f':
4127c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4137c478bd9Sstevel@tonic-gate 			*q++ = 'f';
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 		case '\n':
4167c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4177c478bd9Sstevel@tonic-gate 			*q++ = 'n';
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		case '\r':
4207c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4217c478bd9Sstevel@tonic-gate 			*q++ = 'r';
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 		case '\t':
4247c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4257c478bd9Sstevel@tonic-gate 			*q++ = 't';
4267c478bd9Sstevel@tonic-gate 			break;
4277c478bd9Sstevel@tonic-gate 		case '\v':
4287c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4297c478bd9Sstevel@tonic-gate 			*q++ = 'v';
4307c478bd9Sstevel@tonic-gate 			break;
4317c478bd9Sstevel@tonic-gate 		case '"':
4327c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4337c478bd9Sstevel@tonic-gate 			*q++ = '"';
4347c478bd9Sstevel@tonic-gate 			break;
4357c478bd9Sstevel@tonic-gate 		case '\\':
4367c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4377c478bd9Sstevel@tonic-gate 			*q++ = '\\';
4387c478bd9Sstevel@tonic-gate 			break;
4397c478bd9Sstevel@tonic-gate 		case ' ':
4407c478bd9Sstevel@tonic-gate 			*q++ = c;
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 		default:
4437c478bd9Sstevel@tonic-gate 			if (c < '!' || c > '~') {
4447c478bd9Sstevel@tonic-gate 				*q++ = '\\';
4457c478bd9Sstevel@tonic-gate 				*q++ = ((c >> 6) & 3) + '0';
4467c478bd9Sstevel@tonic-gate 				*q++ = ((c >> 3) & 7) + '0';
4477c478bd9Sstevel@tonic-gate 				*q++ = (c & 7) + '0';
4487c478bd9Sstevel@tonic-gate 			} else
4497c478bd9Sstevel@tonic-gate 				*q++ = c;
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	*q = '\0';
4547c478bd9Sstevel@tonic-gate 	return (s2);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Create a copy of string s in which certain unprintable or special characters
4597c478bd9Sstevel@tonic-gate  * have been converted to an odd representation of their escape sequence.
4607c478bd9Sstevel@tonic-gate  * This algorithm is the old adb convention for representing such sequences.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate char *
strchr2adb(const char * s,size_t n)4637c478bd9Sstevel@tonic-gate strchr2adb(const char *s, size_t n)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	size_t addl = 0;
4667c478bd9Sstevel@tonic-gate 	const char *p;
4677c478bd9Sstevel@tonic-gate 	char *q, *s2;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	for (p = s; p < s + n; p++) {
4707c478bd9Sstevel@tonic-gate 		char c = *p & CHAR_MAX;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		if (c < ' ' || c == CHAR_MAX)
4737c478bd9Sstevel@tonic-gate 			addl++; /* 1 add'l char needed for "^" */
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	s2 = mdb_alloc(n + addl + 1, UM_SLEEP);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	for (p = s, q = s2; p < s + n; p++) {
4797c478bd9Sstevel@tonic-gate 		char c = *p & CHAR_MAX;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		if (c == CHAR_MAX) {
4827c478bd9Sstevel@tonic-gate 			*q++ = '^';
4837c478bd9Sstevel@tonic-gate 			*q++ = '?';
4847c478bd9Sstevel@tonic-gate 		} else if (c < ' ') {
4857c478bd9Sstevel@tonic-gate 			*q++ = '^';
4867c478bd9Sstevel@tonic-gate 			*q++ = c + '@';
4877c478bd9Sstevel@tonic-gate 		} else
4887c478bd9Sstevel@tonic-gate 			*q++ = c;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	*q = '\0';
4927c478bd9Sstevel@tonic-gate 	return (s2);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * Same as strchr, but we only search the first n characters
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate char *
strnchr(const char * s,int c,size_t n)4997c478bd9Sstevel@tonic-gate strnchr(const char *s, int c, size_t n)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	int i = 0;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
5047c478bd9Sstevel@tonic-gate 		if (*(s + i) == (char)c)
5057c478bd9Sstevel@tonic-gate 			return ((char *)(s + i));
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (NULL);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * Split the string s at the first occurrence of character c.  This character
5137c478bd9Sstevel@tonic-gate  * is replaced by \0, and a pointer to the remainder of the string is returned.
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate char *
strsplit(char * s,char c)5167c478bd9Sstevel@tonic-gate strsplit(char *s, char c)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	char *p;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if ((p = strchr(s, c)) == NULL)
5217c478bd9Sstevel@tonic-gate 		return (NULL);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	*p++ = '\0';
5247c478bd9Sstevel@tonic-gate 	return (p);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * Same as strsplit, but split from the last occurrence of character c.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate char *
strrsplit(char * s,char c)5317c478bd9Sstevel@tonic-gate strrsplit(char *s, char c)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	char *p;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if ((p = strrchr(s, c)) == NULL)
5367c478bd9Sstevel@tonic-gate 		return (NULL);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	*p++ = '\0';
5397c478bd9Sstevel@tonic-gate 	return (p);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * Return the address of the first occurrence of any character from s2
5447c478bd9Sstevel@tonic-gate  * in the string s1, or NULL if none exists.  This is similar to libc's
5457c478bd9Sstevel@tonic-gate  * strpbrk, but we add a third parameter to limit the search to the
5467c478bd9Sstevel@tonic-gate  * specified number of bytes in s1, or a \0 character, whichever is
5477c478bd9Sstevel@tonic-gate  * encountered first.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate const char *
strnpbrk(const char * s1,const char * s2,size_t nbytes)5507c478bd9Sstevel@tonic-gate strnpbrk(const char *s1, const char *s2, size_t nbytes)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	const char *p;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (nbytes == 0)
5557c478bd9Sstevel@tonic-gate 		return (NULL);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	do {
5587c478bd9Sstevel@tonic-gate 		for (p = s2; *p != '\0' && *p != *s1; p++)
5597c478bd9Sstevel@tonic-gate 			continue;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		if (*p != '\0')
5627c478bd9Sstevel@tonic-gate 			return (s1);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	} while (--nbytes != 0 && *s1++ != '\0');
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	return (NULL);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * Abbreviate a string if it meets or exceeds the specified length, including
5717c478bd9Sstevel@tonic-gate  * the terminating null character.  The string is abbreviated by replacing the
5727c478bd9Sstevel@tonic-gate  * last four characters with " ...".  strabbr is useful in constructs such as
5737c478bd9Sstevel@tonic-gate  * this one, where nbytes = sizeof (buf):
5747c478bd9Sstevel@tonic-gate  *
5757c478bd9Sstevel@tonic-gate  * if (mdb_snprintf(buf, nbytes, "%s %d %c", ...) >= nbytes)
5767c478bd9Sstevel@tonic-gate  *         (void) strabbr(buf, nbytes);
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * No modifications are made if nbytes is too small to hold the suffix itself.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate char *
strabbr(char * s,size_t nbytes)5817c478bd9Sstevel@tonic-gate strabbr(char *s, size_t nbytes)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	static const char suffix[] = " ...";
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (nbytes > sizeof (suffix) && strlen(s) >= nbytes - 1)
5867c478bd9Sstevel@tonic-gate 		(void) strcpy(&s[nbytes - sizeof (suffix)], suffix);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	return (s);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * Return the basename (name after final /) of the given string.  We use
5937c478bd9Sstevel@tonic-gate  * strbasename rather than basename to avoid conflicting with libgen.h's
5947c478bd9Sstevel@tonic-gate  * non-const function prototype.
5957c478bd9Sstevel@tonic-gate  */
5967c478bd9Sstevel@tonic-gate const char *
strbasename(const char * s)5977c478bd9Sstevel@tonic-gate strbasename(const char *s)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate 	const char *p = strrchr(s, '/');
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (p == NULL)
6027c478bd9Sstevel@tonic-gate 		return (s);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	return (++p);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Return the directory name (name prior to the final /) of the given string.
6097c478bd9Sstevel@tonic-gate  * The string itself is modified.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate char *
strdirname(char * s)6127c478bd9Sstevel@tonic-gate strdirname(char *s)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	static char slash[] = "/";
6157c478bd9Sstevel@tonic-gate 	static char dot[] = ".";
6167c478bd9Sstevel@tonic-gate 	char *p;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (s == NULL || *s == '\0')
6197c478bd9Sstevel@tonic-gate 		return (dot);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	for (p = s + strlen(s); p != s && *--p == '/'; )
6227c478bd9Sstevel@tonic-gate 		continue;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (p == s && *p == '/')
6257c478bd9Sstevel@tonic-gate 		return (slash);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	while (p != s) {
6287c478bd9Sstevel@tonic-gate 		if (*--p == '/') {
6297c478bd9Sstevel@tonic-gate 			while (*p == '/' && p != s)
6307c478bd9Sstevel@tonic-gate 				p--;
6317c478bd9Sstevel@tonic-gate 			*++p = '\0';
6327c478bd9Sstevel@tonic-gate 			return (s);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	return (dot);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Return a pointer to the first character in the string that makes it an
6417c478bd9Sstevel@tonic-gate  * invalid identifer (i.e. incompatible with the mdb syntax), or NULL if
6427c478bd9Sstevel@tonic-gate  * the string is a valid identifier.
6437c478bd9Sstevel@tonic-gate  */
6447c478bd9Sstevel@tonic-gate const char *
strbadid(const char * s)6457c478bd9Sstevel@tonic-gate strbadid(const char *s)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	return (strpbrk(s, "#%^&*-+=,:$/\\?<>;|!`'\"[]\n\t() {}"));
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * Return a boolean value indicating if the given string consists solely
6527c478bd9Sstevel@tonic-gate  * of printable ASCII characters terminated by \0.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate int
strisprint(const char * s)6557c478bd9Sstevel@tonic-gate strisprint(const char *s)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	for (; *s != '\0'; s++) {
6587c478bd9Sstevel@tonic-gate 		if (*s < ' ' || *s > '~')
6597c478bd9Sstevel@tonic-gate 			return (0);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	return (1);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * This is a near direct copy of the inet_ntop() code in
6677c478bd9Sstevel@tonic-gate  * uts/common/inet/ip/ipv6.c, duplicated here for kmdb's sake.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate static void
convert2ascii(char * buf,const in6_addr_t * addr)6707c478bd9Sstevel@tonic-gate convert2ascii(char *buf, const in6_addr_t *addr)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	int		hexdigits;
6737c478bd9Sstevel@tonic-gate 	int		head_zero = 0;
6747c478bd9Sstevel@tonic-gate 	int		tail_zero = 0;
6757c478bd9Sstevel@tonic-gate 	/* tempbuf must be big enough to hold ffff:\0 */
6767c478bd9Sstevel@tonic-gate 	char		tempbuf[6];
6777c478bd9Sstevel@tonic-gate 	char		*ptr;
6787c478bd9Sstevel@tonic-gate 	uint16_t	*addr_component, host_component;
6797c478bd9Sstevel@tonic-gate 	size_t		len;
6807c478bd9Sstevel@tonic-gate 	int		first = FALSE;
6817c478bd9Sstevel@tonic-gate 	int		med_zero = FALSE;
6827c478bd9Sstevel@tonic-gate 	int		end_zero = FALSE;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	addr_component = (uint16_t *)addr;
6857c478bd9Sstevel@tonic-gate 	ptr = buf;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* First count if trailing zeroes higher in number */
6887c478bd9Sstevel@tonic-gate 	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
6897c478bd9Sstevel@tonic-gate 		if (*addr_component == 0) {
6907c478bd9Sstevel@tonic-gate 			if (hexdigits < 4)
6917c478bd9Sstevel@tonic-gate 				head_zero++;
6927c478bd9Sstevel@tonic-gate 			else
6937c478bd9Sstevel@tonic-gate 				tail_zero++;
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 		addr_component++;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	addr_component = (uint16_t *)addr;
6987c478bd9Sstevel@tonic-gate 	if (tail_zero > head_zero && (head_zero + tail_zero) != 7)
6997c478bd9Sstevel@tonic-gate 		end_zero = TRUE;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
7027c478bd9Sstevel@tonic-gate 		/* if entry is a 0 */
7037c478bd9Sstevel@tonic-gate 		if (*addr_component == 0) {
7047c478bd9Sstevel@tonic-gate 			if (!first && *(addr_component + 1) == 0) {
7057c478bd9Sstevel@tonic-gate 				if (end_zero && (hexdigits < 4)) {
7067c478bd9Sstevel@tonic-gate 					*ptr++ = '0';
7077c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
7087c478bd9Sstevel@tonic-gate 				} else {
7097c478bd9Sstevel@tonic-gate 					if (hexdigits == 0)
7107c478bd9Sstevel@tonic-gate 						*ptr++ = ':';
7117c478bd9Sstevel@tonic-gate 					/* add another */
7127c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
7137c478bd9Sstevel@tonic-gate 					first = TRUE;
7147c478bd9Sstevel@tonic-gate 					med_zero = TRUE;
7157c478bd9Sstevel@tonic-gate 				}
7167c478bd9Sstevel@tonic-gate 			} else if (first && med_zero) {
7177c478bd9Sstevel@tonic-gate 				if (hexdigits == 7)
7187c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
7197c478bd9Sstevel@tonic-gate 				addr_component++;
7207c478bd9Sstevel@tonic-gate 				continue;
7217c478bd9Sstevel@tonic-gate 			} else {
7227c478bd9Sstevel@tonic-gate 				*ptr++ = '0';
7237c478bd9Sstevel@tonic-gate 				*ptr++ = ':';
7247c478bd9Sstevel@tonic-gate 			}
7257c478bd9Sstevel@tonic-gate 			addr_component++;
7267c478bd9Sstevel@tonic-gate 			continue;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		if (med_zero)
7297c478bd9Sstevel@tonic-gate 			med_zero = FALSE;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 		tempbuf[0] = '\0';
7327c478bd9Sstevel@tonic-gate 		mdb_nhconvert(&host_component, addr_component,
7337c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t));
7347c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(tempbuf, sizeof (tempbuf), "%x:",
7357c478bd9Sstevel@tonic-gate 		    host_component & 0xffff);
7367c478bd9Sstevel@tonic-gate 		len = strlen(tempbuf);
7377c478bd9Sstevel@tonic-gate 		bcopy(tempbuf, ptr, len);
7387c478bd9Sstevel@tonic-gate 		ptr = ptr + len;
7397c478bd9Sstevel@tonic-gate 		addr_component++;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	*--ptr = '\0';
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate char *
mdb_inet_ntop(int af,const void * addr,char * buf,size_t buflen)7457c478bd9Sstevel@tonic-gate mdb_inet_ntop(int af, const void *addr, char *buf, size_t buflen)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	in6_addr_t	*v6addr;
7487c478bd9Sstevel@tonic-gate 	uchar_t		*v4addr;
7497c478bd9Sstevel@tonic-gate 	char		*caddr;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate #define	UC(b)	(((int)b) & 0xff)
7527c478bd9Sstevel@tonic-gate 	switch (af) {
7537c478bd9Sstevel@tonic-gate 	case AF_INET:
7547c478bd9Sstevel@tonic-gate 		ASSERT(buflen >= INET_ADDRSTRLEN);
7557c478bd9Sstevel@tonic-gate 		v4addr = (uchar_t *)addr;
7567c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(buf, buflen, "%d.%d.%d.%d",
7577c478bd9Sstevel@tonic-gate 		    UC(v4addr[0]), UC(v4addr[1]), UC(v4addr[2]), UC(v4addr[3]));
7587c478bd9Sstevel@tonic-gate 		return (buf);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	case AF_INET6:
7617c478bd9Sstevel@tonic-gate 		ASSERT(buflen >= INET6_ADDRSTRLEN);
7627c478bd9Sstevel@tonic-gate 		v6addr = (in6_addr_t *)addr;
7637c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(v6addr)) {
7647c478bd9Sstevel@tonic-gate 			caddr = (char *)addr;
7657c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(buf, buflen, "::ffff:%d.%d.%d.%d",
7667c478bd9Sstevel@tonic-gate 			    UC(caddr[12]), UC(caddr[13]),
7677c478bd9Sstevel@tonic-gate 			    UC(caddr[14]), UC(caddr[15]));
7687c478bd9Sstevel@tonic-gate 		} else if (IN6_IS_ADDR_V4COMPAT(v6addr)) {
7697c478bd9Sstevel@tonic-gate 			caddr = (char *)addr;
7707c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(buf, buflen, "::%d.%d.%d.%d",
7717c478bd9Sstevel@tonic-gate 			    UC(caddr[12]), UC(caddr[13]), UC(caddr[14]),
7727c478bd9Sstevel@tonic-gate 			    UC(caddr[15]));
7737c478bd9Sstevel@tonic-gate 		} else if (IN6_IS_ADDR_UNSPECIFIED(v6addr)) {
7747c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(buf, buflen, "::");
7757c478bd9Sstevel@tonic-gate 		} else {
7767c478bd9Sstevel@tonic-gate 			convert2ascii(buf, v6addr);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 		return (buf);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate #undef UC
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	return (NULL);
7837c478bd9Sstevel@tonic-gate }
784