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