xref: /titanic_52/usr/src/cmd/prstat/prutil.c (revision 0a1278f26ea4b7c8c0285d4f2d6c5b680904aa01)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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 /*
22*0a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
23*0a1278f2SGary Mills  *
244944376cSJohn Levon  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267166d658SMenno Lageman  *
277166d658SMenno Lageman  * Portions Copyright 2009 Chad Mynhier
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/resource.h>
337c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
347c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h>
367c478bd9Sstevel@tonic-gate #include <zone.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <wchar.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
46004388ebScasper #include <stdio_ext.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <poll.h>
507c478bd9Sstevel@tonic-gate #include <project.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include "prfile.h"
537c478bd9Sstevel@tonic-gate #include "prstat.h"
547c478bd9Sstevel@tonic-gate #include "prutil.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static char PRG_FMT[] = "%s: ";
577c478bd9Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n";
587c478bd9Sstevel@tonic-gate static char *progname;
597c478bd9Sstevel@tonic-gate static char projbuf[PROJECT_BUFSZ];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	RLIMIT_NOFILE_MAX	32767
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
647c478bd9Sstevel@tonic-gate void
657c478bd9Sstevel@tonic-gate Warn(char *format, ...)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int err = errno;
687c478bd9Sstevel@tonic-gate 	va_list alist;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (progname != NULL)
717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
727c478bd9Sstevel@tonic-gate 	va_start(alist, format);
737c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
747c478bd9Sstevel@tonic-gate 	va_end(alist);
757c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
807c478bd9Sstevel@tonic-gate void
817c478bd9Sstevel@tonic-gate Die(char *format, ...)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	int err = errno;
847c478bd9Sstevel@tonic-gate 	va_list alist;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (progname != NULL)
877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
887c478bd9Sstevel@tonic-gate 	va_start(alist, format);
897c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
907c478bd9Sstevel@tonic-gate 	va_end(alist);
917c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
937c478bd9Sstevel@tonic-gate 	exit(1);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate void
977c478bd9Sstevel@tonic-gate Progname(char *arg0)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	char *p = strrchr(arg0, '/');
1007c478bd9Sstevel@tonic-gate 	if (p == NULL)
1017c478bd9Sstevel@tonic-gate 		p = arg0;
1027c478bd9Sstevel@tonic-gate 	else
1037c478bd9Sstevel@tonic-gate 		p++;
1047c478bd9Sstevel@tonic-gate 	progname = p;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate void
1087c478bd9Sstevel@tonic-gate Usage()
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
111*0a1278f2SGary Mills 	    "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
112c6402783Sakolb 	    "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
1137c478bd9Sstevel@tonic-gate 	    "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
1144944376cSJohn Levon 	    "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
1157c478bd9Sstevel@tonic-gate 	    "\t[interval [counter]]\n"));
1167c478bd9Sstevel@tonic-gate 	exit(1);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int
1207c478bd9Sstevel@tonic-gate Atoi(char *p)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int i;
1237c478bd9Sstevel@tonic-gate 	char *q;
1247c478bd9Sstevel@tonic-gate 	errno = 0;
1257c478bd9Sstevel@tonic-gate 	i = (int)strtol(p, &q, 10);
1267c478bd9Sstevel@tonic-gate 	if (errno != 0 || q == p || i < 0 || *q != '\0')
1277c478bd9Sstevel@tonic-gate 		Die(gettext("illegal argument -- %s\n"), p);
1287c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1297c478bd9Sstevel@tonic-gate 	else
1307c478bd9Sstevel@tonic-gate 		return (i);
1317c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate void
1357c478bd9Sstevel@tonic-gate Format_size(char *str, size_t size, int length)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	char tag = 'K';
1387c478bd9Sstevel@tonic-gate 	if (size >= 10000) {
1397c478bd9Sstevel@tonic-gate 		size = (size + 512) / 1024;
1407c478bd9Sstevel@tonic-gate 		tag = 'M';
1417c478bd9Sstevel@tonic-gate 		if (size >= 10000) {
1427c478bd9Sstevel@tonic-gate 			size = (size + 512) / 1024;
1437c478bd9Sstevel@tonic-gate 			tag = 'G';
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	(void) snprintf(str, length, "%4d%c", (int)size, tag);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate void
1507c478bd9Sstevel@tonic-gate Format_time(char *str, ulong_t time, int length)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	(void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
1537c478bd9Sstevel@tonic-gate 	    (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate void
1577c478bd9Sstevel@tonic-gate Format_pct(char *str, float val, int length)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	if (val > (float)100)
1607c478bd9Sstevel@tonic-gate 		val = 100;
1617c478bd9Sstevel@tonic-gate 	if (val < 0)
1627c478bd9Sstevel@tonic-gate 		val = 0;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (val < (float)9.95)
1657c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "%1.1f", val);
1667c478bd9Sstevel@tonic-gate 	else
1677c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "%.0f", val);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate void
1717c478bd9Sstevel@tonic-gate Format_num(char *str, int num, int length)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	if (num >= 100000) {
1747c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, ".%1dM", num/100000);
1757c478bd9Sstevel@tonic-gate 	} else {
1767c478bd9Sstevel@tonic-gate 		if (num >= 1000)
1777c478bd9Sstevel@tonic-gate 			(void) snprintf(str, length, "%2dK", num/1000);
1787c478bd9Sstevel@tonic-gate 		else
1797c478bd9Sstevel@tonic-gate 			(void) snprintf(str, length, "%3d", num);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate void
1847c478bd9Sstevel@tonic-gate Format_state(char *str, char state, processorid_t pr_id, int length)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	switch (state) {
1877c478bd9Sstevel@tonic-gate 	case 'S':
1887c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "sleep", length);
1897c478bd9Sstevel@tonic-gate 		break;
1907c478bd9Sstevel@tonic-gate 	case 'R':
1917c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "run", length);
1927c478bd9Sstevel@tonic-gate 		break;
1937c478bd9Sstevel@tonic-gate 	case 'Z':
1947c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "zombie", length);
1957c478bd9Sstevel@tonic-gate 		break;
1967c478bd9Sstevel@tonic-gate 	case 'T':
1977c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "stop", length);
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	case 'I':
2007c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "idle", length);
2017c478bd9Sstevel@tonic-gate 		break;
202c97ad5cdSakolb 	case 'W':
203c97ad5cdSakolb 		(void) strncpy(str, "wait", length);
2047c478bd9Sstevel@tonic-gate 		break;
2057c478bd9Sstevel@tonic-gate 	case 'O':
2067c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "cpu%-3d", (int)pr_id);
2077c478bd9Sstevel@tonic-gate 		break;
2087c478bd9Sstevel@tonic-gate 	default:
2097c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "?", length);
2107c478bd9Sstevel@tonic-gate 		break;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate void *
2157c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	int	cnt = 0;
2187c478bd9Sstevel@tonic-gate 	void	*sav = ptr;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate eagain:	if ((ptr = realloc(ptr, size)))
2217c478bd9Sstevel@tonic-gate 		return (ptr);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if ((++cnt <= 3) && (errno == EAGAIN)) {
2247c478bd9Sstevel@tonic-gate 		Warn(gettext("realloc() failed, attempt %d"), cnt);
2257c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 5000); /* wait for 5 seconds */
2267c478bd9Sstevel@tonic-gate 		ptr = sav;
2277c478bd9Sstevel@tonic-gate 		goto eagain;
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 	ptr = sav;
2307c478bd9Sstevel@tonic-gate 	Die(gettext("not enough memory"));
2317c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2327c478bd9Sstevel@tonic-gate 	return (NULL);	/* keep gcc happy */
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate void *
2367c478bd9Sstevel@tonic-gate Malloc(size_t size)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	return (Realloc(NULL, size));
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate void *
2427c478bd9Sstevel@tonic-gate Zalloc(size_t size)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	return (memset(Realloc(NULL, size), 0, size));
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate int
2487c478bd9Sstevel@tonic-gate Setrlimit()
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
2517c478bd9Sstevel@tonic-gate 	int fd_limit;
2527c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
2537c478bd9Sstevel@tonic-gate 		Die(gettext("getrlimit failed"));
2547c478bd9Sstevel@tonic-gate 	fd_limit = rlim.rlim_cur;
2557c478bd9Sstevel@tonic-gate 	rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
2567c478bd9Sstevel@tonic-gate 	rlim.rlim_cur = rlim.rlim_max;
257004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
2587c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
2597c478bd9Sstevel@tonic-gate 		return (fd_limit);
2607c478bd9Sstevel@tonic-gate 	else
2617c478bd9Sstevel@tonic-gate 		return (rlim.rlim_cur);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate void
2657c478bd9Sstevel@tonic-gate Priocntl(char *class)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	pcinfo_t pcinfo;
2687c478bd9Sstevel@tonic-gate 	pcparms_t pcparms;
2697c478bd9Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, class);
2707c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
2717c478bd9Sstevel@tonic-gate 		Warn(gettext("cannot get real time class parameters"));
2727c478bd9Sstevel@tonic-gate 		return;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	pcparms.pc_cid = pcinfo.pc_cid;
2757c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
2767c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
2777c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
2787c478bd9Sstevel@tonic-gate 	if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
2797c478bd9Sstevel@tonic-gate 		Warn(gettext("cannot enter the real time class"));
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate void
283*0a1278f2SGary Mills getprojname(projid_t projid, char *str, size_t len, int noresolve,
284*0a1278f2SGary Mills     int trunc, size_t width)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	struct project proj;
287*0a1278f2SGary Mills 	size_t n;
2887c478bd9Sstevel@tonic-gate 
2897166d658SMenno Lageman 	if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) ==
290*0a1278f2SGary Mills 	    NULL) {
2917c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-6d", (int)projid);
292*0a1278f2SGary Mills 	} else {
293*0a1278f2SGary Mills 		n = mbstowcs(NULL, proj.pj_name, 0);
294*0a1278f2SGary Mills 		if (n == (size_t)-1)
295*0a1278f2SGary Mills 			(void) snprintf(str, len, "%-28s", "ERROR");
296*0a1278f2SGary Mills 		else if (trunc && n > width)
297*0a1278f2SGary Mills 			(void) snprintf(str, len, "%.*s%c", width - 1,
298*0a1278f2SGary Mills 			    proj.pj_name, '*');
2997166d658SMenno Lageman 		else
3007166d658SMenno Lageman 			(void) snprintf(str, len, "%-28s", proj.pj_name);
3017c478bd9Sstevel@tonic-gate 	}
302*0a1278f2SGary Mills }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate void
305*0a1278f2SGary Mills getzonename(zoneid_t zoneid, char *str, size_t len, int trunc, size_t width)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	char zone_name[ZONENAME_MAX];
308*0a1278f2SGary Mills 	size_t n;
3097c478bd9Sstevel@tonic-gate 
310*0a1278f2SGary Mills 	if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0) {
3117c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-6d", (int)zoneid);
312*0a1278f2SGary Mills 	} else {
313*0a1278f2SGary Mills 		n = mbstowcs(NULL, zone_name, 0);
314*0a1278f2SGary Mills 		if (n == (size_t)-1)
315*0a1278f2SGary Mills 			(void) snprintf(str, len, "%-28s", "ERROR");
316*0a1278f2SGary Mills 		else if (trunc && n > width)
317*0a1278f2SGary Mills 			(void) snprintf(str, len, "%.*s%c", width - 1,
318*0a1278f2SGary Mills 			    zone_name, '*');
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 			(void) snprintf(str, len, "%-28s", zone_name);
3217c478bd9Sstevel@tonic-gate 	}
322*0a1278f2SGary Mills }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * Remove all unprintable characters from process name
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate void
3287c478bd9Sstevel@tonic-gate stripfname(char *buf)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	int bytesleft = PRFNSZ;
3317c478bd9Sstevel@tonic-gate 	wchar_t wchar;
3327c478bd9Sstevel@tonic-gate 	int length;
3337c478bd9Sstevel@tonic-gate 	char *cp;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	buf[bytesleft - 1] = '\0';
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	for (cp = buf; *cp != '\0'; cp += length) {
3387c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
3397c478bd9Sstevel@tonic-gate 		if (length <= 0) {
3407c478bd9Sstevel@tonic-gate 			*cp = '\0';
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 		if (!iswprint(wchar)) {
3447c478bd9Sstevel@tonic-gate 			if (bytesleft <= length) {
3457c478bd9Sstevel@tonic-gate 				*cp = '\0';
3467c478bd9Sstevel@tonic-gate 				break;
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			(void) memmove(cp, cp + length, bytesleft - length);
3497c478bd9Sstevel@tonic-gate 			length = 0;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 		bytesleft -= length;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate }
354