xref: /illumos-gate/usr/src/cmd/ps/ps.c (revision d21c64a5)
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
551223f18Sjonb  * Common Development and Distribution License (the "License").
651223f18Sjonb  * 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  */
2151223f18Sjonb 
227c478bd9Sstevel@tonic-gate /*
23*d21c64a5SDavid Edmondson  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * ps -- print things about processes.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <pwd.h>
397c478bd9Sstevel@tonic-gate #include <grp.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <dirent.h>
477c478bd9Sstevel@tonic-gate #include <sys/signal.h>
487c478bd9Sstevel@tonic-gate #include <sys/fault.h>
497c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
507c478bd9Sstevel@tonic-gate #include <sys/time.h>
517c478bd9Sstevel@tonic-gate #include <procfs.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <wctype.h>
547c478bd9Sstevel@tonic-gate #include <wchar.h>
557c478bd9Sstevel@tonic-gate #include <libw.h>
567c478bd9Sstevel@tonic-gate #include <stdarg.h>
577c478bd9Sstevel@tonic-gate #include <sys/proc.h>
587c478bd9Sstevel@tonic-gate #include <sys/pset.h>
597c478bd9Sstevel@tonic-gate #include <project.h>
607c478bd9Sstevel@tonic-gate #include <zone.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	min(a, b)	((a) > (b) ? (b) : (a))
637c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	NTTYS	20	/* initial size of table for -t option  */
66c6402783Sakolb #define	SIZ	30	/* initial size of tables for -p, -s, -g, -h and -z */
6751223f18Sjonb 
6851223f18Sjonb /*
6951223f18Sjonb  * Size of buffer holding args for t, p, s, g, u, U, G, z options.
7051223f18Sjonb  * Set to ZONENAME_MAX, the minimum value needed to allow any
7151223f18Sjonb  * zone to be specified.
7251223f18Sjonb  */
7351223f18Sjonb #define	ARGSIZ ZONENAME_MAX
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	MAXUGNAME 10	/* max chars in a user/group name or printed u/g id */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Structure for storing user or group info */
787c478bd9Sstevel@tonic-gate struct ugdata {
797c478bd9Sstevel@tonic-gate 	id_t	id;			/* numeric user-id or group-id */
807c478bd9Sstevel@tonic-gate 	char	name[MAXUGNAME+1];	/* user/group name, null terminated */
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate struct ughead {
847c478bd9Sstevel@tonic-gate 	size_t	size;		/* number of ugdata structs allocated */
857c478bd9Sstevel@tonic-gate 	size_t	nent;		/* number of active entries */
867c478bd9Sstevel@tonic-gate 	struct ugdata *ent;	/* pointer to array of actual entries */
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate enum fname {	/* enumeration of field names */
907c478bd9Sstevel@tonic-gate 	F_USER,		/* effective user of the process */
917c478bd9Sstevel@tonic-gate 	F_RUSER,	/* real user of the process */
927c478bd9Sstevel@tonic-gate 	F_GROUP,	/* effective group of the process */
937c478bd9Sstevel@tonic-gate 	F_RGROUP,	/* real group of the process */
947c478bd9Sstevel@tonic-gate 	F_UID,		/* numeric effective uid of the process */
957c478bd9Sstevel@tonic-gate 	F_RUID,		/* numeric real uid of the process */
967c478bd9Sstevel@tonic-gate 	F_GID,		/* numeric effective gid of the process */
977c478bd9Sstevel@tonic-gate 	F_RGID,		/* numeric real gid of the process */
987c478bd9Sstevel@tonic-gate 	F_PID,		/* process id */
997c478bd9Sstevel@tonic-gate 	F_PPID,		/* parent process id */
1007c478bd9Sstevel@tonic-gate 	F_PGID,		/* process group id */
1017c478bd9Sstevel@tonic-gate 	F_SID,		/* session id */
1027c478bd9Sstevel@tonic-gate 	F_PSR,		/* bound processor */
1037c478bd9Sstevel@tonic-gate 	F_LWP,		/* lwp-id */
1047c478bd9Sstevel@tonic-gate 	F_NLWP,		/* number of lwps */
1057c478bd9Sstevel@tonic-gate 	F_OPRI,		/* old priority (obsolete) */
1067c478bd9Sstevel@tonic-gate 	F_PRI,		/* new priority */
1077c478bd9Sstevel@tonic-gate 	F_F,		/* process flags */
1087c478bd9Sstevel@tonic-gate 	F_S,		/* letter indicating the state */
1097c478bd9Sstevel@tonic-gate 	F_C,		/* processor utilization (obsolete) */
1107c478bd9Sstevel@tonic-gate 	F_PCPU,		/* percent of recently used cpu time */
1117c478bd9Sstevel@tonic-gate 	F_PMEM,		/* percent of physical memory used (rss) */
1127c478bd9Sstevel@tonic-gate 	F_OSZ,		/* virtual size of the process in pages */
1137c478bd9Sstevel@tonic-gate 	F_VSZ,		/* virtual size of the process in kilobytes */
1147c478bd9Sstevel@tonic-gate 	F_RSS,		/* resident set size of the process in kilobytes */
1157c478bd9Sstevel@tonic-gate 	F_NICE,		/* "nice" value of the process */
1167c478bd9Sstevel@tonic-gate 	F_CLASS,	/* scheduler class */
1177c478bd9Sstevel@tonic-gate 	F_STIME,	/* start time of the process, hh:mm:ss or Month Day */
1187c478bd9Sstevel@tonic-gate 	F_ETIME,	/* elapsed time of the process, [[dd-]hh:]mm:ss */
1197c478bd9Sstevel@tonic-gate 	F_TIME,		/* cpu time of the process, [[dd-]hh:]mm:ss */
1207c478bd9Sstevel@tonic-gate 	F_TTY,		/* name of the controlling terminal */
1217c478bd9Sstevel@tonic-gate 	F_ADDR,		/* address of the process (obsolete) */
1227c478bd9Sstevel@tonic-gate 	F_WCHAN,	/* wait channel (sleep condition variable) */
1237c478bd9Sstevel@tonic-gate 	F_FNAME,	/* file name of command */
1247c478bd9Sstevel@tonic-gate 	F_COMM,		/* name of command (argv[0] value) */
1257c478bd9Sstevel@tonic-gate 	F_ARGS,		/* name of command plus all its arguments */
1267c478bd9Sstevel@tonic-gate 	F_TASKID,	/* task id */
1277c478bd9Sstevel@tonic-gate 	F_PROJID,	/* project id */
1287c478bd9Sstevel@tonic-gate 	F_PROJECT,	/* project name of the process */
1297c478bd9Sstevel@tonic-gate 	F_PSET,		/* bound processor set */
1307c478bd9Sstevel@tonic-gate 	F_ZONE,		/* zone name */
1317c478bd9Sstevel@tonic-gate 	F_ZONEID,	/* zone id */
132c6402783Sakolb 	F_CTID,		/* process contract id */
133c6402783Sakolb 	F_LGRP		/* process home lgroup */
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate struct field {
1377c478bd9Sstevel@tonic-gate 	struct field	*next;		/* linked list */
1387c478bd9Sstevel@tonic-gate 	int		fname;		/* field index */
1397c478bd9Sstevel@tonic-gate 	const char	*header;	/* header to use */
1407c478bd9Sstevel@tonic-gate 	int		width;		/* width of field */
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static	struct field *fields = NULL;	/* fields selected via -o */
1447c478bd9Sstevel@tonic-gate static	struct field *last_field = NULL;
1457c478bd9Sstevel@tonic-gate static	int do_header = 0;
1467c478bd9Sstevel@tonic-gate static	struct timeval now;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* array of defined fields, in fname order */
1497c478bd9Sstevel@tonic-gate struct def_field {
1507c478bd9Sstevel@tonic-gate 	const char *fname;
1517c478bd9Sstevel@tonic-gate 	const char *header;
1527c478bd9Sstevel@tonic-gate 	int width;
1537c478bd9Sstevel@tonic-gate 	int minwidth;
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static struct def_field fname[] = {
1577c478bd9Sstevel@tonic-gate 	/* fname	header		width	minwidth */
1587c478bd9Sstevel@tonic-gate 	{ "user",	"USER",		8,	8	},
1597c478bd9Sstevel@tonic-gate 	{ "ruser",	"RUSER",	8,	8	},
1607c478bd9Sstevel@tonic-gate 	{ "group",	"GROUP",	8,	8	},
1617c478bd9Sstevel@tonic-gate 	{ "rgroup",	"RGROUP",	8,	8	},
1627c478bd9Sstevel@tonic-gate 	{ "uid",	"UID",		5,	5	},
1637c478bd9Sstevel@tonic-gate 	{ "ruid",	"RUID",		5,	5	},
1647c478bd9Sstevel@tonic-gate 	{ "gid",	"GID",		5,	5	},
1657c478bd9Sstevel@tonic-gate 	{ "rgid",	"RGID",		5,	5	},
1667c478bd9Sstevel@tonic-gate 	{ "pid",	"PID",		5,	5	},
1677c478bd9Sstevel@tonic-gate 	{ "ppid",	"PPID",		5,	5	},
1687c478bd9Sstevel@tonic-gate 	{ "pgid",	"PGID",		5,	5	},
1697c478bd9Sstevel@tonic-gate 	{ "sid",	"SID",		5,	5	},
1707c478bd9Sstevel@tonic-gate 	{ "psr",	"PSR",		3,	2	},
1717c478bd9Sstevel@tonic-gate 	{ "lwp",	"LWP",		6,	2	},
1727c478bd9Sstevel@tonic-gate 	{ "nlwp",	"NLWP",		4,	2	},
1737c478bd9Sstevel@tonic-gate 	{ "opri",	"PRI",		3,	2	},
1747c478bd9Sstevel@tonic-gate 	{ "pri",	"PRI",		3,	2	},
1757c478bd9Sstevel@tonic-gate 	{ "f",		"F",		2,	2	},
1767c478bd9Sstevel@tonic-gate 	{ "s",		"S",		1,	1	},
1777c478bd9Sstevel@tonic-gate 	{ "c",		"C",		2,	2	},
1787c478bd9Sstevel@tonic-gate 	{ "pcpu",	"%CPU",		4,	4	},
1797c478bd9Sstevel@tonic-gate 	{ "pmem",	"%MEM",		4,	4	},
1807c478bd9Sstevel@tonic-gate 	{ "osz",	"SZ",		4,	4	},
1817c478bd9Sstevel@tonic-gate 	{ "vsz",	"VSZ",		4,	4	},
1827c478bd9Sstevel@tonic-gate 	{ "rss",	"RSS",		4,	4	},
1837c478bd9Sstevel@tonic-gate 	{ "nice",	"NI",		2,	2	},
1847c478bd9Sstevel@tonic-gate 	{ "class",	"CLS",		4,	2	},
1857c478bd9Sstevel@tonic-gate 	{ "stime",	"STIME",	8,	8	},
1867c478bd9Sstevel@tonic-gate 	{ "etime",	"ELAPSED",	11,	7	},
1877c478bd9Sstevel@tonic-gate 	{ "time",	"TIME",		11,	5	},
1887c478bd9Sstevel@tonic-gate 	{ "tty",	"TT",		7,	7	},
1897c478bd9Sstevel@tonic-gate #ifdef _LP64
1907c478bd9Sstevel@tonic-gate 	{ "addr",	"ADDR",		16,	8	},
1917c478bd9Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	16,	8	},
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate 	{ "addr",	"ADDR",		8,	8	},
1947c478bd9Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	8,	8	},
1957c478bd9Sstevel@tonic-gate #endif
1967c478bd9Sstevel@tonic-gate 	{ "fname",	"COMMAND",	8,	8	},
1977c478bd9Sstevel@tonic-gate 	{ "comm",	"COMMAND",	80,	8	},
1987c478bd9Sstevel@tonic-gate 	{ "args",	"COMMAND",	80,	80	},
1997c478bd9Sstevel@tonic-gate 	{ "taskid",	"TASKID",	5,	5	},
2007c478bd9Sstevel@tonic-gate 	{ "projid",	"PROJID",	5,	5	},
2017c478bd9Sstevel@tonic-gate 	{ "project",	"PROJECT",	8,	8	},
2027c478bd9Sstevel@tonic-gate 	{ "pset",	"PSET",		3,	3	},
2037c478bd9Sstevel@tonic-gate 	{ "zone",	"ZONE",		8,	8	},
2047c478bd9Sstevel@tonic-gate 	{ "zoneid",	"ZONEID",	5,	5	},
2057c478bd9Sstevel@tonic-gate 	{ "ctid",	"CTID",		5,	5	},
206c6402783Sakolb 	{ "lgrp",	"LGRP",		4,	2 	},
2077c478bd9Sstevel@tonic-gate };
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #define	NFIELDS	(sizeof (fname) / sizeof (fname[0]))
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static	int	retcode = 1;
2127c478bd9Sstevel@tonic-gate static	int	lflg;
2137c478bd9Sstevel@tonic-gate static	int	Aflg;
2147c478bd9Sstevel@tonic-gate static	int	uflg;
2157c478bd9Sstevel@tonic-gate static	int	Uflg;
2167c478bd9Sstevel@tonic-gate static	int	Gflg;
2177c478bd9Sstevel@tonic-gate static	int	aflg;
2187c478bd9Sstevel@tonic-gate static	int	dflg;
2197c478bd9Sstevel@tonic-gate static	int	Lflg;
2207c478bd9Sstevel@tonic-gate static	int	Pflg;
2217c478bd9Sstevel@tonic-gate static	int	yflg;
2227c478bd9Sstevel@tonic-gate static	int	pflg;
2237c478bd9Sstevel@tonic-gate static	int	fflg;
2247c478bd9Sstevel@tonic-gate static	int	cflg;
2257c478bd9Sstevel@tonic-gate static	int	jflg;
2267c478bd9Sstevel@tonic-gate static	int	gflg;
2277c478bd9Sstevel@tonic-gate static	int	sflg;
2287c478bd9Sstevel@tonic-gate static	int	tflg;
2297c478bd9Sstevel@tonic-gate static	int	zflg;
2307c478bd9Sstevel@tonic-gate static	int	Zflg;
231c6402783Sakolb static	int	hflg;
232c6402783Sakolb static	int	Hflg;
233f48205beScasper static	uid_t	tuid = (uid_t)-1;
2347c478bd9Sstevel@tonic-gate static	int	errflg;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static	int	ndev;		/* number of devices */
2377c478bd9Sstevel@tonic-gate static	int	maxdev;		/* number of devl structures allocated */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #define	DNINCR	100
2407c478bd9Sstevel@tonic-gate #define	DNSIZE	14
2417c478bd9Sstevel@tonic-gate static struct devl {		/* device list   */
2427c478bd9Sstevel@tonic-gate 	char	dname[DNSIZE];	/* device name   */
2437c478bd9Sstevel@tonic-gate 	dev_t	ddev;		/* device number */
2447c478bd9Sstevel@tonic-gate } *devl;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static	struct tty {
2477c478bd9Sstevel@tonic-gate 	char *tname;
2487c478bd9Sstevel@tonic-gate 	dev_t tdev;
2497c478bd9Sstevel@tonic-gate } *tty = NULL;			/* for t option */
2507c478bd9Sstevel@tonic-gate static	size_t	ttysz = 0;
2517c478bd9Sstevel@tonic-gate static	int	ntty = 0;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static	pid_t	*pid = NULL;	/* for p option */
2547c478bd9Sstevel@tonic-gate static	size_t	pidsz = 0;
2557c478bd9Sstevel@tonic-gate static	size_t	npid = 0;
2567c478bd9Sstevel@tonic-gate 
257c6402783Sakolb static	int	*lgrps = NULL;	/* list of lgroup IDs for for h option */
258c6402783Sakolb static	size_t	lgrps_size = 0;	/* size of the lgrps list */
259c6402783Sakolb static	size_t	nlgrps = 0;	/* number elements in the list */
260c6402783Sakolb 
261c6402783Sakolb /* Maximum possible lgroup ID value */
262c6402783Sakolb #define	MAX_LGRP_ID 256
263c6402783Sakolb 
2647c478bd9Sstevel@tonic-gate static	pid_t	*grpid = NULL;	/* for g option */
2657c478bd9Sstevel@tonic-gate static	size_t	grpidsz = 0;
2667c478bd9Sstevel@tonic-gate static	int	ngrpid = 0;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate static	pid_t	*sessid = NULL;	/* for s option */
2697c478bd9Sstevel@tonic-gate static	size_t	sessidsz = 0;
2707c478bd9Sstevel@tonic-gate static	int	nsessid = 0;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static	zoneid_t *zoneid = NULL; /* for z option */
2737c478bd9Sstevel@tonic-gate static	size_t	zoneidsz = 0;
2747c478bd9Sstevel@tonic-gate static	int	nzoneid = 0;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static	int	kbytes_per_page;
2777c478bd9Sstevel@tonic-gate static	int	pidwidth;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static	char	*procdir = "/proc";	/* standard /proc directory */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static struct ughead	euid_tbl;	/* table to store selected euid's */
2827c478bd9Sstevel@tonic-gate static struct ughead	ruid_tbl;	/* table to store selected real uid's */
2837c478bd9Sstevel@tonic-gate static struct ughead	egid_tbl;	/* table to store selected egid's */
2847c478bd9Sstevel@tonic-gate static struct ughead	rgid_tbl;	/* table to store selected real gid's */
2857c478bd9Sstevel@tonic-gate static prheader_t *lpsinfobuf;		/* buffer to contain lpsinfo */
2867c478bd9Sstevel@tonic-gate static size_t	lpbufsize;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * This constant defines the sentinal number of process IDs below which we
2907c478bd9Sstevel@tonic-gate  * only examine individual entries in /proc rather than scanning through
2917c478bd9Sstevel@tonic-gate  * /proc. This optimization is a huge win in the common case.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate #define	PTHRESHOLD	40
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static	void	usage(void);
2967c478bd9Sstevel@tonic-gate static	char	*getarg(char **);
2977c478bd9Sstevel@tonic-gate static	char	*parse_format(char *);
2987c478bd9Sstevel@tonic-gate static	char	*gettty(psinfo_t *);
2997c478bd9Sstevel@tonic-gate static	int	prfind(int, psinfo_t *, char **);
3007c478bd9Sstevel@tonic-gate static	void	prcom(psinfo_t *, char *);
3017c478bd9Sstevel@tonic-gate static	void	prtpct(ushort_t, int);
3027c478bd9Sstevel@tonic-gate static	void	print_time(time_t, int);
3037c478bd9Sstevel@tonic-gate static	void	print_field(psinfo_t *, struct field *, const char *);
3047c478bd9Sstevel@tonic-gate static	void	print_zombie_field(psinfo_t *, struct field *, const char *);
3057c478bd9Sstevel@tonic-gate static	void	pr_fields(psinfo_t *, const char *,
3067c478bd9Sstevel@tonic-gate 		void (*print_fld)(psinfo_t *, struct field *, const char *));
3077c478bd9Sstevel@tonic-gate static	int	search(pid_t *, int, pid_t);
3087c478bd9Sstevel@tonic-gate static	void	add_ugentry(struct ughead *, char *);
3097c478bd9Sstevel@tonic-gate static	int	uconv(struct ughead *);
3107c478bd9Sstevel@tonic-gate static	int	gconv(struct ughead *);
311f48205beScasper static	int	ugfind(id_t, struct ughead *);
3127c478bd9Sstevel@tonic-gate static	void	prtime(timestruc_t, int, int);
3137c478bd9Sstevel@tonic-gate static	void	przom(psinfo_t *);
3147c478bd9Sstevel@tonic-gate static	int	namencnt(char *, int, int);
3157c478bd9Sstevel@tonic-gate static	char	*err_string(int);
3167c478bd9Sstevel@tonic-gate static	int	print_proc(char *pname);
3177c478bd9Sstevel@tonic-gate static	time_t	delta_secs(const timestruc_t *);
3187c478bd9Sstevel@tonic-gate static	int	str2id(const char *, pid_t *, long, long);
319f48205beScasper static	int	str2uid(const char *,  uid_t *, unsigned long, unsigned long);
3207c478bd9Sstevel@tonic-gate static	void	*Realloc(void *, size_t);
3217c478bd9Sstevel@tonic-gate static	int	pidcmp(const void *p1, const void *p2);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate int
3247c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	char	*p;
3277c478bd9Sstevel@tonic-gate 	char	*p1;
3287c478bd9Sstevel@tonic-gate 	char	*parg;
3297c478bd9Sstevel@tonic-gate 	int	c;
3307c478bd9Sstevel@tonic-gate 	int	i;
3317c478bd9Sstevel@tonic-gate 	int	pgerrflg = 0;	/* err flg: non-numeric arg w/p & g options */
33216f94f58Ssayama 	size_t	size, len;
3337c478bd9Sstevel@tonic-gate 	DIR	*dirp;
3347c478bd9Sstevel@tonic-gate 	struct dirent *dentp;
3357c478bd9Sstevel@tonic-gate 	pid_t	maxpid;
3367c478bd9Sstevel@tonic-gate 	pid_t	id;
3377c478bd9Sstevel@tonic-gate 	int	ret;
33816f94f58Ssayama 	char	loc_stime_str[32];
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3417c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3427c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
3437c478bd9Sstevel@tonic-gate #endif
3447c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	(void) memset(&euid_tbl, 0, sizeof (euid_tbl));
3477c478bd9Sstevel@tonic-gate 	(void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
3487c478bd9Sstevel@tonic-gate 	(void) memset(&egid_tbl, 0, sizeof (egid_tbl));
3497c478bd9Sstevel@tonic-gate 	(void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * calculate width of pid fields based on configured MAXPID
3577c478bd9Sstevel@tonic-gate 	 * (must be at least 5 to retain output format compatibility)
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate 	id = maxpid = (pid_t)sysconf(_SC_MAXPID);
3607c478bd9Sstevel@tonic-gate 	pidwidth = 1;
3617c478bd9Sstevel@tonic-gate 	while ((id /= 10) > 0)
3627c478bd9Sstevel@tonic-gate 		++pidwidth;
3637c478bd9Sstevel@tonic-gate 	pidwidth = pidwidth < 5 ? 5 : pidwidth;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	fname[F_PID].width = fname[F_PPID].width = pidwidth;
3667c478bd9Sstevel@tonic-gate 	fname[F_PGID].width = fname[F_SID].width = pidwidth;
3677c478bd9Sstevel@tonic-gate 
36816f94f58Ssayama 	/*
36916f94f58Ssayama 	 * TRANSLATION_NOTE
37016f94f58Ssayama 	 * Specify the printf format with width and precision for
37116f94f58Ssayama 	 * the STIME field.
37216f94f58Ssayama 	 */
37316f94f58Ssayama 	len = snprintf(loc_stime_str, sizeof (loc_stime_str),
37416f94f58Ssayama 	    dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
37516f94f58Ssayama 	if (len >= sizeof (loc_stime_str))
37616f94f58Ssayama 		len = sizeof (loc_stime_str) - 1;
37716f94f58Ssayama 
37816f94f58Ssayama 	fname[F_STIME].width = fname[F_STIME].minwidth = len;
37916f94f58Ssayama 
380c6402783Sakolb 	while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
381c6402783Sakolb 	    != EOF)
3827c478bd9Sstevel@tonic-gate 		switch (c) {
383c6402783Sakolb 		case 'H':		/* Show home lgroups */
384c6402783Sakolb 			Hflg++;
385c6402783Sakolb 			break;
386c6402783Sakolb 		case 'h':
387c6402783Sakolb 			/*
388c6402783Sakolb 			 * Show processes/threads with given home lgroups
389c6402783Sakolb 			 */
390c6402783Sakolb 			hflg++;
391c6402783Sakolb 			p1 = optarg;
392c6402783Sakolb 			do {
393c6402783Sakolb 				int id;
394c6402783Sakolb 
395c6402783Sakolb 				/*
396c6402783Sakolb 				 * Get all IDs in the list, verify for
397c6402783Sakolb 				 * correctness and place in lgrps array.
398c6402783Sakolb 				 */
399c6402783Sakolb 				parg = getarg(&p1);
400c6402783Sakolb 				/* Convert string to integer */
401c6402783Sakolb 				ret = str2id(parg, (pid_t *)&id, 0,
402*d21c64a5SDavid Edmondson 				    MAX_LGRP_ID);
403c6402783Sakolb 				/* Complain if ID didn't parse correctly */
404c6402783Sakolb 				if (ret != 0) {
405c6402783Sakolb 					pgerrflg++;
406c6402783Sakolb 					(void) fprintf(stderr,
407c6402783Sakolb 					    gettext("ps: %s "), parg);
408c6402783Sakolb 					if (ret == EINVAL)
409c6402783Sakolb 						(void) fprintf(stderr,
410c6402783Sakolb 						    gettext("is an invalid "
411c6402783Sakolb 						    "non-numeric argument"));
412c6402783Sakolb 					else
413c6402783Sakolb 						(void) fprintf(stderr,
414c6402783Sakolb 						    gettext("exceeds valid "
415c6402783Sakolb 						    "range"));
416c6402783Sakolb 					(void) fprintf(stderr,
417c6402783Sakolb 					    gettext(" for -h option\n"));
418c6402783Sakolb 					continue;
419c6402783Sakolb 				}
420c6402783Sakolb 
421c6402783Sakolb 				/* Extend lgrps array if needed */
422c6402783Sakolb 				if (nlgrps == lgrps_size) {
423c6402783Sakolb 					/* Double the size of the lgrps array */
424c6402783Sakolb 					if (lgrps_size == 0)
425c6402783Sakolb 						lgrps_size = SIZ;
426c6402783Sakolb 					lgrps_size *= 2;
427c6402783Sakolb 					lgrps = Realloc(lgrps,
428c6402783Sakolb 					    lgrps_size * sizeof (int));
429c6402783Sakolb 				}
430c6402783Sakolb 				/* place the id in the lgrps table */
431c6402783Sakolb 				lgrps[nlgrps++] = id;
432c6402783Sakolb 			} while (*p1);
433c6402783Sakolb 			break;
4347c478bd9Sstevel@tonic-gate 		case 'l':		/* long listing */
4357c478bd9Sstevel@tonic-gate 			lflg++;
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 		case 'f':		/* full listing */
4387c478bd9Sstevel@tonic-gate 			fflg++;
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 		case 'j':
4417c478bd9Sstevel@tonic-gate 			jflg++;
4427c478bd9Sstevel@tonic-gate 			break;
4437c478bd9Sstevel@tonic-gate 		case 'c':
4447c478bd9Sstevel@tonic-gate 			/*
4457c478bd9Sstevel@tonic-gate 			 * Format output to reflect scheduler changes:
4467c478bd9Sstevel@tonic-gate 			 * high numbers for high priorities and don't
4477c478bd9Sstevel@tonic-gate 			 * print nice or p_cpu values.  'c' option only
4487c478bd9Sstevel@tonic-gate 			 * effective when used with 'l' or 'f' options.
4497c478bd9Sstevel@tonic-gate 			 */
4507c478bd9Sstevel@tonic-gate 			cflg++;
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 		case 'A':		/* list every process */
4537c478bd9Sstevel@tonic-gate 		case 'e':		/* (obsolete) list every process */
4547c478bd9Sstevel@tonic-gate 			Aflg++;
4557c478bd9Sstevel@tonic-gate 			tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
456c6402783Sakolb 			zflg = hflg = 0;
4577c478bd9Sstevel@tonic-gate 			break;
4587c478bd9Sstevel@tonic-gate 		case 'a':
4597c478bd9Sstevel@tonic-gate 			/*
4607c478bd9Sstevel@tonic-gate 			 * Same as 'e' except no session group leaders
4617c478bd9Sstevel@tonic-gate 			 * and no non-terminal processes.
4627c478bd9Sstevel@tonic-gate 			 */
4637c478bd9Sstevel@tonic-gate 			aflg++;
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 		case 'd':	/* same as e except no session leaders */
4667c478bd9Sstevel@tonic-gate 			dflg++;
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 		case 'L':	/* show lwps */
4697c478bd9Sstevel@tonic-gate 			Lflg++;
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		case 'P':	/* show bound processor */
4727c478bd9Sstevel@tonic-gate 			Pflg++;
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 		case 'y':	/* omit F & ADDR, report RSS & SZ in Kby */
4757c478bd9Sstevel@tonic-gate 			yflg++;
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 		case 'n':	/* no longer needed; retain as no-op */
4787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4797c478bd9Sstevel@tonic-gate 			    gettext("ps: warning: -n option ignored\n"));
4807c478bd9Sstevel@tonic-gate 			break;
4817c478bd9Sstevel@tonic-gate 		case 't':		/* terminals */
4827c478bd9Sstevel@tonic-gate #define	TSZ	30
4837c478bd9Sstevel@tonic-gate 			tflg++;
4847c478bd9Sstevel@tonic-gate 			p1 = optarg;
4857c478bd9Sstevel@tonic-gate 			do {
4867c478bd9Sstevel@tonic-gate 				char nambuf[TSZ+6];	/* for "/dev/" + '\0' */
4877c478bd9Sstevel@tonic-gate 				struct stat64 s;
4887c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
4897c478bd9Sstevel@tonic-gate 				p = Realloc(NULL, TSZ+1);	/* for '\0' */
4907c478bd9Sstevel@tonic-gate 				/* zero the buffer before using it */
4917c478bd9Sstevel@tonic-gate 				p[0] = '\0';
4927c478bd9Sstevel@tonic-gate 				size = TSZ;
4937c478bd9Sstevel@tonic-gate 				if (isdigit(*parg)) {
4947c478bd9Sstevel@tonic-gate 					(void) strcpy(p, "tty");
4957c478bd9Sstevel@tonic-gate 					size -= 3;
4967c478bd9Sstevel@tonic-gate 				}
4977c478bd9Sstevel@tonic-gate 				(void) strncat(p, parg, size);
4987c478bd9Sstevel@tonic-gate 				if (ntty == ttysz) {
4997c478bd9Sstevel@tonic-gate 					if ((ttysz *= 2) == 0)
5007c478bd9Sstevel@tonic-gate 						ttysz = NTTYS;
5017c478bd9Sstevel@tonic-gate 					tty = Realloc(tty,
5027c478bd9Sstevel@tonic-gate 					    (ttysz + 1) * sizeof (struct tty));
5037c478bd9Sstevel@tonic-gate 				}
5047c478bd9Sstevel@tonic-gate 				tty[ntty].tdev = PRNODEV;
5057c478bd9Sstevel@tonic-gate 				(void) strcpy(nambuf, "/dev/");
5067c478bd9Sstevel@tonic-gate 				(void) strcat(nambuf, p);
5077c478bd9Sstevel@tonic-gate 				if (stat64(nambuf, &s) == 0)
5087c478bd9Sstevel@tonic-gate 					tty[ntty].tdev = s.st_rdev;
5097c478bd9Sstevel@tonic-gate 				tty[ntty++].tname = p;
5107c478bd9Sstevel@tonic-gate 			} while (*p1);
5117c478bd9Sstevel@tonic-gate 			break;
5127c478bd9Sstevel@tonic-gate 		case 'p':		/* proc ids */
5137c478bd9Sstevel@tonic-gate 			pflg++;
5147c478bd9Sstevel@tonic-gate 			p1 = optarg;
5157c478bd9Sstevel@tonic-gate 			do {
5167c478bd9Sstevel@tonic-gate 				pid_t id;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
5197c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5207c478bd9Sstevel@tonic-gate 					pgerrflg++;
5217c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5227c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5237c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
5247c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5257c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
5267c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
5277c478bd9Sstevel@tonic-gate 					else
5287c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5297c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
5307c478bd9Sstevel@tonic-gate 						    "range"));
5317c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5327c478bd9Sstevel@tonic-gate 					    gettext(" for -p option\n"));
5337c478bd9Sstevel@tonic-gate 					continue;
5347c478bd9Sstevel@tonic-gate 				}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 				if (npid == pidsz) {
5377c478bd9Sstevel@tonic-gate 					if ((pidsz *= 2) == 0)
5387c478bd9Sstevel@tonic-gate 						pidsz = SIZ;
5397c478bd9Sstevel@tonic-gate 					pid = Realloc(pid,
5407c478bd9Sstevel@tonic-gate 					    pidsz * sizeof (pid_t));
5417c478bd9Sstevel@tonic-gate 				}
5427c478bd9Sstevel@tonic-gate 				pid[npid++] = id;
5437c478bd9Sstevel@tonic-gate 			} while (*p1);
5447c478bd9Sstevel@tonic-gate 			break;
5457c478bd9Sstevel@tonic-gate 		case 's':		/* session */
5467c478bd9Sstevel@tonic-gate 			sflg++;
5477c478bd9Sstevel@tonic-gate 			p1 = optarg;
5487c478bd9Sstevel@tonic-gate 			do {
5497c478bd9Sstevel@tonic-gate 				pid_t id;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
5527c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5537c478bd9Sstevel@tonic-gate 					pgerrflg++;
5547c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5557c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5567c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
5577c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5587c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
5597c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
5607c478bd9Sstevel@tonic-gate 					else
5617c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5627c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
5637c478bd9Sstevel@tonic-gate 						    "range"));
5647c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5657c478bd9Sstevel@tonic-gate 					    gettext(" for -s option\n"));
5667c478bd9Sstevel@tonic-gate 					continue;
5677c478bd9Sstevel@tonic-gate 				}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 				if (nsessid == sessidsz) {
5707c478bd9Sstevel@tonic-gate 					if ((sessidsz *= 2) == 0)
5717c478bd9Sstevel@tonic-gate 						sessidsz = SIZ;
5727c478bd9Sstevel@tonic-gate 					sessid = Realloc(sessid,
5737c478bd9Sstevel@tonic-gate 					    sessidsz * sizeof (pid_t));
5747c478bd9Sstevel@tonic-gate 				}
5757c478bd9Sstevel@tonic-gate 				sessid[nsessid++] = id;
5767c478bd9Sstevel@tonic-gate 			} while (*p1);
5777c478bd9Sstevel@tonic-gate 			break;
5787c478bd9Sstevel@tonic-gate 		case 'g':		/* proc group */
5797c478bd9Sstevel@tonic-gate 			gflg++;
5807c478bd9Sstevel@tonic-gate 			p1 = optarg;
5817c478bd9Sstevel@tonic-gate 			do {
5827c478bd9Sstevel@tonic-gate 				pid_t id;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
5857c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5867c478bd9Sstevel@tonic-gate 					pgerrflg++;
5877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5887c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5897c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
5907c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5917c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
5927c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
5937c478bd9Sstevel@tonic-gate 					else
5947c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5957c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
5967c478bd9Sstevel@tonic-gate 						    "range"));
5977c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5987c478bd9Sstevel@tonic-gate 					    gettext(" for -g option\n"));
5997c478bd9Sstevel@tonic-gate 					continue;
6007c478bd9Sstevel@tonic-gate 				}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 				if (ngrpid == grpidsz) {
6037c478bd9Sstevel@tonic-gate 					if ((grpidsz *= 2) == 0)
6047c478bd9Sstevel@tonic-gate 						grpidsz = SIZ;
6057c478bd9Sstevel@tonic-gate 					grpid = Realloc(grpid,
6067c478bd9Sstevel@tonic-gate 					    grpidsz * sizeof (pid_t));
6077c478bd9Sstevel@tonic-gate 				}
6087c478bd9Sstevel@tonic-gate 				grpid[ngrpid++] = id;
6097c478bd9Sstevel@tonic-gate 			} while (*p1);
6107c478bd9Sstevel@tonic-gate 			break;
6117c478bd9Sstevel@tonic-gate 		case 'u':		/* effective user name or number */
6127c478bd9Sstevel@tonic-gate 			uflg++;
6137c478bd9Sstevel@tonic-gate 			p1 = optarg;
6147c478bd9Sstevel@tonic-gate 			do {
6157c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6167c478bd9Sstevel@tonic-gate 				add_ugentry(&euid_tbl, parg);
6177c478bd9Sstevel@tonic-gate 			} while (*p1);
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		case 'U':		/* real user name or number */
6207c478bd9Sstevel@tonic-gate 			Uflg++;
6217c478bd9Sstevel@tonic-gate 			p1 = optarg;
6227c478bd9Sstevel@tonic-gate 			do {
6237c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6247c478bd9Sstevel@tonic-gate 				add_ugentry(&ruid_tbl, parg);
6257c478bd9Sstevel@tonic-gate 			} while (*p1);
6267c478bd9Sstevel@tonic-gate 			break;
6277c478bd9Sstevel@tonic-gate 		case 'G':		/* real group name or number */
6287c478bd9Sstevel@tonic-gate 			Gflg++;
6297c478bd9Sstevel@tonic-gate 			p1 = optarg;
6307c478bd9Sstevel@tonic-gate 			do {
6317c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6327c478bd9Sstevel@tonic-gate 				add_ugentry(&rgid_tbl, parg);
6337c478bd9Sstevel@tonic-gate 			} while (*p1);
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 		case 'o':		/* output format */
6367c478bd9Sstevel@tonic-gate 			p = optarg;
6377c478bd9Sstevel@tonic-gate 			while ((p = parse_format(p)) != NULL)
6387c478bd9Sstevel@tonic-gate 				;
6397c478bd9Sstevel@tonic-gate 			break;
6407c478bd9Sstevel@tonic-gate 		case 'z':		/* zone name or number */
6417c478bd9Sstevel@tonic-gate 			zflg++;
6427c478bd9Sstevel@tonic-gate 			p1 = optarg;
6437c478bd9Sstevel@tonic-gate 			do {
6447c478bd9Sstevel@tonic-gate 				zoneid_t id;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6477c478bd9Sstevel@tonic-gate 				if (zone_get_id(parg, &id) != 0) {
6487c478bd9Sstevel@tonic-gate 					pgerrflg++;
6497c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6507c478bd9Sstevel@tonic-gate 					    gettext("ps: unknown zone %s\n"),
6517c478bd9Sstevel@tonic-gate 					    parg);
6527c478bd9Sstevel@tonic-gate 					continue;
6537c478bd9Sstevel@tonic-gate 				}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 				if (nzoneid == zoneidsz) {
6567c478bd9Sstevel@tonic-gate 					if ((zoneidsz *= 2) == 0)
6577c478bd9Sstevel@tonic-gate 						zoneidsz = SIZ;
6587c478bd9Sstevel@tonic-gate 					zoneid = Realloc(zoneid,
6597c478bd9Sstevel@tonic-gate 					    zoneidsz * sizeof (zoneid_t));
6607c478bd9Sstevel@tonic-gate 				}
6617c478bd9Sstevel@tonic-gate 				zoneid[nzoneid++] = id;
6627c478bd9Sstevel@tonic-gate 			} while (*p1);
6637c478bd9Sstevel@tonic-gate 			break;
6647c478bd9Sstevel@tonic-gate 		case 'Z':		/* show zone name */
6657c478bd9Sstevel@tonic-gate 			Zflg++;
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		default:			/* error on ? */
6687c478bd9Sstevel@tonic-gate 			errflg++;
6697c478bd9Sstevel@tonic-gate 			break;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (errflg || optind < argc || pgerrflg)
6737c478bd9Sstevel@tonic-gate 		usage();
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (tflg)
6767c478bd9Sstevel@tonic-gate 		tty[ntty].tname = NULL;
6777c478bd9Sstevel@tonic-gate 	/*
6787c478bd9Sstevel@tonic-gate 	 * If an appropriate option has not been specified, use the
6797c478bd9Sstevel@tonic-gate 	 * current terminal and effective uid as the default.
6807c478bd9Sstevel@tonic-gate 	 */
681c6402783Sakolb 	if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
6827c478bd9Sstevel@tonic-gate 		psinfo_t info;
6837c478bd9Sstevel@tonic-gate 		int procfd;
6847c478bd9Sstevel@tonic-gate 		char *name;
6857c478bd9Sstevel@tonic-gate 		char pname[100];
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		/* get our own controlling tty name using /proc */
6887c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname),
6897c478bd9Sstevel@tonic-gate 		    "%s/self/psinfo", procdir);
6907c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0 ||
6917c478bd9Sstevel@tonic-gate 		    read(procfd, (char *)&info, sizeof (info)) < 0 ||
6927c478bd9Sstevel@tonic-gate 		    info.pr_ttydev == PRNODEV) {
6937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6947c478bd9Sstevel@tonic-gate 			    gettext("ps: no controlling terminal\n"));
6957c478bd9Sstevel@tonic-gate 			exit(1);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 		(void) close(procfd);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		i = 0;
7007c478bd9Sstevel@tonic-gate 		name = gettty(&info);
7017c478bd9Sstevel@tonic-gate 		if (*name == '?') {
7027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7037c478bd9Sstevel@tonic-gate 			    gettext("ps: can't find controlling terminal\n"));
7047c478bd9Sstevel@tonic-gate 			exit(1);
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		if (ntty == ttysz) {
7077c478bd9Sstevel@tonic-gate 			if ((ttysz *= 2) == 0)
7087c478bd9Sstevel@tonic-gate 				ttysz = NTTYS;
7097c478bd9Sstevel@tonic-gate 			tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 		tty[ntty].tdev = info.pr_ttydev;
7127c478bd9Sstevel@tonic-gate 		tty[ntty++].tname = name;
7137c478bd9Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7147c478bd9Sstevel@tonic-gate 		tflg++;
7157c478bd9Sstevel@tonic-gate 		tuid = getuid();
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 	if (Aflg) {
7187c478bd9Sstevel@tonic-gate 		Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
719c6402783Sakolb 		zflg = hflg = 0;
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 	if (Aflg | aflg | dflg)
7227c478bd9Sstevel@tonic-gate 		tflg = 0;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	i = 0;		/* prepare to exit on name lookup errors */
7257c478bd9Sstevel@tonic-gate 	i += uconv(&euid_tbl);
7267c478bd9Sstevel@tonic-gate 	i += uconv(&ruid_tbl);
7277c478bd9Sstevel@tonic-gate 	i += gconv(&egid_tbl);
7287c478bd9Sstevel@tonic-gate 	i += gconv(&rgid_tbl);
7297c478bd9Sstevel@tonic-gate 	if (i)
7307c478bd9Sstevel@tonic-gate 		exit(1);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* allocate a buffer for lwpsinfo structures */
7337c478bd9Sstevel@tonic-gate 	lpbufsize = 4096;
7347c478bd9Sstevel@tonic-gate 	if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
7357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7367c478bd9Sstevel@tonic-gate 		    gettext("ps: no memory\n"));
7377c478bd9Sstevel@tonic-gate 		exit(1);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (fields) {	/* print user-specified header */
7417c478bd9Sstevel@tonic-gate 		if (do_header) {
7427c478bd9Sstevel@tonic-gate 			struct field *f;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 			for (f = fields; f != NULL; f = f->next) {
7457c478bd9Sstevel@tonic-gate 				if (f != fields)
7467c478bd9Sstevel@tonic-gate 					(void) printf(" ");
7477c478bd9Sstevel@tonic-gate 				switch (f->fname) {
7487c478bd9Sstevel@tonic-gate 				case F_TTY:
7497c478bd9Sstevel@tonic-gate 					(void) printf("%-*s",
7507c478bd9Sstevel@tonic-gate 					    f->width, f->header);
7517c478bd9Sstevel@tonic-gate 					break;
7527c478bd9Sstevel@tonic-gate 				case F_FNAME:
7537c478bd9Sstevel@tonic-gate 				case F_COMM:
7547c478bd9Sstevel@tonic-gate 				case F_ARGS:
7557c478bd9Sstevel@tonic-gate 					/*
7567c478bd9Sstevel@tonic-gate 					 * Print these headers full width
7577c478bd9Sstevel@tonic-gate 					 * unless they appear at the end.
7587c478bd9Sstevel@tonic-gate 					 */
7597c478bd9Sstevel@tonic-gate 					if (f->next != NULL) {
7607c478bd9Sstevel@tonic-gate 						(void) printf("%-*s",
7617c478bd9Sstevel@tonic-gate 						    f->width, f->header);
7627c478bd9Sstevel@tonic-gate 					} else {
7637c478bd9Sstevel@tonic-gate 						(void) printf("%s",
7647c478bd9Sstevel@tonic-gate 						    f->header);
7657c478bd9Sstevel@tonic-gate 					}
7667c478bd9Sstevel@tonic-gate 					break;
7677c478bd9Sstevel@tonic-gate 				default:
7687c478bd9Sstevel@tonic-gate 					(void) printf("%*s",
7697c478bd9Sstevel@tonic-gate 					    f->width, f->header);
7707c478bd9Sstevel@tonic-gate 					break;
7717c478bd9Sstevel@tonic-gate 				}
7727c478bd9Sstevel@tonic-gate 			}
7737c478bd9Sstevel@tonic-gate 			(void) printf("\n");
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	} else {	/* print standard header */
7767c478bd9Sstevel@tonic-gate 		if (lflg) {
7777c478bd9Sstevel@tonic-gate 			if (yflg)
7787c478bd9Sstevel@tonic-gate 				(void) printf(" S");
7797c478bd9Sstevel@tonic-gate 			else
7807c478bd9Sstevel@tonic-gate 				(void) printf(" F S");
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 		if (Zflg)
7837c478bd9Sstevel@tonic-gate 			(void) printf("    ZONE");
7847c478bd9Sstevel@tonic-gate 		if (fflg) {
7857c478bd9Sstevel@tonic-gate 			if (lflg)
7867c478bd9Sstevel@tonic-gate 				(void) printf(" ");
7877c478bd9Sstevel@tonic-gate 			(void) printf("     UID");
7887c478bd9Sstevel@tonic-gate 		} else if (lflg)
7897c478bd9Sstevel@tonic-gate 			(void) printf("    UID");
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		(void) printf(" %*s", pidwidth,  "PID");
7927c478bd9Sstevel@tonic-gate 		if (lflg || fflg)
7937c478bd9Sstevel@tonic-gate 			(void) printf(" %*s", pidwidth, "PPID");
7947c478bd9Sstevel@tonic-gate 		if (jflg)
7957c478bd9Sstevel@tonic-gate 			(void) printf(" %*s %*s", pidwidth, "PGID",
7967c478bd9Sstevel@tonic-gate 			    pidwidth, "SID");
7977c478bd9Sstevel@tonic-gate 		if (Lflg)
7987c478bd9Sstevel@tonic-gate 			(void) printf("   LWP");
7997c478bd9Sstevel@tonic-gate 		if (Pflg)
8007c478bd9Sstevel@tonic-gate 			(void) printf(" PSR");
8017c478bd9Sstevel@tonic-gate 		if (Lflg && fflg)
8027c478bd9Sstevel@tonic-gate 			(void) printf("  NLWP");
8037c478bd9Sstevel@tonic-gate 		if (cflg)
8047c478bd9Sstevel@tonic-gate 			(void) printf("  CLS PRI");
8057c478bd9Sstevel@tonic-gate 		else if (lflg || fflg) {
8067c478bd9Sstevel@tonic-gate 			(void) printf("   C");
8077c478bd9Sstevel@tonic-gate 			if (lflg)
8087c478bd9Sstevel@tonic-gate 				(void) printf(" PRI NI");
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 		if (lflg) {
8117c478bd9Sstevel@tonic-gate 			if (yflg)
8127c478bd9Sstevel@tonic-gate 				(void) printf("   RSS     SZ    WCHAN");
8137c478bd9Sstevel@tonic-gate 			else
8147c478bd9Sstevel@tonic-gate 				(void) printf("     ADDR     SZ    WCHAN");
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 		if (fflg)
81716f94f58Ssayama 			(void) printf(" %s", loc_stime_str);
818c6402783Sakolb 		if (Hflg)
819c6402783Sakolb 			(void) printf(" LGRP");
8207c478bd9Sstevel@tonic-gate 		if (Lflg)
8217c478bd9Sstevel@tonic-gate 			(void) printf(" TTY        LTIME CMD\n");
8227c478bd9Sstevel@tonic-gate 		else
8237c478bd9Sstevel@tonic-gate 			(void) printf(" TTY         TIME CMD\n");
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 
827c6402783Sakolb 	if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
8287c478bd9Sstevel@tonic-gate 	    npid <= PTHRESHOLD) {
8297c478bd9Sstevel@tonic-gate 		/*
8307c478bd9Sstevel@tonic-gate 		 * If we are looking at specific processes go straight
8317c478bd9Sstevel@tonic-gate 		 * to their /proc entries and don't scan /proc.
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		int i;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		(void) qsort(pid, npid, sizeof (pid_t), pidcmp);
8367c478bd9Sstevel@tonic-gate 		for (i = 0; i < npid; i++) {
8377c478bd9Sstevel@tonic-gate 			char pname[12];
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			if (i >= 1 && pid[i] == pid[i - 1])
8407c478bd9Sstevel@tonic-gate 				continue;
8417c478bd9Sstevel@tonic-gate 			(void) sprintf(pname, "%d", (int)pid[i]);
8427c478bd9Sstevel@tonic-gate 			if (print_proc(pname) == 0)
8437c478bd9Sstevel@tonic-gate 				retcode = 0;
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 	} else {
8467c478bd9Sstevel@tonic-gate 		/*
8477c478bd9Sstevel@tonic-gate 		 * Determine which processes to print info about by searching
8487c478bd9Sstevel@tonic-gate 		 * the /proc directory and looking at each process.
8497c478bd9Sstevel@tonic-gate 		 */
8507c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(procdir)) == NULL) {
8517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8527c478bd9Sstevel@tonic-gate 			    gettext("ps: cannot open PROC directory %s\n"),
8537c478bd9Sstevel@tonic-gate 			    procdir);
8547c478bd9Sstevel@tonic-gate 			exit(1);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		/* for each active process --- */
8587c478bd9Sstevel@tonic-gate 		while (dentp = readdir(dirp)) {
8597c478bd9Sstevel@tonic-gate 			if (dentp->d_name[0] == '.')    /* skip . and .. */
8607c478bd9Sstevel@tonic-gate 				continue;
8617c478bd9Sstevel@tonic-gate 			if (print_proc(dentp->d_name) == 0)
8627c478bd9Sstevel@tonic-gate 				retcode = 0;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	return (retcode);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate int
8727c478bd9Sstevel@tonic-gate print_proc(char *pid_name)
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate 	char	pname[PATH_MAX];
8757c478bd9Sstevel@tonic-gate 	int	pdlen;
8767c478bd9Sstevel@tonic-gate 	int	found;
8777c478bd9Sstevel@tonic-gate 	int	procfd; /* filedescriptor for /proc/nnnnn/psinfo */
8787c478bd9Sstevel@tonic-gate 	char	*tp;    /* ptr to ttyname,  if any */
8797c478bd9Sstevel@tonic-gate 	psinfo_t info;  /* process information from /proc */
8807c478bd9Sstevel@tonic-gate 	lwpsinfo_t *lwpsinfo;   /* array of lwpsinfo structs */
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
8837c478bd9Sstevel@tonic-gate 	if (pdlen >= sizeof (pname) - 10)
8847c478bd9Sstevel@tonic-gate 		return (1);
8857c478bd9Sstevel@tonic-gate retry:
8867c478bd9Sstevel@tonic-gate 	(void) strcpy(&pname[pdlen], "psinfo");
8877c478bd9Sstevel@tonic-gate 	if ((procfd = open(pname, O_RDONLY)) == -1) {
8887c478bd9Sstevel@tonic-gate 		/* Process may have exited meanwhile. */
8897c478bd9Sstevel@tonic-gate 		return (1);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * Get the info structure for the process and close quickly.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	if (read(procfd, (char *)&info, sizeof (info)) < 0) {
8957c478bd9Sstevel@tonic-gate 		int	saverr = errno;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		(void) close(procfd);
8987c478bd9Sstevel@tonic-gate 		if (saverr == EAGAIN)
8997c478bd9Sstevel@tonic-gate 			goto retry;
9007c478bd9Sstevel@tonic-gate 		if (saverr != ENOENT)
9017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9027c478bd9Sstevel@tonic-gate 			    gettext("ps: read() on %s: %s\n"),
9037c478bd9Sstevel@tonic-gate 			    pname, err_string(saverr));
9047c478bd9Sstevel@tonic-gate 		return (1);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	(void) close(procfd);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	found = 0;
9097c478bd9Sstevel@tonic-gate 	if (info.pr_lwp.pr_state == 0)	/* can't happen? */
9107c478bd9Sstevel@tonic-gate 		return (1);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Omit session group leaders for 'a' and 'd' options.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
9167c478bd9Sstevel@tonic-gate 		return (1);
9177c478bd9Sstevel@tonic-gate 	if (Aflg || dflg)
9187c478bd9Sstevel@tonic-gate 		found++;
9197c478bd9Sstevel@tonic-gate 	else if (pflg && search(pid, npid, info.pr_pid))
9207c478bd9Sstevel@tonic-gate 		found++;	/* ppid in p option arg list */
921f48205beScasper 	else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
9227c478bd9Sstevel@tonic-gate 		found++;	/* puid in u option arg list */
923f48205beScasper 	else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
9247c478bd9Sstevel@tonic-gate 		found++;	/* puid in U option arg list */
9257c478bd9Sstevel@tonic-gate #ifdef NOT_YET
926f48205beScasper 	else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
9277c478bd9Sstevel@tonic-gate 		found++;	/* pgid in g option arg list */
9287c478bd9Sstevel@tonic-gate #endif	/* NOT_YET */
929f48205beScasper 	else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
9307c478bd9Sstevel@tonic-gate 		found++;	/* pgid in G option arg list */
9317c478bd9Sstevel@tonic-gate 	else if (gflg && search(grpid, ngrpid, info.pr_pgid))
9327c478bd9Sstevel@tonic-gate 		found++;	/* grpid in g option arg list */
9337c478bd9Sstevel@tonic-gate 	else if (sflg && search(sessid, nsessid, info.pr_sid))
9347c478bd9Sstevel@tonic-gate 		found++;	/* sessid in s option arg list */
9357c478bd9Sstevel@tonic-gate 	else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
9367c478bd9Sstevel@tonic-gate 		found++;	/* zoneid in z option arg list */
937c6402783Sakolb 	else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
938c6402783Sakolb 		found++;	/* home lgroup in h option arg list */
9397c478bd9Sstevel@tonic-gate 	if (!found && !tflg && !aflg)
9407c478bd9Sstevel@tonic-gate 		return (1);
9417c478bd9Sstevel@tonic-gate 	if (!prfind(found, &info, &tp))
9427c478bd9Sstevel@tonic-gate 		return (1);
9437c478bd9Sstevel@tonic-gate 	if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
9447c478bd9Sstevel@tonic-gate 		ssize_t prsz;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		(void) strcpy(&pname[pdlen], "lpsinfo");
9477c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
9487c478bd9Sstevel@tonic-gate 			return (1);
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * Get the info structures for the lwps.
9517c478bd9Sstevel@tonic-gate 		 */
9527c478bd9Sstevel@tonic-gate 		prsz = read(procfd, lpsinfobuf, lpbufsize);
9537c478bd9Sstevel@tonic-gate 		if (prsz == -1) {
9547c478bd9Sstevel@tonic-gate 			int	saverr = errno;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 			(void) close(procfd);
9577c478bd9Sstevel@tonic-gate 			if (saverr == EAGAIN)
9587c478bd9Sstevel@tonic-gate 				goto retry;
9597c478bd9Sstevel@tonic-gate 			if (saverr != ENOENT)
9607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
9617c478bd9Sstevel@tonic-gate 				    gettext("ps: read() on %s: %s\n"),
9627c478bd9Sstevel@tonic-gate 				    pname, err_string(saverr));
9637c478bd9Sstevel@tonic-gate 			return (1);
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		(void) close(procfd);
9667c478bd9Sstevel@tonic-gate 		if (prsz == lpbufsize) {
9677c478bd9Sstevel@tonic-gate 			/*
9687c478bd9Sstevel@tonic-gate 			 * buffer overflow. Realloc new buffer.
9697c478bd9Sstevel@tonic-gate 			 * Error handling is done in Realloc().
9707c478bd9Sstevel@tonic-gate 			 */
9717c478bd9Sstevel@tonic-gate 			lpbufsize *= 2;
9727c478bd9Sstevel@tonic-gate 			lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
9737c478bd9Sstevel@tonic-gate 			goto retry;
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 		if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
9767c478bd9Sstevel@tonic-gate 			goto retry;
9777c478bd9Sstevel@tonic-gate 		lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
9807c478bd9Sstevel@tonic-gate 		prcom(&info, tp);
9817c478bd9Sstevel@tonic-gate 	} else {
9827c478bd9Sstevel@tonic-gate 		int nlwp = 0;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		do {
9857c478bd9Sstevel@tonic-gate 			info.pr_lwp = *lwpsinfo;
9867c478bd9Sstevel@tonic-gate 			prcom(&info, tp);
9877c478bd9Sstevel@tonic-gate 			/* LINTED improper alignment */
9887c478bd9Sstevel@tonic-gate 			lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
989*d21c64a5SDavid Edmondson 			    lpsinfobuf->pr_entsize);
9907c478bd9Sstevel@tonic-gate 		} while (++nlwp < lpsinfobuf->pr_nent);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 	return (0);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate static void
9977c478bd9Sstevel@tonic-gate usage(void)		/* print usage message and quit */
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	static char usage1[] =
1000c6402783Sakolb 	    "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
10017c478bd9Sstevel@tonic-gate 	static char usage2[] =
10027c478bd9Sstevel@tonic-gate 	    "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
10037c478bd9Sstevel@tonic-gate 	static char usage3[] =
1004c6402783Sakolb 	    "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] "
1005c6402783Sakolb 	    "[-h lgrplist]";
10067c478bd9Sstevel@tonic-gate 	static char usage4[] =
10077c478bd9Sstevel@tonic-gate 	    "  'format' is one or more of:";
10087c478bd9Sstevel@tonic-gate 	static char usage5[] =
10097c478bd9Sstevel@tonic-gate 	    "\tuser ruser group rgroup uid ruid gid rgid pid ppid pgid "
10107c478bd9Sstevel@tonic-gate 	    "sid taskid ctid";
10117c478bd9Sstevel@tonic-gate 	static char usage6[] =
10127c478bd9Sstevel@tonic-gate 	    "\tpri opri pcpu pmem vsz rss osz nice class time etime stime zone "
10137c478bd9Sstevel@tonic-gate 	    "zoneid";
10147c478bd9Sstevel@tonic-gate 	static char usage7[] =
10157c478bd9Sstevel@tonic-gate 	    "\tf s c lwp nlwp psr tty addr wchan fname comm args "
1016c6402783Sakolb 	    "projid project pset lgrp";
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
10197c478bd9Sstevel@tonic-gate 	    gettext("usage: %s\n%s\n%s\n%s\n%s\n%s\n%s\n"),
10207c478bd9Sstevel@tonic-gate 	    gettext(usage1), gettext(usage2), gettext(usage3),
10217c478bd9Sstevel@tonic-gate 	    gettext(usage4), gettext(usage5), gettext(usage6), gettext(usage7));
10227c478bd9Sstevel@tonic-gate 	exit(1);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate /*
10267c478bd9Sstevel@tonic-gate  * getarg() finds the next argument in list and copies arg into argbuf.
10277c478bd9Sstevel@tonic-gate  * p1 first pts to arg passed back from getopt routine.  p1 is then
10287c478bd9Sstevel@tonic-gate  * bumped to next character that is not a comma or blank -- p1 NULL
10297c478bd9Sstevel@tonic-gate  * indicates end of list.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate static char *
10327c478bd9Sstevel@tonic-gate getarg(char **pp1)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	static char argbuf[ARGSIZ];
10357c478bd9Sstevel@tonic-gate 	char *p1 = *pp1;
10367c478bd9Sstevel@tonic-gate 	char *parga = argbuf;
10377c478bd9Sstevel@tonic-gate 	int c;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10407c478bd9Sstevel@tonic-gate 		p1++;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
10437c478bd9Sstevel@tonic-gate 		if (parga < argbuf + ARGSIZ - 1)
10447c478bd9Sstevel@tonic-gate 			*parga++ = c;
10457c478bd9Sstevel@tonic-gate 		p1++;
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 	*parga = '\0';
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10507c478bd9Sstevel@tonic-gate 		p1++;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	*pp1 = p1;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	return (argbuf);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate /*
10587c478bd9Sstevel@tonic-gate  * parse_format() takes the argument to the -o option,
10597c478bd9Sstevel@tonic-gate  * sets up the next output field structure, and returns
10607c478bd9Sstevel@tonic-gate  * a pointer to any further output field specifier(s).
10617c478bd9Sstevel@tonic-gate  * As a side-effect, it increments errflg if encounters a format error.
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate static char *
10647c478bd9Sstevel@tonic-gate parse_format(char *arg)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	int c;
10677c478bd9Sstevel@tonic-gate 	char *name;
10687c478bd9Sstevel@tonic-gate 	char *header = NULL;
10697c478bd9Sstevel@tonic-gate 	int width = 0;
10707c478bd9Sstevel@tonic-gate 	struct def_field *df;
10717c478bd9Sstevel@tonic-gate 	struct field *f;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
10747c478bd9Sstevel@tonic-gate 		arg++;
10757c478bd9Sstevel@tonic-gate 	if (c == '\0')
10767c478bd9Sstevel@tonic-gate 		return (NULL);
10777c478bd9Sstevel@tonic-gate 	name = arg;
10787c478bd9Sstevel@tonic-gate 	arg = strpbrk(arg, " \t\r\v\f\n,=");
10797c478bd9Sstevel@tonic-gate 	if (arg != NULL) {
10807c478bd9Sstevel@tonic-gate 		c = *arg;
10817c478bd9Sstevel@tonic-gate 		*arg++ = '\0';
10827c478bd9Sstevel@tonic-gate 		if (c == '=') {
10837c478bd9Sstevel@tonic-gate 			char *s;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 			header = arg;
10867c478bd9Sstevel@tonic-gate 			arg = NULL;
10877c478bd9Sstevel@tonic-gate 			width = strlen(header);
10887c478bd9Sstevel@tonic-gate 			s = header + width;
10897c478bd9Sstevel@tonic-gate 			while (s > header && isspace(*--s))
10907c478bd9Sstevel@tonic-gate 				*s = '\0';
10917c478bd9Sstevel@tonic-gate 			while (isspace(*header))
10927c478bd9Sstevel@tonic-gate 				header++;
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 	for (df = &fname[0]; df < &fname[NFIELDS]; df++)
10967c478bd9Sstevel@tonic-gate 		if (strcmp(name, df->fname) == 0) {
10977c478bd9Sstevel@tonic-gate 			if (strcmp(name, "lwp") == 0)
10987c478bd9Sstevel@tonic-gate 				Lflg++;
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 	if (df >= &fname[NFIELDS]) {
11027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1103*d21c64a5SDavid Edmondson 		    gettext("ps: unknown output format: -o %s\n"),
1104*d21c64a5SDavid Edmondson 		    name);
11057c478bd9Sstevel@tonic-gate 		errflg++;
11067c478bd9Sstevel@tonic-gate 		return (arg);
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 	if ((f = malloc(sizeof (*f))) == NULL) {
11097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11107c478bd9Sstevel@tonic-gate 		    gettext("ps: malloc() for output format failed, %s\n"),
11117c478bd9Sstevel@tonic-gate 		    err_string(errno));
11127c478bd9Sstevel@tonic-gate 		exit(1);
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 	f->next = NULL;
11157c478bd9Sstevel@tonic-gate 	f->fname = df - &fname[0];
11167c478bd9Sstevel@tonic-gate 	f->header = header? header : df->header;
11177c478bd9Sstevel@tonic-gate 	if (width == 0)
11187c478bd9Sstevel@tonic-gate 		width = df->width;
11197c478bd9Sstevel@tonic-gate 	if (*f->header != '\0')
11207c478bd9Sstevel@tonic-gate 		do_header = 1;
11217c478bd9Sstevel@tonic-gate 	f->width = max(width, df->minwidth);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if (fields == NULL)
11247c478bd9Sstevel@tonic-gate 		fields = last_field = f;
11257c478bd9Sstevel@tonic-gate 	else {
11267c478bd9Sstevel@tonic-gate 		last_field->next = f;
11277c478bd9Sstevel@tonic-gate 		last_field = f;
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	return (arg);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate static char *
11347c478bd9Sstevel@tonic-gate devlookup(dev_t ddev)
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate 	struct devl *dp;
11377c478bd9Sstevel@tonic-gate 	int i;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	for (dp = devl, i = 0; i < ndev; dp++, i++) {
11407c478bd9Sstevel@tonic-gate 		if (dp->ddev == ddev)
11417c478bd9Sstevel@tonic-gate 			return (dp->dname);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	return (NULL);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static char *
11477c478bd9Sstevel@tonic-gate devadd(char *name, dev_t ddev)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	struct devl *dp;
11507c478bd9Sstevel@tonic-gate 	int leng, start, i;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (ndev == maxdev) {
11537c478bd9Sstevel@tonic-gate 		maxdev += DNINCR;
11547c478bd9Sstevel@tonic-gate 		devl = Realloc(devl, maxdev * sizeof (struct devl));
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 	dp = &devl[ndev++];
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	dp->ddev = ddev;
11597c478bd9Sstevel@tonic-gate 	if (name == NULL) {
11607c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->dname, "??");
11617c478bd9Sstevel@tonic-gate 		return (dp->dname);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	leng = strlen(name);
11657c478bd9Sstevel@tonic-gate 	/* Strip off /dev/ */
11667c478bd9Sstevel@tonic-gate 	if (leng < DNSIZE + 4)
11677c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->dname, &name[5]);
11687c478bd9Sstevel@tonic-gate 	else {
11697c478bd9Sstevel@tonic-gate 		start = leng - DNSIZE - 1;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		for (i = start; i < leng && name[i] != '/'; i++)
11727c478bd9Sstevel@tonic-gate 				;
11737c478bd9Sstevel@tonic-gate 		if (i == leng)
11747c478bd9Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[start], DNSIZE);
11757c478bd9Sstevel@tonic-gate 		else
11767c478bd9Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[i+1], DNSIZE);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	return (dp->dname);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * gettty returns the user's tty number or ? if none.
11837c478bd9Sstevel@tonic-gate  */
11847c478bd9Sstevel@tonic-gate static char *
11857c478bd9Sstevel@tonic-gate gettty(psinfo_t *psinfo)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	extern char *_ttyname_dev(dev_t, char *, size_t);
11887c478bd9Sstevel@tonic-gate 	char devname[TTYNAME_MAX];
11897c478bd9Sstevel@tonic-gate 	char *retval;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (psinfo->pr_ttydev == PRNODEV)
11927c478bd9Sstevel@tonic-gate 		return ("?");
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
11957c478bd9Sstevel@tonic-gate 		return (retval);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	return (devadd(retval, psinfo->pr_ttydev));
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * Find the process's tty and return 1 if process is to be printed.
12047c478bd9Sstevel@tonic-gate  */
12057c478bd9Sstevel@tonic-gate static int
12067c478bd9Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate 	char	*tp;
12097c478bd9Sstevel@tonic-gate 	struct tty *ttyp;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
12127c478bd9Sstevel@tonic-gate 		/* process is a zombie */
12137c478bd9Sstevel@tonic-gate 		*tpp = "?";
12147c478bd9Sstevel@tonic-gate 		if (tflg && !found)
12157c478bd9Sstevel@tonic-gate 			return (0);
12167c478bd9Sstevel@tonic-gate 		return (1);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	/*
12207c478bd9Sstevel@tonic-gate 	 * Get current terminal.  If none ("?") and 'a' is set, don't print
12217c478bd9Sstevel@tonic-gate 	 * info.  If 't' is set, check if term is in list of desired terminals
12227c478bd9Sstevel@tonic-gate 	 * and print it if it is.
12237c478bd9Sstevel@tonic-gate 	 */
12247c478bd9Sstevel@tonic-gate 	tp = gettty(psinfo);
12257c478bd9Sstevel@tonic-gate 	if (aflg && *tp == '?') {
12267c478bd9Sstevel@tonic-gate 		*tpp = tp;
12277c478bd9Sstevel@tonic-gate 		return (0);
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 	if (tflg && !found) {
12307c478bd9Sstevel@tonic-gate 		int match = 0;
12317c478bd9Sstevel@tonic-gate 		char *other = NULL;
12327c478bd9Sstevel@tonic-gate 		for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
12337c478bd9Sstevel@tonic-gate 			/*
12347c478bd9Sstevel@tonic-gate 			 * Look for a name match
12357c478bd9Sstevel@tonic-gate 			 */
12367c478bd9Sstevel@tonic-gate 			if (strcmp(tp, ttyp->tname) == 0) {
12377c478bd9Sstevel@tonic-gate 				match = 1;
12387c478bd9Sstevel@tonic-gate 				break;
12397c478bd9Sstevel@tonic-gate 			}
12407c478bd9Sstevel@tonic-gate 			/*
12417c478bd9Sstevel@tonic-gate 			 * Look for same device under different names.
12427c478bd9Sstevel@tonic-gate 			 */
12437c478bd9Sstevel@tonic-gate 			if ((other == NULL) &&
12447c478bd9Sstevel@tonic-gate 			    (ttyp->tdev != PRNODEV) &&
12457c478bd9Sstevel@tonic-gate 			    (psinfo->pr_ttydev == ttyp->tdev))
12467c478bd9Sstevel@tonic-gate 				other = ttyp->tname;
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 		if (!match && (other != NULL)) {
12497c478bd9Sstevel@tonic-gate 			/*
12507c478bd9Sstevel@tonic-gate 			 * found under a different name
12517c478bd9Sstevel@tonic-gate 			 */
12527c478bd9Sstevel@tonic-gate 			match = 1;
12537c478bd9Sstevel@tonic-gate 			tp = other;
12547c478bd9Sstevel@tonic-gate 		}
1255f48205beScasper 		if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
12567c478bd9Sstevel@tonic-gate 			/*
12577c478bd9Sstevel@tonic-gate 			 * not found OR not matching euid
12587c478bd9Sstevel@tonic-gate 			 */
12597c478bd9Sstevel@tonic-gate 			*tpp = tp;
12607c478bd9Sstevel@tonic-gate 			return (0);
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 	*tpp = tp;
12647c478bd9Sstevel@tonic-gate 	return (1);
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate /*
12687c478bd9Sstevel@tonic-gate  * Print info about the process.
12697c478bd9Sstevel@tonic-gate  */
12707c478bd9Sstevel@tonic-gate static void
12717c478bd9Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	char	*cp;
12747c478bd9Sstevel@tonic-gate 	long	tm;
12757c478bd9Sstevel@tonic-gate 	int	bytesleft;
12767c478bd9Sstevel@tonic-gate 	int	wcnt, length;
12777c478bd9Sstevel@tonic-gate 	wchar_t	wchar;
12787c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
12797c478bd9Sstevel@tonic-gate 	int	zombie_lwp;
12807c478bd9Sstevel@tonic-gate 	char	zonename[ZONENAME_MAX];
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * If process is zombie, call zombie print routine and return.
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
12867c478bd9Sstevel@tonic-gate 		if (fields != NULL)
12877c478bd9Sstevel@tonic-gate 			pr_fields(psinfo, ttyp, print_zombie_field);
12887c478bd9Sstevel@tonic-gate 		else
12897c478bd9Sstevel@tonic-gate 			przom(psinfo);
12907c478bd9Sstevel@tonic-gate 		return;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * If user specified '-o format', print requested fields and return.
12977c478bd9Sstevel@tonic-gate 	 */
12987c478bd9Sstevel@tonic-gate 	if (fields != NULL) {
12997c478bd9Sstevel@tonic-gate 		pr_fields(psinfo, ttyp, print_field);
13007c478bd9Sstevel@tonic-gate 		return;
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
13057c478bd9Sstevel@tonic-gate 	 * spearator, rather than keeping track of which column is first.  All
13067c478bd9Sstevel@tonic-gate 	 * other fields are printed with a leading space.
13077c478bd9Sstevel@tonic-gate 	 */
13087c478bd9Sstevel@tonic-gate 	if (lflg) {
13097c478bd9Sstevel@tonic-gate 		if (!yflg)
13107c478bd9Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
13117c478bd9Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (Zflg) {						/* ZONE */
13157c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
13167c478bd9Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
13177c478bd9Sstevel@tonic-gate 			(void) printf("%7.7d ", ((int)psinfo->pr_zoneid));
13187c478bd9Sstevel@tonic-gate 		} else {
13197c478bd9Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
13207c478bd9Sstevel@tonic-gate 		}
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if (fflg) {						/* UID */
13247c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
13257c478bd9Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
13267c478bd9Sstevel@tonic-gate 		else
1327f48205beScasper 			(void) printf("%7.7u ", psinfo->pr_euid);
13287c478bd9Sstevel@tonic-gate 	} else if (lflg) {
1329f48205beScasper 		(void) printf("%6u ", psinfo->pr_euid);
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
13327c478bd9Sstevel@tonic-gate 	if (lflg || fflg)
13337c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13347c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_ppid); /* PPID */
13357c478bd9Sstevel@tonic-gate 	if (jflg) {
13367c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13377c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);	/* PGID */
13387c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13397c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_sid);	/* SID  */
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 	if (Lflg)
13427c478bd9Sstevel@tonic-gate 		(void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
13437c478bd9Sstevel@tonic-gate 	if (Pflg) {
13447c478bd9Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE)	/* PSR */
13457c478bd9Sstevel@tonic-gate 			(void) printf("   -");
13467c478bd9Sstevel@tonic-gate 		else
13477c478bd9Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	if (Lflg && fflg)					/* NLWP */
13507c478bd9Sstevel@tonic-gate 		(void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
13517c478bd9Sstevel@tonic-gate 	if (cflg) {
13527c478bd9Sstevel@tonic-gate 		if (zombie_lwp)					/* CLS */
13537c478bd9Sstevel@tonic-gate 			(void) printf("     ");
13547c478bd9Sstevel@tonic-gate 		else
13557c478bd9Sstevel@tonic-gate 			(void) printf(" %4s", psinfo->pr_lwp.pr_clname);
13567c478bd9Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI */
13577c478bd9Sstevel@tonic-gate 	} else if (lflg || fflg) {
13587c478bd9Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
13597c478bd9Sstevel@tonic-gate 		if (lflg) {					    /* PRI NI */
13607c478bd9Sstevel@tonic-gate 			/*
13617c478bd9Sstevel@tonic-gate 			 * Print priorities the old way (lower numbers
13627c478bd9Sstevel@tonic-gate 			 * mean higher priority) and print nice value
13637c478bd9Sstevel@tonic-gate 			 * for time sharing procs.
13647c478bd9Sstevel@tonic-gate 			 */
13657c478bd9Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
13667c478bd9Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_oldpri != 0)
13677c478bd9Sstevel@tonic-gate 				(void) printf(" %2d", psinfo->pr_lwp.pr_nice);
13687c478bd9Sstevel@tonic-gate 			else
13697c478bd9Sstevel@tonic-gate 				(void) printf(" %2.2s",
13707c478bd9Sstevel@tonic-gate 				    psinfo->pr_lwp.pr_clname);
13717c478bd9Sstevel@tonic-gate 		}
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 	if (lflg) {
13747c478bd9Sstevel@tonic-gate 		if (yflg) {
13757c478bd9Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* RSS */
13767c478bd9Sstevel@tonic-gate 				(void) printf("     0");
13777c478bd9Sstevel@tonic-gate 			else if (psinfo->pr_rssize)
13787c478bd9Sstevel@tonic-gate 				(void) printf(" %5lu",
1379*d21c64a5SDavid Edmondson 				    (ulong_t)psinfo->pr_rssize);
13807c478bd9Sstevel@tonic-gate 			else
13817c478bd9Sstevel@tonic-gate 				(void) printf("     ?");
13827c478bd9Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
13837c478bd9Sstevel@tonic-gate 				(void) printf("      0");
13847c478bd9Sstevel@tonic-gate 			else if (psinfo->pr_size)
13857c478bd9Sstevel@tonic-gate 				(void) printf(" %6lu",
1386*d21c64a5SDavid Edmondson 				    (ulong_t)psinfo->pr_size);
13877c478bd9Sstevel@tonic-gate 			else
13887c478bd9Sstevel@tonic-gate 				(void) printf("      ?");
13897c478bd9Sstevel@tonic-gate 		} else {
13907c478bd9Sstevel@tonic-gate #ifndef _LP64
13917c478bd9Sstevel@tonic-gate 			if (psinfo->pr_addr)			/* ADDR */
13927c478bd9Sstevel@tonic-gate 				(void) printf(" %8lx",
1393*d21c64a5SDavid Edmondson 				    (ulong_t)psinfo->pr_addr);
13947c478bd9Sstevel@tonic-gate 			else
13957c478bd9Sstevel@tonic-gate #endif
13967c478bd9Sstevel@tonic-gate 				(void) printf("        ?");
13977c478bd9Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
13987c478bd9Sstevel@tonic-gate 				(void) printf("      0");
13997c478bd9Sstevel@tonic-gate 			else if (psinfo->pr_size)
14007c478bd9Sstevel@tonic-gate 				(void) printf(" %6lu",
14017c478bd9Sstevel@tonic-gate 				    (ulong_t)psinfo->pr_size / kbytes_per_page);
14027c478bd9Sstevel@tonic-gate 			else
14037c478bd9Sstevel@tonic-gate 				(void) printf("      ?");
14047c478bd9Sstevel@tonic-gate 		}
14057c478bd9Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_sname != 'S')		/* WCHAN */
14067c478bd9Sstevel@tonic-gate 			(void) printf("         ");
14077c478bd9Sstevel@tonic-gate #ifndef _LP64
14087c478bd9Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_wchan)
14097c478bd9Sstevel@tonic-gate 			(void) printf(" %8lx",
1410*d21c64a5SDavid Edmondson 			    (ulong_t)psinfo->pr_lwp.pr_wchan);
14117c478bd9Sstevel@tonic-gate #endif
14127c478bd9Sstevel@tonic-gate 		else
14137c478bd9Sstevel@tonic-gate 			(void) printf("        ?");
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 	if (fflg) {						/* STIME */
141616f94f58Ssayama 		int width = fname[F_STIME].width;
14177c478bd9Sstevel@tonic-gate 		if (Lflg)
141816f94f58Ssayama 			prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
14197c478bd9Sstevel@tonic-gate 		else
142016f94f58Ssayama 			prtime(psinfo->pr_start, width + 1, 1);
14217c478bd9Sstevel@tonic-gate 	}
1422c6402783Sakolb 
1423c6402783Sakolb 	if (Hflg) {
1424c6402783Sakolb 		/* Display home lgroup */
1425c6402783Sakolb 		(void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
1426c6402783Sakolb 	}
1427c6402783Sakolb 
14287c478bd9Sstevel@tonic-gate 	(void) printf(" %-8.14s", ttyp);			/* TTY */
14297c478bd9Sstevel@tonic-gate 	if (Lflg) {
14307c478bd9Sstevel@tonic-gate 		tm = psinfo->pr_lwp.pr_time.tv_sec;
14317c478bd9Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
14327c478bd9Sstevel@tonic-gate 			tm++;
14337c478bd9Sstevel@tonic-gate 	} else {
14347c478bd9Sstevel@tonic-gate 		tm = psinfo->pr_time.tv_sec;
14357c478bd9Sstevel@tonic-gate 		if (psinfo->pr_time.tv_nsec > 500000000)
14367c478bd9Sstevel@tonic-gate 			tm++;
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);		/* [L]TIME */
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (zombie_lwp) {
14417c478bd9Sstevel@tonic-gate 		(void) printf(" <defunct>\n");
14427c478bd9Sstevel@tonic-gate 		return;
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (!fflg) {						/* CMD */
14467c478bd9Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, 8);
14477c478bd9Sstevel@tonic-gate 		(void) printf(" %.*s\n", wcnt, psinfo->pr_fname);
14487c478bd9Sstevel@tonic-gate 		return;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 
1451c6402783Sakolb 
14527c478bd9Sstevel@tonic-gate 	/*
14537c478bd9Sstevel@tonic-gate 	 * PRARGSZ == length of cmd arg string.
14547c478bd9Sstevel@tonic-gate 	 */
14557c478bd9Sstevel@tonic-gate 	psinfo->pr_psargs[PRARGSZ-1] = '\0';
14567c478bd9Sstevel@tonic-gate 	bytesleft = PRARGSZ;
14577c478bd9Sstevel@tonic-gate 	for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
14587c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
14597c478bd9Sstevel@tonic-gate 		if (length == 0)
14607c478bd9Sstevel@tonic-gate 			break;
14617c478bd9Sstevel@tonic-gate 		if (length < 0 || !iswprint(wchar)) {
14627c478bd9Sstevel@tonic-gate 			if (length < 0)
14637c478bd9Sstevel@tonic-gate 				length = 1;
14647c478bd9Sstevel@tonic-gate 			if (bytesleft <= length) {
14657c478bd9Sstevel@tonic-gate 				*cp = '\0';
14667c478bd9Sstevel@tonic-gate 				break;
14677c478bd9Sstevel@tonic-gate 			}
14687c478bd9Sstevel@tonic-gate 			/* omit the unprintable character */
14697c478bd9Sstevel@tonic-gate 			(void) memmove(cp, cp+length, bytesleft-length);
14707c478bd9Sstevel@tonic-gate 			length = 0;
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 		bytesleft -= length;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 	wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ);
14757c478bd9Sstevel@tonic-gate 	(void) printf(" %.*s\n", wcnt, psinfo->pr_psargs);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate  * Print percent from 16-bit binary fraction [0 .. 1]
14807c478bd9Sstevel@tonic-gate  * Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
14817c478bd9Sstevel@tonic-gate  */
14827c478bd9Sstevel@tonic-gate static void
14837c478bd9Sstevel@tonic-gate prtpct(ushort_t pct, int width)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	uint_t value = pct;	/* need 32 bits to compute with */
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	value = ((value * 1000) + 0x7000) >> 15;	/* [0 .. 1000] */
14887c478bd9Sstevel@tonic-gate 	if (value >= 1000)
14897c478bd9Sstevel@tonic-gate 		value = 999;
14907c478bd9Sstevel@tonic-gate 	if ((width -= 2) < 2)
14917c478bd9Sstevel@tonic-gate 		width = 2;
14927c478bd9Sstevel@tonic-gate 	(void) printf("%*u.%u", width, value / 10, value % 10);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate static void
14967c478bd9Sstevel@tonic-gate print_time(time_t tim, int width)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	char buf[30];
14997c478bd9Sstevel@tonic-gate 	time_t seconds;
15007c478bd9Sstevel@tonic-gate 	time_t minutes;
15017c478bd9Sstevel@tonic-gate 	time_t hours;
15027c478bd9Sstevel@tonic-gate 	time_t days;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (tim < 0) {
15057c478bd9Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
15067c478bd9Sstevel@tonic-gate 		return;
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	seconds = tim % 60;
15107c478bd9Sstevel@tonic-gate 	tim /= 60;
15117c478bd9Sstevel@tonic-gate 	minutes = tim % 60;
15127c478bd9Sstevel@tonic-gate 	tim /= 60;
15137c478bd9Sstevel@tonic-gate 	hours = tim % 24;
15147c478bd9Sstevel@tonic-gate 	days = tim / 24;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (days > 0) {
15177c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
15187c478bd9Sstevel@tonic-gate 		    days, hours, minutes, seconds);
15197c478bd9Sstevel@tonic-gate 	} else if (hours > 0) {
15207c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
15217c478bd9Sstevel@tonic-gate 		    hours, minutes, seconds);
15227c478bd9Sstevel@tonic-gate 	} else {
15237c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
15247c478bd9Sstevel@tonic-gate 		    minutes, seconds);
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	(void) printf("%*s", width, buf);
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate static void
15317c478bd9Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	int width = f->width;
15347c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
15357c478bd9Sstevel@tonic-gate 	struct group *grp;
15367c478bd9Sstevel@tonic-gate 	time_t cputime;
15377c478bd9Sstevel@tonic-gate 	int bytesleft;
15387c478bd9Sstevel@tonic-gate 	int wcnt;
15397c478bd9Sstevel@tonic-gate 	wchar_t	wchar;
15407c478bd9Sstevel@tonic-gate 	char *cp;
15417c478bd9Sstevel@tonic-gate 	int length;
15427c478bd9Sstevel@tonic-gate 	ulong_t mask;
15437c478bd9Sstevel@tonic-gate 	char c, *csave;
15447c478bd9Sstevel@tonic-gate 	int zombie_lwp;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	switch (f->fname) {
15497c478bd9Sstevel@tonic-gate 	case F_RUSER:
15507c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_uid)) != NULL)
15517c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
15527c478bd9Sstevel@tonic-gate 		else
1553f48205beScasper 			(void) printf("%*u", width, psinfo->pr_uid);
15547c478bd9Sstevel@tonic-gate 		break;
15557c478bd9Sstevel@tonic-gate 	case F_USER:
15567c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
15577c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
15587c478bd9Sstevel@tonic-gate 		else
1559f48205beScasper 			(void) printf("%*u", width, psinfo->pr_euid);
15607c478bd9Sstevel@tonic-gate 		break;
15617c478bd9Sstevel@tonic-gate 	case F_RGROUP:
15627c478bd9Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
15637c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
15647c478bd9Sstevel@tonic-gate 		else
1565f48205beScasper 			(void) printf("%*u", width, psinfo->pr_gid);
15667c478bd9Sstevel@tonic-gate 		break;
15677c478bd9Sstevel@tonic-gate 	case F_GROUP:
15687c478bd9Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
15697c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
15707c478bd9Sstevel@tonic-gate 		else
1571f48205beScasper 			(void) printf("%*u", width, psinfo->pr_egid);
15727c478bd9Sstevel@tonic-gate 		break;
15737c478bd9Sstevel@tonic-gate 	case F_RUID:
1574f48205beScasper 		(void) printf("%*u", width, psinfo->pr_uid);
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	case F_UID:
1577f48205beScasper 		(void) printf("%*u", width, psinfo->pr_euid);
15787c478bd9Sstevel@tonic-gate 		break;
15797c478bd9Sstevel@tonic-gate 	case F_RGID:
1580f48205beScasper 		(void) printf("%*u", width, psinfo->pr_gid);
15817c478bd9Sstevel@tonic-gate 		break;
15827c478bd9Sstevel@tonic-gate 	case F_GID:
1583f48205beScasper 		(void) printf("%*u", width, psinfo->pr_egid);
15847c478bd9Sstevel@tonic-gate 		break;
15857c478bd9Sstevel@tonic-gate 	case F_PID:
15867c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pid);
15877c478bd9Sstevel@tonic-gate 		break;
15887c478bd9Sstevel@tonic-gate 	case F_PPID:
15897c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_ppid);
15907c478bd9Sstevel@tonic-gate 		break;
15917c478bd9Sstevel@tonic-gate 	case F_PGID:
15927c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pgid);
15937c478bd9Sstevel@tonic-gate 		break;
15947c478bd9Sstevel@tonic-gate 	case F_SID:
15957c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_sid);
15967c478bd9Sstevel@tonic-gate 		break;
15977c478bd9Sstevel@tonic-gate 	case F_PSR:
15987c478bd9Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
15997c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16007c478bd9Sstevel@tonic-gate 		else
16017c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
16027c478bd9Sstevel@tonic-gate 		break;
16037c478bd9Sstevel@tonic-gate 	case F_LWP:
16047c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
16057c478bd9Sstevel@tonic-gate 		break;
16067c478bd9Sstevel@tonic-gate 	case F_NLWP:
16077c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
16087c478bd9Sstevel@tonic-gate 		break;
16097c478bd9Sstevel@tonic-gate 	case F_OPRI:
16107c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16117c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16127c478bd9Sstevel@tonic-gate 		else
16137c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
16147c478bd9Sstevel@tonic-gate 		break;
16157c478bd9Sstevel@tonic-gate 	case F_PRI:
16167c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16177c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16187c478bd9Sstevel@tonic-gate 		else
16197c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
16207c478bd9Sstevel@tonic-gate 		break;
16217c478bd9Sstevel@tonic-gate 	case F_F:
16227c478bd9Sstevel@tonic-gate 		mask = 0xffffffffUL;
16237c478bd9Sstevel@tonic-gate 		if (width < 8)
16247c478bd9Sstevel@tonic-gate 			mask >>= (8 - width) * 4;
16257c478bd9Sstevel@tonic-gate 		(void) printf("%*lx", width, psinfo->pr_flag & mask);
16267c478bd9Sstevel@tonic-gate 		break;
16277c478bd9Sstevel@tonic-gate 	case F_S:
16287c478bd9Sstevel@tonic-gate 		(void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
16297c478bd9Sstevel@tonic-gate 		break;
16307c478bd9Sstevel@tonic-gate 	case F_C:
16317c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16327c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16337c478bd9Sstevel@tonic-gate 		else
16347c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
16357c478bd9Sstevel@tonic-gate 		break;
16367c478bd9Sstevel@tonic-gate 	case F_PCPU:
16377c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16387c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16397c478bd9Sstevel@tonic-gate 		else if (Lflg)
16407c478bd9Sstevel@tonic-gate 			prtpct(psinfo->pr_lwp.pr_pctcpu, width);
16417c478bd9Sstevel@tonic-gate 		else
16427c478bd9Sstevel@tonic-gate 			prtpct(psinfo->pr_pctcpu, width);
16437c478bd9Sstevel@tonic-gate 		break;
16447c478bd9Sstevel@tonic-gate 	case F_PMEM:
16457c478bd9Sstevel@tonic-gate 		prtpct(psinfo->pr_pctmem, width);
16467c478bd9Sstevel@tonic-gate 		break;
16477c478bd9Sstevel@tonic-gate 	case F_OSZ:
16487c478bd9Sstevel@tonic-gate 		(void) printf("%*lu", width,
1649*d21c64a5SDavid Edmondson 		    (ulong_t)psinfo->pr_size / kbytes_per_page);
16507c478bd9Sstevel@tonic-gate 		break;
16517c478bd9Sstevel@tonic-gate 	case F_VSZ:
16527c478bd9Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
16537c478bd9Sstevel@tonic-gate 		break;
16547c478bd9Sstevel@tonic-gate 	case F_RSS:
16557c478bd9Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
16567c478bd9Sstevel@tonic-gate 		break;
16577c478bd9Sstevel@tonic-gate 	case F_NICE:
16587c478bd9Sstevel@tonic-gate 		/* if pr_oldpri is zero, then this class has no nice */
16597c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16607c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16617c478bd9Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_oldpri != 0)
16627c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
16637c478bd9Sstevel@tonic-gate 		else
16647c478bd9Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
1665*d21c64a5SDavid Edmondson 			    psinfo->pr_lwp.pr_clname);
16667c478bd9Sstevel@tonic-gate 		break;
16677c478bd9Sstevel@tonic-gate 	case F_CLASS:
16687c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
16697c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16707c478bd9Sstevel@tonic-gate 		else
16717c478bd9Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
1672*d21c64a5SDavid Edmondson 			    psinfo->pr_lwp.pr_clname);
16737c478bd9Sstevel@tonic-gate 		break;
16747c478bd9Sstevel@tonic-gate 	case F_STIME:
16757c478bd9Sstevel@tonic-gate 		if (Lflg)
16767c478bd9Sstevel@tonic-gate 			prtime(psinfo->pr_lwp.pr_start, width, 0);
16777c478bd9Sstevel@tonic-gate 		else
16787c478bd9Sstevel@tonic-gate 			prtime(psinfo->pr_start, width, 0);
16797c478bd9Sstevel@tonic-gate 		break;
16807c478bd9Sstevel@tonic-gate 	case F_ETIME:
16817c478bd9Sstevel@tonic-gate 		if (Lflg)
16827c478bd9Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_lwp.pr_start),
1683*d21c64a5SDavid Edmondson 			    width);
16847c478bd9Sstevel@tonic-gate 		else
16857c478bd9Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_start), width);
16867c478bd9Sstevel@tonic-gate 		break;
16877c478bd9Sstevel@tonic-gate 	case F_TIME:
16887c478bd9Sstevel@tonic-gate 		if (Lflg) {
16897c478bd9Sstevel@tonic-gate 			cputime = psinfo->pr_lwp.pr_time.tv_sec;
16907c478bd9Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
16917c478bd9Sstevel@tonic-gate 				cputime++;
16927c478bd9Sstevel@tonic-gate 		} else {
16937c478bd9Sstevel@tonic-gate 			cputime = psinfo->pr_time.tv_sec;
16947c478bd9Sstevel@tonic-gate 			if (psinfo->pr_time.tv_nsec > 500000000)
16957c478bd9Sstevel@tonic-gate 				cputime++;
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 		print_time(cputime, width);
16987c478bd9Sstevel@tonic-gate 		break;
16997c478bd9Sstevel@tonic-gate 	case F_TTY:
17007c478bd9Sstevel@tonic-gate 		(void) printf("%-*s", width, ttyp);
17017c478bd9Sstevel@tonic-gate 		break;
17027c478bd9Sstevel@tonic-gate 	case F_ADDR:
17037c478bd9Sstevel@tonic-gate 		if (zombie_lwp)
17047c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
17057c478bd9Sstevel@tonic-gate 		else if (Lflg)
17067c478bd9Sstevel@tonic-gate 			(void) printf("%*lx", width,
1707*d21c64a5SDavid Edmondson 			    (long)psinfo->pr_lwp.pr_addr);
17087c478bd9Sstevel@tonic-gate 		else
17097c478bd9Sstevel@tonic-gate 			(void) printf("%*lx", width, (long)psinfo->pr_addr);
17107c478bd9Sstevel@tonic-gate 		break;
17117c478bd9Sstevel@tonic-gate 	case F_WCHAN:
17127c478bd9Sstevel@tonic-gate 		if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
17137c478bd9Sstevel@tonic-gate 			(void) printf("%*lx", width,
1714*d21c64a5SDavid Edmondson 			    (long)psinfo->pr_lwp.pr_wchan);
17157c478bd9Sstevel@tonic-gate 		else
17167c478bd9Sstevel@tonic-gate 			(void) printf("%*.*s", width, width, "-");
17177c478bd9Sstevel@tonic-gate 		break;
17187c478bd9Sstevel@tonic-gate 	case F_FNAME:
17197c478bd9Sstevel@tonic-gate 		/*
17207c478bd9Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
17217c478bd9Sstevel@tonic-gate 		 */
17227c478bd9Sstevel@tonic-gate 		if (zombie_lwp) {
17237c478bd9Sstevel@tonic-gate 			if (f->next != NULL)
17247c478bd9Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17257c478bd9Sstevel@tonic-gate 			else
17267c478bd9Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17277c478bd9Sstevel@tonic-gate 			break;
17287c478bd9Sstevel@tonic-gate 		}
17297c478bd9Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, width);
17307c478bd9Sstevel@tonic-gate 		if (f->next != NULL)
17317c478bd9Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
17327c478bd9Sstevel@tonic-gate 		else
17337c478bd9Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, psinfo->pr_fname);
17347c478bd9Sstevel@tonic-gate 		break;
17357c478bd9Sstevel@tonic-gate 	case F_COMM:
17367c478bd9Sstevel@tonic-gate 		if (zombie_lwp) {
17377c478bd9Sstevel@tonic-gate 			if (f->next != NULL)
17387c478bd9Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17397c478bd9Sstevel@tonic-gate 			else
17407c478bd9Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17417c478bd9Sstevel@tonic-gate 			break;
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 		csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n");
17447c478bd9Sstevel@tonic-gate 		if (csave) {
17457c478bd9Sstevel@tonic-gate 			c = *csave;
17467c478bd9Sstevel@tonic-gate 			*csave = '\0';
17477c478bd9Sstevel@tonic-gate 		}
17487c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
17497c478bd9Sstevel@tonic-gate 	case F_ARGS:
17507c478bd9Sstevel@tonic-gate 		/*
17517c478bd9Sstevel@tonic-gate 		 * PRARGSZ == length of cmd arg string.
17527c478bd9Sstevel@tonic-gate 		 */
17537c478bd9Sstevel@tonic-gate 		if (zombie_lwp) {
17547c478bd9Sstevel@tonic-gate 			(void) printf("%-*s", width, "<defunct>");
17557c478bd9Sstevel@tonic-gate 			break;
17567c478bd9Sstevel@tonic-gate 		}
17577c478bd9Sstevel@tonic-gate 		psinfo->pr_psargs[PRARGSZ-1] = '\0';
17587c478bd9Sstevel@tonic-gate 		bytesleft = PRARGSZ;
17597c478bd9Sstevel@tonic-gate 		for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
17607c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, cp, MB_LEN_MAX);
17617c478bd9Sstevel@tonic-gate 			if (length == 0)
17627c478bd9Sstevel@tonic-gate 				break;
17637c478bd9Sstevel@tonic-gate 			if (length < 0 || !iswprint(wchar)) {
17647c478bd9Sstevel@tonic-gate 				if (length < 0)
17657c478bd9Sstevel@tonic-gate 					length = 1;
17667c478bd9Sstevel@tonic-gate 				if (bytesleft <= length) {
17677c478bd9Sstevel@tonic-gate 					*cp = '\0';
17687c478bd9Sstevel@tonic-gate 					break;
17697c478bd9Sstevel@tonic-gate 				}
17707c478bd9Sstevel@tonic-gate 				/* omit the unprintable character */
17717c478bd9Sstevel@tonic-gate 				(void) memmove(cp, cp+length, bytesleft-length);
17727c478bd9Sstevel@tonic-gate 				length = 0;
17737c478bd9Sstevel@tonic-gate 			}
17747c478bd9Sstevel@tonic-gate 			bytesleft -= length;
17757c478bd9Sstevel@tonic-gate 		}
17767c478bd9Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width);
17777c478bd9Sstevel@tonic-gate 		/*
17787c478bd9Sstevel@tonic-gate 		 * Print full width unless this is the last format.
17797c478bd9Sstevel@tonic-gate 		 */
17807c478bd9Sstevel@tonic-gate 		if (f->next != NULL)
17817c478bd9Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt,
17827c478bd9Sstevel@tonic-gate 			    psinfo->pr_psargs);
17837c478bd9Sstevel@tonic-gate 		else
17847c478bd9Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt,
17857c478bd9Sstevel@tonic-gate 			    psinfo->pr_psargs);
17867c478bd9Sstevel@tonic-gate 		if (f->fname == F_COMM && csave)
17877c478bd9Sstevel@tonic-gate 			*csave = c;
17887c478bd9Sstevel@tonic-gate 		break;
17897c478bd9Sstevel@tonic-gate 	case F_TASKID:
17907c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_taskid);
17917c478bd9Sstevel@tonic-gate 		break;
17927c478bd9Sstevel@tonic-gate 	case F_PROJID:
17937c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_projid);
17947c478bd9Sstevel@tonic-gate 		break;
17957c478bd9Sstevel@tonic-gate 	case F_PROJECT:
17967c478bd9Sstevel@tonic-gate 		{
17977c478bd9Sstevel@tonic-gate 			struct project cproj;
17987c478bd9Sstevel@tonic-gate 			char proj_buf[PROJECT_BUFSZ];
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 			if ((getprojbyid(psinfo->pr_projid, &cproj,
18017c478bd9Sstevel@tonic-gate 			    (void *)&proj_buf, PROJECT_BUFSZ)) == NULL)
18027c478bd9Sstevel@tonic-gate 				(void) printf("%*d", width,
18037c478bd9Sstevel@tonic-gate 				    (int)psinfo->pr_projid);
18047c478bd9Sstevel@tonic-gate 			else
18057c478bd9Sstevel@tonic-gate 				(void) printf("%*s", width,
18067c478bd9Sstevel@tonic-gate 				    (cproj.pj_name != NULL) ?
18077c478bd9Sstevel@tonic-gate 				    cproj.pj_name : "---");
18087c478bd9Sstevel@tonic-gate 		}
18097c478bd9Sstevel@tonic-gate 		break;
18107c478bd9Sstevel@tonic-gate 	case F_PSET:
18117c478bd9Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
18127c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18137c478bd9Sstevel@tonic-gate 		else
18147c478bd9Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
18157c478bd9Sstevel@tonic-gate 		break;
18167c478bd9Sstevel@tonic-gate 	case F_ZONEID:
18177c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_zoneid);
18187c478bd9Sstevel@tonic-gate 		break;
18197c478bd9Sstevel@tonic-gate 	case F_ZONE:
18207c478bd9Sstevel@tonic-gate 		{
18217c478bd9Sstevel@tonic-gate 			char zonename[ZONENAME_MAX];
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 			if (getzonenamebyid(psinfo->pr_zoneid, zonename,
18247c478bd9Sstevel@tonic-gate 			    sizeof (zonename)) < 0) {
18257c478bd9Sstevel@tonic-gate 				(void) printf("%*d", width,
18267c478bd9Sstevel@tonic-gate 				    ((int)psinfo->pr_zoneid));
18277c478bd9Sstevel@tonic-gate 			} else {
18287c478bd9Sstevel@tonic-gate 				(void) printf("%*s", width, zonename);
18297c478bd9Sstevel@tonic-gate 			}
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		break;
18327c478bd9Sstevel@tonic-gate 	case F_CTID:
18337c478bd9Sstevel@tonic-gate 		if (psinfo->pr_contract == -1)
18347c478bd9Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18357c478bd9Sstevel@tonic-gate 		else
18367c478bd9Sstevel@tonic-gate 			(void) printf("%*ld", width, (long)psinfo->pr_contract);
18377c478bd9Sstevel@tonic-gate 		break;
1838c6402783Sakolb 	case F_LGRP:
1839c6402783Sakolb 		/* Display home lgroup */
1840c6402783Sakolb 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
1841c6402783Sakolb 		break;
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate static void
18467c478bd9Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	int wcnt;
18497c478bd9Sstevel@tonic-gate 	int width = f->width;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	switch (f->fname) {
18527c478bd9Sstevel@tonic-gate 	case F_FNAME:
18537c478bd9Sstevel@tonic-gate 	case F_COMM:
18547c478bd9Sstevel@tonic-gate 	case F_ARGS:
18557c478bd9Sstevel@tonic-gate 		/*
18567c478bd9Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
18577c478bd9Sstevel@tonic-gate 		 */
18587c478bd9Sstevel@tonic-gate 		wcnt = min(width, sizeof ("<defunct>"));
18597c478bd9Sstevel@tonic-gate 		if (f->next != NULL)
18607c478bd9Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, "<defunct>");
18617c478bd9Sstevel@tonic-gate 		else
18627c478bd9Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, "<defunct>");
18637c478bd9Sstevel@tonic-gate 		break;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	case F_PSR:
18667c478bd9Sstevel@tonic-gate 	case F_PCPU:
18677c478bd9Sstevel@tonic-gate 	case F_PMEM:
18687c478bd9Sstevel@tonic-gate 	case F_NICE:
18697c478bd9Sstevel@tonic-gate 	case F_CLASS:
18707c478bd9Sstevel@tonic-gate 	case F_STIME:
18717c478bd9Sstevel@tonic-gate 	case F_ETIME:
18727c478bd9Sstevel@tonic-gate 	case F_WCHAN:
18737c478bd9Sstevel@tonic-gate 	case F_PSET:
18747c478bd9Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
18757c478bd9Sstevel@tonic-gate 		break;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	case F_OPRI:
18787c478bd9Sstevel@tonic-gate 	case F_PRI:
18797c478bd9Sstevel@tonic-gate 	case F_OSZ:
18807c478bd9Sstevel@tonic-gate 	case F_VSZ:
18817c478bd9Sstevel@tonic-gate 	case F_RSS:
18827c478bd9Sstevel@tonic-gate 		(void) printf("%*d", width, 0);
18837c478bd9Sstevel@tonic-gate 		break;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	default:
18867c478bd9Sstevel@tonic-gate 		print_field(psinfo, f, ttyp);
18877c478bd9Sstevel@tonic-gate 		break;
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate static void
18927c478bd9Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp,
18937c478bd9Sstevel@tonic-gate 	void (*print_fld)(psinfo_t *, struct field *, const char *))
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	struct field *f;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	for (f = fields; f != NULL; f = f->next) {
18987c478bd9Sstevel@tonic-gate 		print_fld(psinfo, f, ttyp);
18997c478bd9Sstevel@tonic-gate 		if (f->next != NULL)
19007c478bd9Sstevel@tonic-gate 			(void) printf(" ");
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 	(void) printf("\n");
19037c478bd9Sstevel@tonic-gate }
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate /*
19067c478bd9Sstevel@tonic-gate  * Returns 1 if arg is found in array arr, of length num; 0 otherwise.
19077c478bd9Sstevel@tonic-gate  */
19087c478bd9Sstevel@tonic-gate static int
19097c478bd9Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg)
19107c478bd9Sstevel@tonic-gate {
19117c478bd9Sstevel@tonic-gate 	int i;
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	for (i = 0; i < number; i++)
19147c478bd9Sstevel@tonic-gate 		if (arg == arr[i])
19157c478bd9Sstevel@tonic-gate 			return (1);
19167c478bd9Sstevel@tonic-gate 	return (0);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * Add an entry (user, group) to the specified table.
19217c478bd9Sstevel@tonic-gate  */
19227c478bd9Sstevel@tonic-gate static void
19237c478bd9Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name)
19247c478bd9Sstevel@tonic-gate {
19257c478bd9Sstevel@tonic-gate 	struct ugdata *entp;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	if (tbl->size == tbl->nent) {	/* reallocate the table entries */
19287c478bd9Sstevel@tonic-gate 		if ((tbl->size *= 2) == 0)
19297c478bd9Sstevel@tonic-gate 			tbl->size = 32;		/* first time */
19307c478bd9Sstevel@tonic-gate 		tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 	entp = &tbl->ent[tbl->nent++];
19337c478bd9Sstevel@tonic-gate 	entp->id = 0;
19347c478bd9Sstevel@tonic-gate 	(void) strncpy(entp->name, name, MAXUGNAME);
19357c478bd9Sstevel@tonic-gate 	entp->name[MAXUGNAME] = '\0';
19367c478bd9Sstevel@tonic-gate }
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate static int
19397c478bd9Sstevel@tonic-gate uconv(struct ughead *uhead)
19407c478bd9Sstevel@tonic-gate {
19417c478bd9Sstevel@tonic-gate 	struct ugdata *utbl = uhead->ent;
19427c478bd9Sstevel@tonic-gate 	int n = uhead->nent;
19437c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
19447c478bd9Sstevel@tonic-gate 	int i;
19457c478bd9Sstevel@tonic-gate 	int fnd = 0;
19467c478bd9Sstevel@tonic-gate 	uid_t uid;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/*
19497c478bd9Sstevel@tonic-gate 	 * Ask the name service for names.
19507c478bd9Sstevel@tonic-gate 	 */
19517c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19527c478bd9Sstevel@tonic-gate 		/*
19537c478bd9Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
19547c478bd9Sstevel@tonic-gate 		 */
1955f48205beScasper 		if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
19567c478bd9Sstevel@tonic-gate 			pwd = getpwuid(uid);
19577c478bd9Sstevel@tonic-gate 		else
19587c478bd9Sstevel@tonic-gate 			pwd = getpwnam(utbl[i].name);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		/*
19617c478bd9Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
19627c478bd9Sstevel@tonic-gate 		 */
19637c478bd9Sstevel@tonic-gate 		if (pwd == NULL) {
19647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
19657c478bd9Sstevel@tonic-gate 			    gettext("ps: unknown user %s\n"), utbl[i].name);
19667c478bd9Sstevel@tonic-gate 			continue;
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		utbl[fnd].id = pwd->pw_uid;
19707c478bd9Sstevel@tonic-gate 		(void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
19717c478bd9Sstevel@tonic-gate 		fnd++;
19727c478bd9Sstevel@tonic-gate 	}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	uhead->nent = fnd;	/* in case it changed */
19757c478bd9Sstevel@tonic-gate 	return (n - fnd);
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate static int
19797c478bd9Sstevel@tonic-gate gconv(struct ughead *ghead)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	struct ugdata *gtbl = ghead->ent;
19827c478bd9Sstevel@tonic-gate 	int n = ghead->nent;
19837c478bd9Sstevel@tonic-gate 	struct group *grp;
19847c478bd9Sstevel@tonic-gate 	gid_t gid;
19857c478bd9Sstevel@tonic-gate 	int i;
19867c478bd9Sstevel@tonic-gate 	int fnd = 0;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/*
19897c478bd9Sstevel@tonic-gate 	 * Ask the name service for names.
19907c478bd9Sstevel@tonic-gate 	 */
19917c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19927c478bd9Sstevel@tonic-gate 		/*
19937c478bd9Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
19947c478bd9Sstevel@tonic-gate 		 */
1995f48205beScasper 		if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
19967c478bd9Sstevel@tonic-gate 			grp = getgrgid(gid);
19977c478bd9Sstevel@tonic-gate 		else
19987c478bd9Sstevel@tonic-gate 			grp = getgrnam(gtbl[i].name);
19997c478bd9Sstevel@tonic-gate 		/*
20007c478bd9Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
20017c478bd9Sstevel@tonic-gate 		 */
20027c478bd9Sstevel@tonic-gate 		if (grp == NULL) {
20037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
20047c478bd9Sstevel@tonic-gate 			    gettext("ps: unknown group %s\n"), gtbl[i].name);
20057c478bd9Sstevel@tonic-gate 			continue;
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 		gtbl[fnd].id = grp->gr_gid;
20097c478bd9Sstevel@tonic-gate 		(void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
20107c478bd9Sstevel@tonic-gate 		fnd++;
20117c478bd9Sstevel@tonic-gate 	}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	ghead->nent = fnd;	/* in case it changed */
20147c478bd9Sstevel@tonic-gate 	return (n - fnd);
20157c478bd9Sstevel@tonic-gate }
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate /*
20187c478bd9Sstevel@tonic-gate  * Return 1 if puid is in table, otherwise 0.
20197c478bd9Sstevel@tonic-gate  */
20207c478bd9Sstevel@tonic-gate static int
20217c478bd9Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead)
20227c478bd9Sstevel@tonic-gate {
20237c478bd9Sstevel@tonic-gate 	struct ugdata *utbl = ughead->ent;
20247c478bd9Sstevel@tonic-gate 	int n = ughead->nent;
20257c478bd9Sstevel@tonic-gate 	int i;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++)
20287c478bd9Sstevel@tonic-gate 		if (utbl[i].id == id)
20297c478bd9Sstevel@tonic-gate 			return (1);
20307c478bd9Sstevel@tonic-gate 	return (0);
20317c478bd9Sstevel@tonic-gate }
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate /*
20347c478bd9Sstevel@tonic-gate  * Print starting time of process unless process started more than 24 hours
20357c478bd9Sstevel@tonic-gate  * ago, in which case the date is printed.  The date is printed in the form
20367c478bd9Sstevel@tonic-gate  * "MMM dd" if old format, else the blank is replaced with an '_' so
20377c478bd9Sstevel@tonic-gate  * it appears as a single word (for parseability).
20387c478bd9Sstevel@tonic-gate  */
20397c478bd9Sstevel@tonic-gate static void
20407c478bd9Sstevel@tonic-gate prtime(timestruc_t st, int width, int old)
20417c478bd9Sstevel@tonic-gate {
20427c478bd9Sstevel@tonic-gate 	char sttim[26];
20437c478bd9Sstevel@tonic-gate 	time_t starttime;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	starttime = st.tv_sec;
20467c478bd9Sstevel@tonic-gate 	if (st.tv_nsec > 500000000)
20477c478bd9Sstevel@tonic-gate 		starttime++;
20487c478bd9Sstevel@tonic-gate 	if ((now.tv_sec - starttime) >= 24*60*60) {
204916f94f58Ssayama 		(void) strftime(sttim, sizeof (sttim), old?
205016f94f58Ssayama 		/*
205116f94f58Ssayama 		 * TRANSLATION_NOTE
205216f94f58Ssayama 		 * This time format is used by STIME field when -f option
205316f94f58Ssayama 		 * is specified.  Used for processes that begun more than
205416f94f58Ssayama 		 * 24 hours.
205516f94f58Ssayama 		 */
205616f94f58Ssayama 		    dcgettext(NULL, "%b %d", LC_TIME) :
205716f94f58Ssayama 		/*
205816f94f58Ssayama 		 * TRANSLATION_NOTE
205916f94f58Ssayama 		 * This time format is used by STIME field when -o option
206016f94f58Ssayama 		 * is specified.  Used for processes that begun more than
206116f94f58Ssayama 		 * 24 hours.
206216f94f58Ssayama 		 */
206316f94f58Ssayama 		    dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
20647c478bd9Sstevel@tonic-gate 	} else {
206516f94f58Ssayama 		/*
206616f94f58Ssayama 		 * TRANSLATION_NOTE
206716f94f58Ssayama 		 * This time format is used by STIME field when -f or -o option
206816f94f58Ssayama 		 * is specified.  Used for processes that begun less than
206916f94f58Ssayama 		 * 24 hours.
207016f94f58Ssayama 		 */
207116f94f58Ssayama 		(void) strftime(sttim, sizeof (sttim),
207216f94f58Ssayama 		    dcgettext(NULL, "%H:%M:%S", LC_TIME),
207316f94f58Ssayama 		    localtime(&starttime));
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 	(void) printf("%*.*s", width, width, sttim);
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate static void
20797c478bd9Sstevel@tonic-gate przom(psinfo_t *psinfo)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	long	tm;
20827c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
20837c478bd9Sstevel@tonic-gate 	char zonename[ZONENAME_MAX];
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	/*
20867c478bd9Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
20877c478bd9Sstevel@tonic-gate 	 * spearator, rather than keeping track of which column is first.  All
20887c478bd9Sstevel@tonic-gate 	 * other fields are printed with a leading space.
20897c478bd9Sstevel@tonic-gate 	 */
20907c478bd9Sstevel@tonic-gate 	if (lflg) {	/* F S */
20917c478bd9Sstevel@tonic-gate 		if (!yflg)
20927c478bd9Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
20937c478bd9Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate 	if (Zflg) {
20967c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
20977c478bd9Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
20987c478bd9Sstevel@tonic-gate 			(void) printf("%7.7d ", ((int)psinfo->pr_zoneid));
20997c478bd9Sstevel@tonic-gate 		} else {
21007c478bd9Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
21017c478bd9Sstevel@tonic-gate 		}
21027c478bd9Sstevel@tonic-gate 	}
2103c6402783Sakolb 	if (Hflg) {
2104c6402783Sakolb 		/* Display home lgroup */
2105c6402783Sakolb 		(void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2106c6402783Sakolb 	}
21077c478bd9Sstevel@tonic-gate 	if (fflg) {
21087c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
21097c478bd9Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
21107c478bd9Sstevel@tonic-gate 		else
2111*d21c64a5SDavid Edmondson 			(void) printf(" %7.7u ", psinfo->pr_euid);
21127c478bd9Sstevel@tonic-gate 	} else if (lflg)
2113f48205beScasper 		(void) printf("%6u ", psinfo->pr_euid);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid);	/* PID */
21167c478bd9Sstevel@tonic-gate 	if (lflg || fflg)
21177c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21187c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_ppid);			/* PPID */
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	if (jflg) {
21217c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21227c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);			/* PGID */
21237c478bd9Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21247c478bd9Sstevel@tonic-gate 		    (int)psinfo->pr_sid);			/* SID  */
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	if (Lflg)
21287c478bd9Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* LWP */
21297c478bd9Sstevel@tonic-gate 	if (Pflg)
21307c478bd9Sstevel@tonic-gate 		(void) printf("   -");				/* PSR */
21317c478bd9Sstevel@tonic-gate 	if (Lflg && fflg)
21327c478bd9Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* NLWP */
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	if (cflg) {
21357c478bd9Sstevel@tonic-gate 		(void) printf(" %4s", "-");	/* zombies have no class */
21367c478bd9Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI	*/
21377c478bd9Sstevel@tonic-gate 	} else if (lflg || fflg) {
21387c478bd9Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
21397c478bd9Sstevel@tonic-gate 		if (lflg)
21407c478bd9Sstevel@tonic-gate 			(void) printf(" %3d %2s",
21417c478bd9Sstevel@tonic-gate 			    psinfo->pr_lwp.pr_oldpri, "-");	/* PRI NI */
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	if (lflg) {
21447c478bd9Sstevel@tonic-gate 		if (yflg)				/* RSS SZ WCHAN */
21457c478bd9Sstevel@tonic-gate 			(void) printf(" %5d %6d %8s", 0, 0, "-");
21467c478bd9Sstevel@tonic-gate 		else					/* ADDR SZ WCHAN */
21477c478bd9Sstevel@tonic-gate 			(void) printf(" %8s %6d %8s", "-", 0, "-");
21487c478bd9Sstevel@tonic-gate 	}
214916f94f58Ssayama 	if (fflg) {
215016f94f58Ssayama 		int width = fname[F_STIME].width;
215116f94f58Ssayama 		(void) printf(" %*.*s", width, width, "-"); 	/* STIME */
215216f94f58Ssayama 	}
21537c478bd9Sstevel@tonic-gate 	(void) printf(" %-8.14s", "?");				/* TTY */
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	tm = psinfo->pr_time.tv_sec;
21567c478bd9Sstevel@tonic-gate 	if (psinfo->pr_time.tv_nsec > 500000000)
21577c478bd9Sstevel@tonic-gate 		tm++;
21587c478bd9Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);	/* TIME */
21597c478bd9Sstevel@tonic-gate 	(void) printf(" <defunct>\n");
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate /*
21637c478bd9Sstevel@tonic-gate  * Function to compute the number of printable bytes in a multibyte
21647c478bd9Sstevel@tonic-gate  * command string ("internationalization").
21657c478bd9Sstevel@tonic-gate  */
21667c478bd9Sstevel@tonic-gate static int
21677c478bd9Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize)
21687c478bd9Sstevel@tonic-gate {
21697c478bd9Sstevel@tonic-gate 	int csiwcnt = 0, scrwcnt = 0;
21707c478bd9Sstevel@tonic-gate 	int ncsisz, nscrsz;
21717c478bd9Sstevel@tonic-gate 	wchar_t  wchar;
21727c478bd9Sstevel@tonic-gate 	int	 len;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	while (*cmd != '\0') {
21757c478bd9Sstevel@tonic-gate 		if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
21767c478bd9Sstevel@tonic-gate 			len = MB_CUR_MAX;
21777c478bd9Sstevel@tonic-gate 		if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
21787c478bd9Sstevel@tonic-gate 			return (8); /* default to use for illegal chars */
21797c478bd9Sstevel@tonic-gate 		if ((nscrsz = wcwidth(wchar)) <= 0)
21807c478bd9Sstevel@tonic-gate 			return (8);
21817c478bd9Sstevel@tonic-gate 		if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize)
21827c478bd9Sstevel@tonic-gate 			break;
21837c478bd9Sstevel@tonic-gate 		csiwcnt += ncsisz;
21847c478bd9Sstevel@tonic-gate 		scrwcnt += nscrsz;
21857c478bd9Sstevel@tonic-gate 		cmd += ncsisz;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 	return (csiwcnt);
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate static char *
21917c478bd9Sstevel@tonic-gate err_string(int err)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate 	static char buf[32];
21947c478bd9Sstevel@tonic-gate 	char *str = strerror(err);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	if (str == NULL)
21977c478bd9Sstevel@tonic-gate 		(void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	return (str);
22007c478bd9Sstevel@tonic-gate }
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate /* If allocation fails, die */
22037c478bd9Sstevel@tonic-gate static void *
22047c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size)
22057c478bd9Sstevel@tonic-gate {
22067c478bd9Sstevel@tonic-gate 	ptr = realloc(ptr, size);
22077c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
22087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ps: no memory\n"));
22097c478bd9Sstevel@tonic-gate 		exit(1);
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 	return (ptr);
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate static time_t
22157c478bd9Sstevel@tonic-gate delta_secs(const timestruc_t *start)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate 	time_t seconds = now.tv_sec - start->tv_sec;
22187c478bd9Sstevel@tonic-gate 	long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (nanosecs >= (NANOSEC / 2))
22217c478bd9Sstevel@tonic-gate 		seconds++;
22227c478bd9Sstevel@tonic-gate 	else if (nanosecs < -(NANOSEC / 2))
22237c478bd9Sstevel@tonic-gate 		seconds--;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	return (seconds);
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate /*
22297c478bd9Sstevel@tonic-gate  * Returns the following:
22307c478bd9Sstevel@tonic-gate  *
22317c478bd9Sstevel@tonic-gate  * 	0	No error
22327c478bd9Sstevel@tonic-gate  * 	EINVAL	Invalid number
22337c478bd9Sstevel@tonic-gate  * 	ERANGE	Value exceeds (min, max) range
22347c478bd9Sstevel@tonic-gate  */
22357c478bd9Sstevel@tonic-gate static int
22367c478bd9Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max)
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate 	char *q;
22397c478bd9Sstevel@tonic-gate 	long number;
22407c478bd9Sstevel@tonic-gate 	int error;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	errno = 0;
22437c478bd9Sstevel@tonic-gate 	number = strtol(p, &q, 10);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	if (errno != 0 || q == p || *q != '\0') {
22467c478bd9Sstevel@tonic-gate 		if ((error = errno) == 0) {
22477c478bd9Sstevel@tonic-gate 			/*
22487c478bd9Sstevel@tonic-gate 			 * strtol() can fail without setting errno, or it can
22497c478bd9Sstevel@tonic-gate 			 * set it to EINVAL or ERANGE.  In the case errno is
22507c478bd9Sstevel@tonic-gate 			 * still zero, return EINVAL.
22517c478bd9Sstevel@tonic-gate 			 */
22527c478bd9Sstevel@tonic-gate 			error = EINVAL;
22537c478bd9Sstevel@tonic-gate 		}
22547c478bd9Sstevel@tonic-gate 	} else if (number < min || number > max) {
22557c478bd9Sstevel@tonic-gate 		error = ERANGE;
22567c478bd9Sstevel@tonic-gate 	} else {
22577c478bd9Sstevel@tonic-gate 		error = 0;
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	*val = number;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	return (error);
22637c478bd9Sstevel@tonic-gate }
22647c478bd9Sstevel@tonic-gate 
2265f48205beScasper /*
2266f48205beScasper  * Returns the following:
2267f48205beScasper  *
2268f48205beScasper  * 	0	No error
2269f48205beScasper  * 	EINVAL	Invalid number
2270f48205beScasper  * 	ERANGE	Value exceeds (min, max) range
2271f48205beScasper  */
2272f48205beScasper static int
2273f48205beScasper str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
2274f48205beScasper {
2275f48205beScasper 	char *q;
2276f48205beScasper 	unsigned long number;
2277f48205beScasper 	int error;
2278f48205beScasper 
2279f48205beScasper 	errno = 0;
2280f48205beScasper 	number = strtoul(p, &q, 10);
2281f48205beScasper 
2282f48205beScasper 	if (errno != 0 || q == p || *q != '\0') {
2283f48205beScasper 		if ((error = errno) == 0) {
2284f48205beScasper 			/*
2285f48205beScasper 			 * strtoul() can fail without setting errno, or it can
2286f48205beScasper 			 * set it to EINVAL or ERANGE.  In the case errno is
2287f48205beScasper 			 * still zero, return EINVAL.
2288f48205beScasper 			 */
2289f48205beScasper 			error = EINVAL;
2290f48205beScasper 		}
2291f48205beScasper 	} else if (number < min || number > max) {
2292f48205beScasper 		error = ERANGE;
2293f48205beScasper 	} else {
2294f48205beScasper 		error = 0;
2295f48205beScasper 	}
2296f48205beScasper 
2297f48205beScasper 	*val = number;
2298f48205beScasper 
2299f48205beScasper 	return (error);
2300f48205beScasper }
2301f48205beScasper 
23027c478bd9Sstevel@tonic-gate static int
23037c478bd9Sstevel@tonic-gate pidcmp(const void *p1, const void *p2)
23047c478bd9Sstevel@tonic-gate {
23057c478bd9Sstevel@tonic-gate 	pid_t i = *((pid_t *)p1);
23067c478bd9Sstevel@tonic-gate 	pid_t j = *((pid_t *)p2);
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	return (i - j);
23097c478bd9Sstevel@tonic-gate }
2310