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 /*
220a1278f2SGary Mills * Copyright (c) 2013 Gary Mills
230a1278f2SGary 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
28*ab618543SJohn Levon * Copyright 2018 Joyent, Inc. All rights reserved.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/resource.h>
347c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h>
367c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h>
377c478bd9Sstevel@tonic-gate #include <zone.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #include <limits.h>
417c478bd9Sstevel@tonic-gate #include <wchar.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <stdarg.h>
467c478bd9Sstevel@tonic-gate #include <stdio.h>
47004388ebScasper #include <stdio_ext.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <poll.h>
517c478bd9Sstevel@tonic-gate #include <project.h>
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #include "prfile.h"
547c478bd9Sstevel@tonic-gate #include "prstat.h"
557c478bd9Sstevel@tonic-gate #include "prutil.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate static char PRG_FMT[] = "%s: ";
587c478bd9Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n";
597c478bd9Sstevel@tonic-gate static char *progname;
607c478bd9Sstevel@tonic-gate static char projbuf[PROJECT_BUFSZ];
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #define RLIMIT_NOFILE_MAX 32767
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
657c478bd9Sstevel@tonic-gate void
Warn(char * format,...)667c478bd9Sstevel@tonic-gate Warn(char *format, ...)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate int err = errno;
697c478bd9Sstevel@tonic-gate va_list alist;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate if (progname != NULL)
727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PRG_FMT, progname);
737c478bd9Sstevel@tonic-gate va_start(alist, format);
747c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
757c478bd9Sstevel@tonic-gate va_end(alist);
767c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
817c478bd9Sstevel@tonic-gate void
Die(char * format,...)827c478bd9Sstevel@tonic-gate Die(char *format, ...)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate int err = errno;
857c478bd9Sstevel@tonic-gate va_list alist;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (progname != NULL)
887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PRG_FMT, progname);
897c478bd9Sstevel@tonic-gate va_start(alist, format);
907c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
917c478bd9Sstevel@tonic-gate va_end(alist);
927c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
947c478bd9Sstevel@tonic-gate exit(1);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate void
Progname(char * arg0)987c478bd9Sstevel@tonic-gate Progname(char *arg0)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate char *p = strrchr(arg0, '/');
1017c478bd9Sstevel@tonic-gate if (p == NULL)
1027c478bd9Sstevel@tonic-gate p = arg0;
1037c478bd9Sstevel@tonic-gate else
1047c478bd9Sstevel@tonic-gate p++;
1057c478bd9Sstevel@tonic-gate progname = p;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate void
Usage()1097c478bd9Sstevel@tonic-gate Usage()
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1120a1278f2SGary Mills "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
113c6402783Sakolb "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
1147c478bd9Sstevel@tonic-gate "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
1154944376cSJohn Levon "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
1167c478bd9Sstevel@tonic-gate "\t[interval [counter]]\n"));
1177c478bd9Sstevel@tonic-gate exit(1);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate int
Atoi(char * p)1217c478bd9Sstevel@tonic-gate Atoi(char *p)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate int i;
1247c478bd9Sstevel@tonic-gate char *q;
1257c478bd9Sstevel@tonic-gate errno = 0;
1267c478bd9Sstevel@tonic-gate i = (int)strtol(p, &q, 10);
1277c478bd9Sstevel@tonic-gate if (errno != 0 || q == p || i < 0 || *q != '\0')
1287c478bd9Sstevel@tonic-gate Die(gettext("illegal argument -- %s\n"), p);
1297c478bd9Sstevel@tonic-gate /*NOTREACHED*/
1307c478bd9Sstevel@tonic-gate else
1317c478bd9Sstevel@tonic-gate return (i);
1327c478bd9Sstevel@tonic-gate return (0); /* keep gcc happy */
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate void
Format_size(char * str,size_t size,int length)1367c478bd9Sstevel@tonic-gate Format_size(char *str, size_t size, int length)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate char tag = 'K';
1397c478bd9Sstevel@tonic-gate if (size >= 10000) {
1407c478bd9Sstevel@tonic-gate size = (size + 512) / 1024;
1417c478bd9Sstevel@tonic-gate tag = 'M';
1427c478bd9Sstevel@tonic-gate if (size >= 10000) {
1437c478bd9Sstevel@tonic-gate size = (size + 512) / 1024;
1447c478bd9Sstevel@tonic-gate tag = 'G';
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%4d%c", (int)size, tag);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate void
Format_time(char * str,ulong_t time,int length)1517c478bd9Sstevel@tonic-gate Format_time(char *str, ulong_t time, int length)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate (void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
1547c478bd9Sstevel@tonic-gate (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate void
Format_pct(char * str,float val,int length)1587c478bd9Sstevel@tonic-gate Format_pct(char *str, float val, int length)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate if (val > (float)100)
1617c478bd9Sstevel@tonic-gate val = 100;
1627c478bd9Sstevel@tonic-gate if (val < 0)
1637c478bd9Sstevel@tonic-gate val = 0;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (val < (float)9.95)
1667c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%1.1f", val);
1677c478bd9Sstevel@tonic-gate else
1687c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%.0f", val);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate void
Format_num(char * str,int num,int length)1727c478bd9Sstevel@tonic-gate Format_num(char *str, int num, int length)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate if (num >= 100000) {
1757c478bd9Sstevel@tonic-gate (void) snprintf(str, length, ".%1dM", num/100000);
1767c478bd9Sstevel@tonic-gate } else {
1777c478bd9Sstevel@tonic-gate if (num >= 1000)
1787c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%2dK", num/1000);
1797c478bd9Sstevel@tonic-gate else
1807c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%3d", num);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate void
Format_state(char * str,char state,processorid_t pr_id,int length)1857c478bd9Sstevel@tonic-gate Format_state(char *str, char state, processorid_t pr_id, int length)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate switch (state) {
1887c478bd9Sstevel@tonic-gate case 'S':
1897c478bd9Sstevel@tonic-gate (void) strncpy(str, "sleep", length);
1907c478bd9Sstevel@tonic-gate break;
1917c478bd9Sstevel@tonic-gate case 'R':
1927c478bd9Sstevel@tonic-gate (void) strncpy(str, "run", length);
1937c478bd9Sstevel@tonic-gate break;
1947c478bd9Sstevel@tonic-gate case 'Z':
1957c478bd9Sstevel@tonic-gate (void) strncpy(str, "zombie", length);
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate case 'T':
1987c478bd9Sstevel@tonic-gate (void) strncpy(str, "stop", length);
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate case 'I':
2017c478bd9Sstevel@tonic-gate (void) strncpy(str, "idle", length);
2027c478bd9Sstevel@tonic-gate break;
203c97ad5cdSakolb case 'W':
204c97ad5cdSakolb (void) strncpy(str, "wait", length);
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate case 'O':
2077c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "cpu%-3d", (int)pr_id);
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate default:
2107c478bd9Sstevel@tonic-gate (void) strncpy(str, "?", length);
2117c478bd9Sstevel@tonic-gate break;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate void *
Realloc(void * ptr,size_t size)2167c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate int cnt = 0;
2197c478bd9Sstevel@tonic-gate void *sav = ptr;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate eagain: if ((ptr = realloc(ptr, size)))
2227c478bd9Sstevel@tonic-gate return (ptr);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate if ((++cnt <= 3) && (errno == EAGAIN)) {
2257c478bd9Sstevel@tonic-gate Warn(gettext("realloc() failed, attempt %d"), cnt);
2267c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, 5000); /* wait for 5 seconds */
2277c478bd9Sstevel@tonic-gate ptr = sav;
2287c478bd9Sstevel@tonic-gate goto eagain;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate ptr = sav;
2317c478bd9Sstevel@tonic-gate Die(gettext("not enough memory"));
2327c478bd9Sstevel@tonic-gate /*NOTREACHED*/
2337c478bd9Sstevel@tonic-gate return (NULL); /* keep gcc happy */
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate void *
Malloc(size_t size)2377c478bd9Sstevel@tonic-gate Malloc(size_t size)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate return (Realloc(NULL, size));
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate void *
Zalloc(size_t size)2437c478bd9Sstevel@tonic-gate Zalloc(size_t size)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate return (memset(Realloc(NULL, size), 0, size));
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate int
Setrlimit()2497c478bd9Sstevel@tonic-gate Setrlimit()
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate struct rlimit rlim;
2527c478bd9Sstevel@tonic-gate int fd_limit;
2537c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
2547c478bd9Sstevel@tonic-gate Die(gettext("getrlimit failed"));
2557c478bd9Sstevel@tonic-gate fd_limit = rlim.rlim_cur;
2567c478bd9Sstevel@tonic-gate rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
2577c478bd9Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max;
258004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
2597c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
2607c478bd9Sstevel@tonic-gate return (fd_limit);
2617c478bd9Sstevel@tonic-gate else
2627c478bd9Sstevel@tonic-gate return (rlim.rlim_cur);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate void
Priocntl(char * class)2667c478bd9Sstevel@tonic-gate Priocntl(char *class)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
2697c478bd9Sstevel@tonic-gate pcparms_t pcparms;
2707c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, class);
2717c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
2727c478bd9Sstevel@tonic-gate Warn(gettext("cannot get real time class parameters"));
2737c478bd9Sstevel@tonic-gate return;
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate pcparms.pc_cid = pcinfo.pc_cid;
2767c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
2777c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
2787c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
2797c478bd9Sstevel@tonic-gate if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
2807c478bd9Sstevel@tonic-gate Warn(gettext("cannot enter the real time class"));
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate void
getprojname(projid_t projid,char * str,size_t len,int noresolve,int trunc,size_t width)2840a1278f2SGary Mills getprojname(projid_t projid, char *str, size_t len, int noresolve,
2850a1278f2SGary Mills int trunc, size_t width)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate struct project proj;
2880a1278f2SGary Mills size_t n;
2897c478bd9Sstevel@tonic-gate
2907166d658SMenno Lageman if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) ==
2910a1278f2SGary Mills NULL) {
2927c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-6d", (int)projid);
2930a1278f2SGary Mills } else {
2940a1278f2SGary Mills n = mbstowcs(NULL, proj.pj_name, 0);
2950a1278f2SGary Mills if (n == (size_t)-1)
2960a1278f2SGary Mills (void) snprintf(str, len, "%-28s", "ERROR");
2970a1278f2SGary Mills else if (trunc && n > width)
2980a1278f2SGary Mills (void) snprintf(str, len, "%.*s%c", width - 1,
2990a1278f2SGary Mills proj.pj_name, '*');
3000a1278f2SGary Mills else
3010a1278f2SGary Mills (void) snprintf(str, len, "%-28s", proj.pj_name);
3020a1278f2SGary Mills }
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate void
getzonename(zoneid_t zoneid,char * str,size_t len,int trunc,size_t width)3060a1278f2SGary Mills getzonename(zoneid_t zoneid, char *str, size_t len, int trunc, size_t width)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate char zone_name[ZONENAME_MAX];
3090a1278f2SGary Mills size_t n;
3107c478bd9Sstevel@tonic-gate
3110a1278f2SGary Mills if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0) {
3127c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-6d", (int)zoneid);
3130a1278f2SGary Mills } else {
3140a1278f2SGary Mills n = mbstowcs(NULL, zone_name, 0);
3150a1278f2SGary Mills if (n == (size_t)-1)
3160a1278f2SGary Mills (void) snprintf(str, len, "%-28s", "ERROR");
3170a1278f2SGary Mills else if (trunc && n > width)
3180a1278f2SGary Mills (void) snprintf(str, len, "%.*s%c", width - 1,
3190a1278f2SGary Mills zone_name, '*');
3200a1278f2SGary Mills else
3210a1278f2SGary Mills (void) snprintf(str, len, "%-28s", zone_name);
3220a1278f2SGary Mills }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate * Remove all unprintable characters from process name
3277c478bd9Sstevel@tonic-gate */
328*ab618543SJohn Levon static void
stripfname(char * buf,size_t bufsize,const char * pname)329*ab618543SJohn Levon stripfname(char *buf, size_t bufsize, const char *pname)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate int bytesleft = PRFNSZ;
3327c478bd9Sstevel@tonic-gate wchar_t wchar;
3337c478bd9Sstevel@tonic-gate int length;
3347c478bd9Sstevel@tonic-gate char *cp;
3357c478bd9Sstevel@tonic-gate
336*ab618543SJohn Levon (void) strlcpy(buf, pname, bufsize);
337*ab618543SJohn Levon
3387c478bd9Sstevel@tonic-gate buf[bytesleft - 1] = '\0';
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate for (cp = buf; *cp != '\0'; cp += length) {
3417c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX);
3427c478bd9Sstevel@tonic-gate if (length <= 0) {
3437c478bd9Sstevel@tonic-gate *cp = '\0';
3447c478bd9Sstevel@tonic-gate break;
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate if (!iswprint(wchar)) {
3477c478bd9Sstevel@tonic-gate if (bytesleft <= length) {
3487c478bd9Sstevel@tonic-gate *cp = '\0';
3497c478bd9Sstevel@tonic-gate break;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate (void) memmove(cp, cp + length, bytesleft - length);
3527c478bd9Sstevel@tonic-gate length = 0;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate bytesleft -= length;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate }
357*ab618543SJohn Levon
358*ab618543SJohn Levon
359*ab618543SJohn Levon /*
360*ab618543SJohn Levon * prstat has always implicitly wanted a terminal width of at least 80 columns
361*ab618543SJohn Levon * (when a TTY is present). If run in a terminal narrower than 80 columns,
362*ab618543SJohn Levon * prstat output may wrap. For wider terminals, we allow the last column to use
363*ab618543SJohn Levon * the additional space.
364*ab618543SJohn Levon *
365*ab618543SJohn Levon * We never truncate if using -c, or not outputting to a TTY.
366*ab618543SJohn Levon */
367*ab618543SJohn Levon static int
format_namewidth(void)368*ab618543SJohn Levon format_namewidth(void)
369*ab618543SJohn Levon {
370*ab618543SJohn Levon int prefixlen = 0;
371*ab618543SJohn Levon
372*ab618543SJohn Levon if (opts.o_cols == 0 || !(opts.o_outpmode & (OPT_TERMCAP | OPT_TRUNC)))
373*ab618543SJohn Levon return (0);
374*ab618543SJohn Levon
375*ab618543SJohn Levon if (opts.o_outpmode & OPT_PSINFO) {
376*ab618543SJohn Levon if (opts.o_outpmode & OPT_LGRP)
377*ab618543SJohn Levon prefixlen = 64;
378*ab618543SJohn Levon else
379*ab618543SJohn Levon prefixlen = 59;
380*ab618543SJohn Levon } else if (opts.o_outpmode & OPT_MSACCT) {
381*ab618543SJohn Levon prefixlen = 64;
382*ab618543SJohn Levon }
383*ab618543SJohn Levon
384*ab618543SJohn Levon return (opts.o_cols - prefixlen);
385*ab618543SJohn Levon }
386*ab618543SJohn Levon
387*ab618543SJohn Levon void
format_name(lwp_info_t * lwp,char * buf,size_t buflen)388*ab618543SJohn Levon format_name(lwp_info_t *lwp, char *buf, size_t buflen)
389*ab618543SJohn Levon {
390*ab618543SJohn Levon int pname_width = PRFNSZ;
391*ab618543SJohn Levon char nr_suffix[20];
392*ab618543SJohn Levon char pname[PRFNSZ];
393*ab618543SJohn Levon int width;
394*ab618543SJohn Levon int n;
395*ab618543SJohn Levon
396*ab618543SJohn Levon stripfname(pname, sizeof (pname), lwp->li_info.pr_fname);
397*ab618543SJohn Levon
398*ab618543SJohn Levon if (opts.o_outpmode & OPT_LWPS) {
399*ab618543SJohn Levon n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
400*ab618543SJohn Levon lwp->li_info.pr_lwp.pr_lwpid);
401*ab618543SJohn Levon } else {
402*ab618543SJohn Levon n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
403*ab618543SJohn Levon lwp->li_info.pr_nlwp + lwp->li_info.pr_nzomb);
404*ab618543SJohn Levon }
405*ab618543SJohn Levon
406*ab618543SJohn Levon width = format_namewidth();
407*ab618543SJohn Levon
408*ab618543SJohn Levon /* If we're over budget, truncate the process name not the LWP part. */
409*ab618543SJohn Levon if (strlen(pname) > (width - n - 1)) {
410*ab618543SJohn Levon pname_width = width - n - 1;
411*ab618543SJohn Levon pname[pname_width - 1] = '*';
412*ab618543SJohn Levon }
413*ab618543SJohn Levon
414*ab618543SJohn Levon if ((opts.o_outpmode & OPT_LWPS) && lwp->li_lwpname[0] != '\0') {
415*ab618543SJohn Levon n = snprintf(buf, buflen, "%.*s/%s [%s]", pname_width,
416*ab618543SJohn Levon pname, nr_suffix, lwp->li_lwpname);
417*ab618543SJohn Levon } else {
418*ab618543SJohn Levon n = snprintf(buf, buflen, "%.*s/%s", pname_width,
419*ab618543SJohn Levon pname, nr_suffix);
420*ab618543SJohn Levon }
421*ab618543SJohn Levon
422*ab618543SJohn Levon if (width > 0 && strlen(buf) > width)
423*ab618543SJohn Levon buf[width] = '\0';
424*ab618543SJohn Levon }
425