xref: /onnv-gate/usr/src/cmd/ps/ps.c (revision 8233)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51454Sjonb  * Common Development and Distribution License (the "License").
61454Sjonb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211454Sjonb 
220Sstevel@tonic-gate /*
23*8233Sdme@sun.com  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * ps -- print things about processes.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <pwd.h>
390Sstevel@tonic-gate #include <grp.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <limits.h>
460Sstevel@tonic-gate #include <dirent.h>
470Sstevel@tonic-gate #include <sys/signal.h>
480Sstevel@tonic-gate #include <sys/fault.h>
490Sstevel@tonic-gate #include <sys/syscall.h>
500Sstevel@tonic-gate #include <sys/time.h>
510Sstevel@tonic-gate #include <procfs.h>
520Sstevel@tonic-gate #include <locale.h>
530Sstevel@tonic-gate #include <wctype.h>
540Sstevel@tonic-gate #include <wchar.h>
550Sstevel@tonic-gate #include <libw.h>
560Sstevel@tonic-gate #include <stdarg.h>
570Sstevel@tonic-gate #include <sys/proc.h>
580Sstevel@tonic-gate #include <sys/pset.h>
590Sstevel@tonic-gate #include <project.h>
600Sstevel@tonic-gate #include <zone.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	min(a, b)	((a) > (b) ? (b) : (a))
630Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	NTTYS	20	/* initial size of table for -t option  */
662685Sakolb #define	SIZ	30	/* initial size of tables for -p, -s, -g, -h and -z */
671454Sjonb 
681454Sjonb /*
691454Sjonb  * Size of buffer holding args for t, p, s, g, u, U, G, z options.
701454Sjonb  * Set to ZONENAME_MAX, the minimum value needed to allow any
711454Sjonb  * zone to be specified.
721454Sjonb  */
731454Sjonb #define	ARGSIZ ZONENAME_MAX
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	MAXUGNAME 10	/* max chars in a user/group name or printed u/g id */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /* Structure for storing user or group info */
780Sstevel@tonic-gate struct ugdata {
790Sstevel@tonic-gate 	id_t	id;			/* numeric user-id or group-id */
800Sstevel@tonic-gate 	char	name[MAXUGNAME+1];	/* user/group name, null terminated */
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate struct ughead {
840Sstevel@tonic-gate 	size_t	size;		/* number of ugdata structs allocated */
850Sstevel@tonic-gate 	size_t	nent;		/* number of active entries */
860Sstevel@tonic-gate 	struct ugdata *ent;	/* pointer to array of actual entries */
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate enum fname {	/* enumeration of field names */
900Sstevel@tonic-gate 	F_USER,		/* effective user of the process */
910Sstevel@tonic-gate 	F_RUSER,	/* real user of the process */
920Sstevel@tonic-gate 	F_GROUP,	/* effective group of the process */
930Sstevel@tonic-gate 	F_RGROUP,	/* real group of the process */
940Sstevel@tonic-gate 	F_UID,		/* numeric effective uid of the process */
950Sstevel@tonic-gate 	F_RUID,		/* numeric real uid of the process */
960Sstevel@tonic-gate 	F_GID,		/* numeric effective gid of the process */
970Sstevel@tonic-gate 	F_RGID,		/* numeric real gid of the process */
980Sstevel@tonic-gate 	F_PID,		/* process id */
990Sstevel@tonic-gate 	F_PPID,		/* parent process id */
1000Sstevel@tonic-gate 	F_PGID,		/* process group id */
1010Sstevel@tonic-gate 	F_SID,		/* session id */
1020Sstevel@tonic-gate 	F_PSR,		/* bound processor */
1030Sstevel@tonic-gate 	F_LWP,		/* lwp-id */
1040Sstevel@tonic-gate 	F_NLWP,		/* number of lwps */
1050Sstevel@tonic-gate 	F_OPRI,		/* old priority (obsolete) */
1060Sstevel@tonic-gate 	F_PRI,		/* new priority */
1070Sstevel@tonic-gate 	F_F,		/* process flags */
1080Sstevel@tonic-gate 	F_S,		/* letter indicating the state */
1090Sstevel@tonic-gate 	F_C,		/* processor utilization (obsolete) */
1100Sstevel@tonic-gate 	F_PCPU,		/* percent of recently used cpu time */
1110Sstevel@tonic-gate 	F_PMEM,		/* percent of physical memory used (rss) */
1120Sstevel@tonic-gate 	F_OSZ,		/* virtual size of the process in pages */
1130Sstevel@tonic-gate 	F_VSZ,		/* virtual size of the process in kilobytes */
1140Sstevel@tonic-gate 	F_RSS,		/* resident set size of the process in kilobytes */
1150Sstevel@tonic-gate 	F_NICE,		/* "nice" value of the process */
1160Sstevel@tonic-gate 	F_CLASS,	/* scheduler class */
1170Sstevel@tonic-gate 	F_STIME,	/* start time of the process, hh:mm:ss or Month Day */
1180Sstevel@tonic-gate 	F_ETIME,	/* elapsed time of the process, [[dd-]hh:]mm:ss */
1190Sstevel@tonic-gate 	F_TIME,		/* cpu time of the process, [[dd-]hh:]mm:ss */
1200Sstevel@tonic-gate 	F_TTY,		/* name of the controlling terminal */
1210Sstevel@tonic-gate 	F_ADDR,		/* address of the process (obsolete) */
1220Sstevel@tonic-gate 	F_WCHAN,	/* wait channel (sleep condition variable) */
1230Sstevel@tonic-gate 	F_FNAME,	/* file name of command */
1240Sstevel@tonic-gate 	F_COMM,		/* name of command (argv[0] value) */
1250Sstevel@tonic-gate 	F_ARGS,		/* name of command plus all its arguments */
1260Sstevel@tonic-gate 	F_TASKID,	/* task id */
1270Sstevel@tonic-gate 	F_PROJID,	/* project id */
1280Sstevel@tonic-gate 	F_PROJECT,	/* project name of the process */
1290Sstevel@tonic-gate 	F_PSET,		/* bound processor set */
1300Sstevel@tonic-gate 	F_ZONE,		/* zone name */
1310Sstevel@tonic-gate 	F_ZONEID,	/* zone id */
1322685Sakolb 	F_CTID,		/* process contract id */
1332685Sakolb 	F_LGRP		/* process home lgroup */
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate struct field {
1370Sstevel@tonic-gate 	struct field	*next;		/* linked list */
1380Sstevel@tonic-gate 	int		fname;		/* field index */
1390Sstevel@tonic-gate 	const char	*header;	/* header to use */
1400Sstevel@tonic-gate 	int		width;		/* width of field */
1410Sstevel@tonic-gate };
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static	struct field *fields = NULL;	/* fields selected via -o */
1440Sstevel@tonic-gate static	struct field *last_field = NULL;
1450Sstevel@tonic-gate static	int do_header = 0;
1460Sstevel@tonic-gate static	struct timeval now;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /* array of defined fields, in fname order */
1490Sstevel@tonic-gate struct def_field {
1500Sstevel@tonic-gate 	const char *fname;
1510Sstevel@tonic-gate 	const char *header;
1520Sstevel@tonic-gate 	int width;
1530Sstevel@tonic-gate 	int minwidth;
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct def_field fname[] = {
1570Sstevel@tonic-gate 	/* fname	header		width	minwidth */
1580Sstevel@tonic-gate 	{ "user",	"USER",		8,	8	},
1590Sstevel@tonic-gate 	{ "ruser",	"RUSER",	8,	8	},
1600Sstevel@tonic-gate 	{ "group",	"GROUP",	8,	8	},
1610Sstevel@tonic-gate 	{ "rgroup",	"RGROUP",	8,	8	},
1620Sstevel@tonic-gate 	{ "uid",	"UID",		5,	5	},
1630Sstevel@tonic-gate 	{ "ruid",	"RUID",		5,	5	},
1640Sstevel@tonic-gate 	{ "gid",	"GID",		5,	5	},
1650Sstevel@tonic-gate 	{ "rgid",	"RGID",		5,	5	},
1660Sstevel@tonic-gate 	{ "pid",	"PID",		5,	5	},
1670Sstevel@tonic-gate 	{ "ppid",	"PPID",		5,	5	},
1680Sstevel@tonic-gate 	{ "pgid",	"PGID",		5,	5	},
1690Sstevel@tonic-gate 	{ "sid",	"SID",		5,	5	},
1700Sstevel@tonic-gate 	{ "psr",	"PSR",		3,	2	},
1710Sstevel@tonic-gate 	{ "lwp",	"LWP",		6,	2	},
1720Sstevel@tonic-gate 	{ "nlwp",	"NLWP",		4,	2	},
1730Sstevel@tonic-gate 	{ "opri",	"PRI",		3,	2	},
1740Sstevel@tonic-gate 	{ "pri",	"PRI",		3,	2	},
1750Sstevel@tonic-gate 	{ "f",		"F",		2,	2	},
1760Sstevel@tonic-gate 	{ "s",		"S",		1,	1	},
1770Sstevel@tonic-gate 	{ "c",		"C",		2,	2	},
1780Sstevel@tonic-gate 	{ "pcpu",	"%CPU",		4,	4	},
1790Sstevel@tonic-gate 	{ "pmem",	"%MEM",		4,	4	},
1800Sstevel@tonic-gate 	{ "osz",	"SZ",		4,	4	},
1810Sstevel@tonic-gate 	{ "vsz",	"VSZ",		4,	4	},
1820Sstevel@tonic-gate 	{ "rss",	"RSS",		4,	4	},
1830Sstevel@tonic-gate 	{ "nice",	"NI",		2,	2	},
1840Sstevel@tonic-gate 	{ "class",	"CLS",		4,	2	},
1850Sstevel@tonic-gate 	{ "stime",	"STIME",	8,	8	},
1860Sstevel@tonic-gate 	{ "etime",	"ELAPSED",	11,	7	},
1870Sstevel@tonic-gate 	{ "time",	"TIME",		11,	5	},
1880Sstevel@tonic-gate 	{ "tty",	"TT",		7,	7	},
1890Sstevel@tonic-gate #ifdef _LP64
1900Sstevel@tonic-gate 	{ "addr",	"ADDR",		16,	8	},
1910Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	16,	8	},
1920Sstevel@tonic-gate #else
1930Sstevel@tonic-gate 	{ "addr",	"ADDR",		8,	8	},
1940Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	8,	8	},
1950Sstevel@tonic-gate #endif
1960Sstevel@tonic-gate 	{ "fname",	"COMMAND",	8,	8	},
1970Sstevel@tonic-gate 	{ "comm",	"COMMAND",	80,	8	},
1980Sstevel@tonic-gate 	{ "args",	"COMMAND",	80,	80	},
1990Sstevel@tonic-gate 	{ "taskid",	"TASKID",	5,	5	},
2000Sstevel@tonic-gate 	{ "projid",	"PROJID",	5,	5	},
2010Sstevel@tonic-gate 	{ "project",	"PROJECT",	8,	8	},
2020Sstevel@tonic-gate 	{ "pset",	"PSET",		3,	3	},
2030Sstevel@tonic-gate 	{ "zone",	"ZONE",		8,	8	},
2040Sstevel@tonic-gate 	{ "zoneid",	"ZONEID",	5,	5	},
2050Sstevel@tonic-gate 	{ "ctid",	"CTID",		5,	5	},
2062685Sakolb 	{ "lgrp",	"LGRP",		4,	2 	},
2070Sstevel@tonic-gate };
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate #define	NFIELDS	(sizeof (fname) / sizeof (fname[0]))
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static	int	retcode = 1;
2120Sstevel@tonic-gate static	int	lflg;
2130Sstevel@tonic-gate static	int	Aflg;
2140Sstevel@tonic-gate static	int	uflg;
2150Sstevel@tonic-gate static	int	Uflg;
2160Sstevel@tonic-gate static	int	Gflg;
2170Sstevel@tonic-gate static	int	aflg;
2180Sstevel@tonic-gate static	int	dflg;
2190Sstevel@tonic-gate static	int	Lflg;
2200Sstevel@tonic-gate static	int	Pflg;
2210Sstevel@tonic-gate static	int	yflg;
2220Sstevel@tonic-gate static	int	pflg;
2230Sstevel@tonic-gate static	int	fflg;
2240Sstevel@tonic-gate static	int	cflg;
2250Sstevel@tonic-gate static	int	jflg;
2260Sstevel@tonic-gate static	int	gflg;
2270Sstevel@tonic-gate static	int	sflg;
2280Sstevel@tonic-gate static	int	tflg;
2290Sstevel@tonic-gate static	int	zflg;
2300Sstevel@tonic-gate static	int	Zflg;
2312685Sakolb static	int	hflg;
2322685Sakolb static	int	Hflg;
2334321Scasper static	uid_t	tuid = (uid_t)-1;
2340Sstevel@tonic-gate static	int	errflg;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static	int	ndev;		/* number of devices */
2370Sstevel@tonic-gate static	int	maxdev;		/* number of devl structures allocated */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate #define	DNINCR	100
2400Sstevel@tonic-gate #define	DNSIZE	14
2410Sstevel@tonic-gate static struct devl {		/* device list   */
2420Sstevel@tonic-gate 	char	dname[DNSIZE];	/* device name   */
2430Sstevel@tonic-gate 	dev_t	ddev;		/* device number */
2440Sstevel@tonic-gate } *devl;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static	struct tty {
2470Sstevel@tonic-gate 	char *tname;
2480Sstevel@tonic-gate 	dev_t tdev;
2490Sstevel@tonic-gate } *tty = NULL;			/* for t option */
2500Sstevel@tonic-gate static	size_t	ttysz = 0;
2510Sstevel@tonic-gate static	int	ntty = 0;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static	pid_t	*pid = NULL;	/* for p option */
2540Sstevel@tonic-gate static	size_t	pidsz = 0;
2550Sstevel@tonic-gate static	size_t	npid = 0;
2560Sstevel@tonic-gate 
2572685Sakolb static	int	*lgrps = NULL;	/* list of lgroup IDs for for h option */
2582685Sakolb static	size_t	lgrps_size = 0;	/* size of the lgrps list */
2592685Sakolb static	size_t	nlgrps = 0;	/* number elements in the list */
2602685Sakolb 
2612685Sakolb /* Maximum possible lgroup ID value */
2622685Sakolb #define	MAX_LGRP_ID 256
2632685Sakolb 
2640Sstevel@tonic-gate static	pid_t	*grpid = NULL;	/* for g option */
2650Sstevel@tonic-gate static	size_t	grpidsz = 0;
2660Sstevel@tonic-gate static	int	ngrpid = 0;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate static	pid_t	*sessid = NULL;	/* for s option */
2690Sstevel@tonic-gate static	size_t	sessidsz = 0;
2700Sstevel@tonic-gate static	int	nsessid = 0;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static	zoneid_t *zoneid = NULL; /* for z option */
2730Sstevel@tonic-gate static	size_t	zoneidsz = 0;
2740Sstevel@tonic-gate static	int	nzoneid = 0;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static	int	kbytes_per_page;
2770Sstevel@tonic-gate static	int	pidwidth;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate static	char	*procdir = "/proc";	/* standard /proc directory */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate static struct ughead	euid_tbl;	/* table to store selected euid's */
2820Sstevel@tonic-gate static struct ughead	ruid_tbl;	/* table to store selected real uid's */
2830Sstevel@tonic-gate static struct ughead	egid_tbl;	/* table to store selected egid's */
2840Sstevel@tonic-gate static struct ughead	rgid_tbl;	/* table to store selected real gid's */
2850Sstevel@tonic-gate static prheader_t *lpsinfobuf;		/* buffer to contain lpsinfo */
2860Sstevel@tonic-gate static size_t	lpbufsize;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * This constant defines the sentinal number of process IDs below which we
2900Sstevel@tonic-gate  * only examine individual entries in /proc rather than scanning through
2910Sstevel@tonic-gate  * /proc. This optimization is a huge win in the common case.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate #define	PTHRESHOLD	40
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static	void	usage(void);
2960Sstevel@tonic-gate static	char	*getarg(char **);
2970Sstevel@tonic-gate static	char	*parse_format(char *);
2980Sstevel@tonic-gate static	char	*gettty(psinfo_t *);
2990Sstevel@tonic-gate static	int	prfind(int, psinfo_t *, char **);
3000Sstevel@tonic-gate static	void	prcom(psinfo_t *, char *);
3010Sstevel@tonic-gate static	void	prtpct(ushort_t, int);
3020Sstevel@tonic-gate static	void	print_time(time_t, int);
3030Sstevel@tonic-gate static	void	print_field(psinfo_t *, struct field *, const char *);
3040Sstevel@tonic-gate static	void	print_zombie_field(psinfo_t *, struct field *, const char *);
3050Sstevel@tonic-gate static	void	pr_fields(psinfo_t *, const char *,
3060Sstevel@tonic-gate 		void (*print_fld)(psinfo_t *, struct field *, const char *));
3070Sstevel@tonic-gate static	int	search(pid_t *, int, pid_t);
3080Sstevel@tonic-gate static	void	add_ugentry(struct ughead *, char *);
3090Sstevel@tonic-gate static	int	uconv(struct ughead *);
3100Sstevel@tonic-gate static	int	gconv(struct ughead *);
3114321Scasper static	int	ugfind(id_t, struct ughead *);
3120Sstevel@tonic-gate static	void	prtime(timestruc_t, int, int);
3130Sstevel@tonic-gate static	void	przom(psinfo_t *);
3140Sstevel@tonic-gate static	int	namencnt(char *, int, int);
3150Sstevel@tonic-gate static	char	*err_string(int);
3160Sstevel@tonic-gate static	int	print_proc(char *pname);
3170Sstevel@tonic-gate static	time_t	delta_secs(const timestruc_t *);
3180Sstevel@tonic-gate static	int	str2id(const char *, pid_t *, long, long);
3194321Scasper static	int	str2uid(const char *,  uid_t *, unsigned long, unsigned long);
3200Sstevel@tonic-gate static	void	*Realloc(void *, size_t);
3210Sstevel@tonic-gate static	int	pidcmp(const void *p1, const void *p2);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate int
3240Sstevel@tonic-gate main(int argc, char **argv)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	char	*p;
3270Sstevel@tonic-gate 	char	*p1;
3280Sstevel@tonic-gate 	char	*parg;
3290Sstevel@tonic-gate 	int	c;
3300Sstevel@tonic-gate 	int	i;
3310Sstevel@tonic-gate 	int	pgerrflg = 0;	/* err flg: non-numeric arg w/p & g options */
3322966Ssayama 	size_t	size, len;
3330Sstevel@tonic-gate 	DIR	*dirp;
3340Sstevel@tonic-gate 	struct dirent *dentp;
3350Sstevel@tonic-gate 	pid_t	maxpid;
3360Sstevel@tonic-gate 	pid_t	id;
3370Sstevel@tonic-gate 	int	ret;
3382966Ssayama 	char	loc_stime_str[32];
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3410Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3420Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
3430Sstevel@tonic-gate #endif
3440Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	(void) memset(&euid_tbl, 0, sizeof (euid_tbl));
3470Sstevel@tonic-gate 	(void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
3480Sstevel@tonic-gate 	(void) memset(&egid_tbl, 0, sizeof (egid_tbl));
3490Sstevel@tonic-gate 	(void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * calculate width of pid fields based on configured MAXPID
3570Sstevel@tonic-gate 	 * (must be at least 5 to retain output format compatibility)
3580Sstevel@tonic-gate 	 */
3590Sstevel@tonic-gate 	id = maxpid = (pid_t)sysconf(_SC_MAXPID);
3600Sstevel@tonic-gate 	pidwidth = 1;
3610Sstevel@tonic-gate 	while ((id /= 10) > 0)
3620Sstevel@tonic-gate 		++pidwidth;
3630Sstevel@tonic-gate 	pidwidth = pidwidth < 5 ? 5 : pidwidth;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	fname[F_PID].width = fname[F_PPID].width = pidwidth;
3660Sstevel@tonic-gate 	fname[F_PGID].width = fname[F_SID].width = pidwidth;
3670Sstevel@tonic-gate 
3682966Ssayama 	/*
3692966Ssayama 	 * TRANSLATION_NOTE
3702966Ssayama 	 * Specify the printf format with width and precision for
3712966Ssayama 	 * the STIME field.
3722966Ssayama 	 */
3732966Ssayama 	len = snprintf(loc_stime_str, sizeof (loc_stime_str),
3742966Ssayama 	    dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
3752966Ssayama 	if (len >= sizeof (loc_stime_str))
3762966Ssayama 		len = sizeof (loc_stime_str) - 1;
3772966Ssayama 
3782966Ssayama 	fname[F_STIME].width = fname[F_STIME].minwidth = len;
3792966Ssayama 
3802685Sakolb 	while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
3812685Sakolb 	    != EOF)
3820Sstevel@tonic-gate 		switch (c) {
3832685Sakolb 		case 'H':		/* Show home lgroups */
3842685Sakolb 			Hflg++;
3852685Sakolb 			break;
3862685Sakolb 		case 'h':
3872685Sakolb 			/*
3882685Sakolb 			 * Show processes/threads with given home lgroups
3892685Sakolb 			 */
3902685Sakolb 			hflg++;
3912685Sakolb 			p1 = optarg;
3922685Sakolb 			do {
3932685Sakolb 				int id;
3942685Sakolb 
3952685Sakolb 				/*
3962685Sakolb 				 * Get all IDs in the list, verify for
3972685Sakolb 				 * correctness and place in lgrps array.
3982685Sakolb 				 */
3992685Sakolb 				parg = getarg(&p1);
4002685Sakolb 				/* Convert string to integer */
4012685Sakolb 				ret = str2id(parg, (pid_t *)&id, 0,
402*8233Sdme@sun.com 				    MAX_LGRP_ID);
4032685Sakolb 				/* Complain if ID didn't parse correctly */
4042685Sakolb 				if (ret != 0) {
4052685Sakolb 					pgerrflg++;
4062685Sakolb 					(void) fprintf(stderr,
4072685Sakolb 					    gettext("ps: %s "), parg);
4082685Sakolb 					if (ret == EINVAL)
4092685Sakolb 						(void) fprintf(stderr,
4102685Sakolb 						    gettext("is an invalid "
4112685Sakolb 						    "non-numeric argument"));
4122685Sakolb 					else
4132685Sakolb 						(void) fprintf(stderr,
4142685Sakolb 						    gettext("exceeds valid "
4152685Sakolb 						    "range"));
4162685Sakolb 					(void) fprintf(stderr,
4172685Sakolb 					    gettext(" for -h option\n"));
4182685Sakolb 					continue;
4192685Sakolb 				}
4202685Sakolb 
4212685Sakolb 				/* Extend lgrps array if needed */
4222685Sakolb 				if (nlgrps == lgrps_size) {
4232685Sakolb 					/* Double the size of the lgrps array */
4242685Sakolb 					if (lgrps_size == 0)
4252685Sakolb 						lgrps_size = SIZ;
4262685Sakolb 					lgrps_size *= 2;
4272685Sakolb 					lgrps = Realloc(lgrps,
4282685Sakolb 					    lgrps_size * sizeof (int));
4292685Sakolb 				}
4302685Sakolb 				/* place the id in the lgrps table */
4312685Sakolb 				lgrps[nlgrps++] = id;
4322685Sakolb 			} while (*p1);
4332685Sakolb 			break;
4340Sstevel@tonic-gate 		case 'l':		/* long listing */
4350Sstevel@tonic-gate 			lflg++;
4360Sstevel@tonic-gate 			break;
4370Sstevel@tonic-gate 		case 'f':		/* full listing */
4380Sstevel@tonic-gate 			fflg++;
4390Sstevel@tonic-gate 			break;
4400Sstevel@tonic-gate 		case 'j':
4410Sstevel@tonic-gate 			jflg++;
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 		case 'c':
4440Sstevel@tonic-gate 			/*
4450Sstevel@tonic-gate 			 * Format output to reflect scheduler changes:
4460Sstevel@tonic-gate 			 * high numbers for high priorities and don't
4470Sstevel@tonic-gate 			 * print nice or p_cpu values.  'c' option only
4480Sstevel@tonic-gate 			 * effective when used with 'l' or 'f' options.
4490Sstevel@tonic-gate 			 */
4500Sstevel@tonic-gate 			cflg++;
4510Sstevel@tonic-gate 			break;
4520Sstevel@tonic-gate 		case 'A':		/* list every process */
4530Sstevel@tonic-gate 		case 'e':		/* (obsolete) list every process */
4540Sstevel@tonic-gate 			Aflg++;
4550Sstevel@tonic-gate 			tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
4562685Sakolb 			zflg = hflg = 0;
4570Sstevel@tonic-gate 			break;
4580Sstevel@tonic-gate 		case 'a':
4590Sstevel@tonic-gate 			/*
4600Sstevel@tonic-gate 			 * Same as 'e' except no session group leaders
4610Sstevel@tonic-gate 			 * and no non-terminal processes.
4620Sstevel@tonic-gate 			 */
4630Sstevel@tonic-gate 			aflg++;
4640Sstevel@tonic-gate 			break;
4650Sstevel@tonic-gate 		case 'd':	/* same as e except no session leaders */
4660Sstevel@tonic-gate 			dflg++;
4670Sstevel@tonic-gate 			break;
4680Sstevel@tonic-gate 		case 'L':	/* show lwps */
4690Sstevel@tonic-gate 			Lflg++;
4700Sstevel@tonic-gate 			break;
4710Sstevel@tonic-gate 		case 'P':	/* show bound processor */
4720Sstevel@tonic-gate 			Pflg++;
4730Sstevel@tonic-gate 			break;
4740Sstevel@tonic-gate 		case 'y':	/* omit F & ADDR, report RSS & SZ in Kby */
4750Sstevel@tonic-gate 			yflg++;
4760Sstevel@tonic-gate 			break;
4770Sstevel@tonic-gate 		case 'n':	/* no longer needed; retain as no-op */
4780Sstevel@tonic-gate 			(void) fprintf(stderr,
4790Sstevel@tonic-gate 			    gettext("ps: warning: -n option ignored\n"));
4800Sstevel@tonic-gate 			break;
4810Sstevel@tonic-gate 		case 't':		/* terminals */
4820Sstevel@tonic-gate #define	TSZ	30
4830Sstevel@tonic-gate 			tflg++;
4840Sstevel@tonic-gate 			p1 = optarg;
4850Sstevel@tonic-gate 			do {
4860Sstevel@tonic-gate 				char nambuf[TSZ+6];	/* for "/dev/" + '\0' */
4870Sstevel@tonic-gate 				struct stat64 s;
4880Sstevel@tonic-gate 				parg = getarg(&p1);
4890Sstevel@tonic-gate 				p = Realloc(NULL, TSZ+1);	/* for '\0' */
4900Sstevel@tonic-gate 				/* zero the buffer before using it */
4910Sstevel@tonic-gate 				p[0] = '\0';
4920Sstevel@tonic-gate 				size = TSZ;
4930Sstevel@tonic-gate 				if (isdigit(*parg)) {
4940Sstevel@tonic-gate 					(void) strcpy(p, "tty");
4950Sstevel@tonic-gate 					size -= 3;
4960Sstevel@tonic-gate 				}
4970Sstevel@tonic-gate 				(void) strncat(p, parg, size);
4980Sstevel@tonic-gate 				if (ntty == ttysz) {
4990Sstevel@tonic-gate 					if ((ttysz *= 2) == 0)
5000Sstevel@tonic-gate 						ttysz = NTTYS;
5010Sstevel@tonic-gate 					tty = Realloc(tty,
5020Sstevel@tonic-gate 					    (ttysz + 1) * sizeof (struct tty));
5030Sstevel@tonic-gate 				}
5040Sstevel@tonic-gate 				tty[ntty].tdev = PRNODEV;
5050Sstevel@tonic-gate 				(void) strcpy(nambuf, "/dev/");
5060Sstevel@tonic-gate 				(void) strcat(nambuf, p);
5070Sstevel@tonic-gate 				if (stat64(nambuf, &s) == 0)
5080Sstevel@tonic-gate 					tty[ntty].tdev = s.st_rdev;
5090Sstevel@tonic-gate 				tty[ntty++].tname = p;
5100Sstevel@tonic-gate 			} while (*p1);
5110Sstevel@tonic-gate 			break;
5120Sstevel@tonic-gate 		case 'p':		/* proc ids */
5130Sstevel@tonic-gate 			pflg++;
5140Sstevel@tonic-gate 			p1 = optarg;
5150Sstevel@tonic-gate 			do {
5160Sstevel@tonic-gate 				pid_t id;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 				parg = getarg(&p1);
5190Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5200Sstevel@tonic-gate 					pgerrflg++;
5210Sstevel@tonic-gate 					(void) fprintf(stderr,
5220Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5230Sstevel@tonic-gate 					if (ret == EINVAL)
5240Sstevel@tonic-gate 						(void) fprintf(stderr,
5250Sstevel@tonic-gate 						    gettext("is an invalid "
5260Sstevel@tonic-gate 						    "non-numeric argument"));
5270Sstevel@tonic-gate 					else
5280Sstevel@tonic-gate 						(void) fprintf(stderr,
5290Sstevel@tonic-gate 						    gettext("exceeds valid "
5300Sstevel@tonic-gate 						    "range"));
5310Sstevel@tonic-gate 					(void) fprintf(stderr,
5320Sstevel@tonic-gate 					    gettext(" for -p option\n"));
5330Sstevel@tonic-gate 					continue;
5340Sstevel@tonic-gate 				}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 				if (npid == pidsz) {
5370Sstevel@tonic-gate 					if ((pidsz *= 2) == 0)
5380Sstevel@tonic-gate 						pidsz = SIZ;
5390Sstevel@tonic-gate 					pid = Realloc(pid,
5400Sstevel@tonic-gate 					    pidsz * sizeof (pid_t));
5410Sstevel@tonic-gate 				}
5420Sstevel@tonic-gate 				pid[npid++] = id;
5430Sstevel@tonic-gate 			} while (*p1);
5440Sstevel@tonic-gate 			break;
5450Sstevel@tonic-gate 		case 's':		/* session */
5460Sstevel@tonic-gate 			sflg++;
5470Sstevel@tonic-gate 			p1 = optarg;
5480Sstevel@tonic-gate 			do {
5490Sstevel@tonic-gate 				pid_t id;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 				parg = getarg(&p1);
5520Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5530Sstevel@tonic-gate 					pgerrflg++;
5540Sstevel@tonic-gate 					(void) fprintf(stderr,
5550Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5560Sstevel@tonic-gate 					if (ret == EINVAL)
5570Sstevel@tonic-gate 						(void) fprintf(stderr,
5580Sstevel@tonic-gate 						    gettext("is an invalid "
5590Sstevel@tonic-gate 						    "non-numeric argument"));
5600Sstevel@tonic-gate 					else
5610Sstevel@tonic-gate 						(void) fprintf(stderr,
5620Sstevel@tonic-gate 						    gettext("exceeds valid "
5630Sstevel@tonic-gate 						    "range"));
5640Sstevel@tonic-gate 					(void) fprintf(stderr,
5650Sstevel@tonic-gate 					    gettext(" for -s option\n"));
5660Sstevel@tonic-gate 					continue;
5670Sstevel@tonic-gate 				}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 				if (nsessid == sessidsz) {
5700Sstevel@tonic-gate 					if ((sessidsz *= 2) == 0)
5710Sstevel@tonic-gate 						sessidsz = SIZ;
5720Sstevel@tonic-gate 					sessid = Realloc(sessid,
5730Sstevel@tonic-gate 					    sessidsz * sizeof (pid_t));
5740Sstevel@tonic-gate 				}
5750Sstevel@tonic-gate 				sessid[nsessid++] = id;
5760Sstevel@tonic-gate 			} while (*p1);
5770Sstevel@tonic-gate 			break;
5780Sstevel@tonic-gate 		case 'g':		/* proc group */
5790Sstevel@tonic-gate 			gflg++;
5800Sstevel@tonic-gate 			p1 = optarg;
5810Sstevel@tonic-gate 			do {
5820Sstevel@tonic-gate 				pid_t id;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 				parg = getarg(&p1);
5850Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5860Sstevel@tonic-gate 					pgerrflg++;
5870Sstevel@tonic-gate 					(void) fprintf(stderr,
5880Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5890Sstevel@tonic-gate 					if (ret == EINVAL)
5900Sstevel@tonic-gate 						(void) fprintf(stderr,
5910Sstevel@tonic-gate 						    gettext("is an invalid "
5920Sstevel@tonic-gate 						    "non-numeric argument"));
5930Sstevel@tonic-gate 					else
5940Sstevel@tonic-gate 						(void) fprintf(stderr,
5950Sstevel@tonic-gate 						    gettext("exceeds valid "
5960Sstevel@tonic-gate 						    "range"));
5970Sstevel@tonic-gate 					(void) fprintf(stderr,
5980Sstevel@tonic-gate 					    gettext(" for -g option\n"));
5990Sstevel@tonic-gate 					continue;
6000Sstevel@tonic-gate 				}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 				if (ngrpid == grpidsz) {
6030Sstevel@tonic-gate 					if ((grpidsz *= 2) == 0)
6040Sstevel@tonic-gate 						grpidsz = SIZ;
6050Sstevel@tonic-gate 					grpid = Realloc(grpid,
6060Sstevel@tonic-gate 					    grpidsz * sizeof (pid_t));
6070Sstevel@tonic-gate 				}
6080Sstevel@tonic-gate 				grpid[ngrpid++] = id;
6090Sstevel@tonic-gate 			} while (*p1);
6100Sstevel@tonic-gate 			break;
6110Sstevel@tonic-gate 		case 'u':		/* effective user name or number */
6120Sstevel@tonic-gate 			uflg++;
6130Sstevel@tonic-gate 			p1 = optarg;
6140Sstevel@tonic-gate 			do {
6150Sstevel@tonic-gate 				parg = getarg(&p1);
6160Sstevel@tonic-gate 				add_ugentry(&euid_tbl, parg);
6170Sstevel@tonic-gate 			} while (*p1);
6180Sstevel@tonic-gate 			break;
6190Sstevel@tonic-gate 		case 'U':		/* real user name or number */
6200Sstevel@tonic-gate 			Uflg++;
6210Sstevel@tonic-gate 			p1 = optarg;
6220Sstevel@tonic-gate 			do {
6230Sstevel@tonic-gate 				parg = getarg(&p1);
6240Sstevel@tonic-gate 				add_ugentry(&ruid_tbl, parg);
6250Sstevel@tonic-gate 			} while (*p1);
6260Sstevel@tonic-gate 			break;
6270Sstevel@tonic-gate 		case 'G':		/* real group name or number */
6280Sstevel@tonic-gate 			Gflg++;
6290Sstevel@tonic-gate 			p1 = optarg;
6300Sstevel@tonic-gate 			do {
6310Sstevel@tonic-gate 				parg = getarg(&p1);
6320Sstevel@tonic-gate 				add_ugentry(&rgid_tbl, parg);
6330Sstevel@tonic-gate 			} while (*p1);
6340Sstevel@tonic-gate 			break;
6350Sstevel@tonic-gate 		case 'o':		/* output format */
6360Sstevel@tonic-gate 			p = optarg;
6370Sstevel@tonic-gate 			while ((p = parse_format(p)) != NULL)
6380Sstevel@tonic-gate 				;
6390Sstevel@tonic-gate 			break;
6400Sstevel@tonic-gate 		case 'z':		/* zone name or number */
6410Sstevel@tonic-gate 			zflg++;
6420Sstevel@tonic-gate 			p1 = optarg;
6430Sstevel@tonic-gate 			do {
6440Sstevel@tonic-gate 				zoneid_t id;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 				parg = getarg(&p1);
6470Sstevel@tonic-gate 				if (zone_get_id(parg, &id) != 0) {
6480Sstevel@tonic-gate 					pgerrflg++;
6490Sstevel@tonic-gate 					(void) fprintf(stderr,
6500Sstevel@tonic-gate 					    gettext("ps: unknown zone %s\n"),
6510Sstevel@tonic-gate 					    parg);
6520Sstevel@tonic-gate 					continue;
6530Sstevel@tonic-gate 				}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 				if (nzoneid == zoneidsz) {
6560Sstevel@tonic-gate 					if ((zoneidsz *= 2) == 0)
6570Sstevel@tonic-gate 						zoneidsz = SIZ;
6580Sstevel@tonic-gate 					zoneid = Realloc(zoneid,
6590Sstevel@tonic-gate 					    zoneidsz * sizeof (zoneid_t));
6600Sstevel@tonic-gate 				}
6610Sstevel@tonic-gate 				zoneid[nzoneid++] = id;
6620Sstevel@tonic-gate 			} while (*p1);
6630Sstevel@tonic-gate 			break;
6640Sstevel@tonic-gate 		case 'Z':		/* show zone name */
6650Sstevel@tonic-gate 			Zflg++;
6660Sstevel@tonic-gate 			break;
6670Sstevel@tonic-gate 		default:			/* error on ? */
6680Sstevel@tonic-gate 			errflg++;
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (errflg || optind < argc || pgerrflg)
6730Sstevel@tonic-gate 		usage();
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (tflg)
6760Sstevel@tonic-gate 		tty[ntty].tname = NULL;
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * If an appropriate option has not been specified, use the
6790Sstevel@tonic-gate 	 * current terminal and effective uid as the default.
6800Sstevel@tonic-gate 	 */
6812685Sakolb 	if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
6820Sstevel@tonic-gate 		psinfo_t info;
6830Sstevel@tonic-gate 		int procfd;
6840Sstevel@tonic-gate 		char *name;
6850Sstevel@tonic-gate 		char pname[100];
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		/* get our own controlling tty name using /proc */
6880Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname),
6890Sstevel@tonic-gate 		    "%s/self/psinfo", procdir);
6900Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0 ||
6910Sstevel@tonic-gate 		    read(procfd, (char *)&info, sizeof (info)) < 0 ||
6920Sstevel@tonic-gate 		    info.pr_ttydev == PRNODEV) {
6930Sstevel@tonic-gate 			(void) fprintf(stderr,
6940Sstevel@tonic-gate 			    gettext("ps: no controlling terminal\n"));
6950Sstevel@tonic-gate 			exit(1);
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 		(void) close(procfd);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		i = 0;
7000Sstevel@tonic-gate 		name = gettty(&info);
7010Sstevel@tonic-gate 		if (*name == '?') {
7020Sstevel@tonic-gate 			(void) fprintf(stderr,
7030Sstevel@tonic-gate 			    gettext("ps: can't find controlling terminal\n"));
7040Sstevel@tonic-gate 			exit(1);
7050Sstevel@tonic-gate 		}
7060Sstevel@tonic-gate 		if (ntty == ttysz) {
7070Sstevel@tonic-gate 			if ((ttysz *= 2) == 0)
7080Sstevel@tonic-gate 				ttysz = NTTYS;
7090Sstevel@tonic-gate 			tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 		tty[ntty].tdev = info.pr_ttydev;
7120Sstevel@tonic-gate 		tty[ntty++].tname = name;
7130Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7140Sstevel@tonic-gate 		tflg++;
7150Sstevel@tonic-gate 		tuid = getuid();
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 	if (Aflg) {
7180Sstevel@tonic-gate 		Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
7192685Sakolb 		zflg = hflg = 0;
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	if (Aflg | aflg | dflg)
7220Sstevel@tonic-gate 		tflg = 0;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	i = 0;		/* prepare to exit on name lookup errors */
7250Sstevel@tonic-gate 	i += uconv(&euid_tbl);
7260Sstevel@tonic-gate 	i += uconv(&ruid_tbl);
7270Sstevel@tonic-gate 	i += gconv(&egid_tbl);
7280Sstevel@tonic-gate 	i += gconv(&rgid_tbl);
7290Sstevel@tonic-gate 	if (i)
7300Sstevel@tonic-gate 		exit(1);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* allocate a buffer for lwpsinfo structures */
7330Sstevel@tonic-gate 	lpbufsize = 4096;
7340Sstevel@tonic-gate 	if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
7350Sstevel@tonic-gate 		(void) fprintf(stderr,
7360Sstevel@tonic-gate 		    gettext("ps: no memory\n"));
7370Sstevel@tonic-gate 		exit(1);
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (fields) {	/* print user-specified header */
7410Sstevel@tonic-gate 		if (do_header) {
7420Sstevel@tonic-gate 			struct field *f;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 			for (f = fields; f != NULL; f = f->next) {
7450Sstevel@tonic-gate 				if (f != fields)
7460Sstevel@tonic-gate 					(void) printf(" ");
7470Sstevel@tonic-gate 				switch (f->fname) {
7480Sstevel@tonic-gate 				case F_TTY:
7490Sstevel@tonic-gate 					(void) printf("%-*s",
7500Sstevel@tonic-gate 					    f->width, f->header);
7510Sstevel@tonic-gate 					break;
7520Sstevel@tonic-gate 				case F_FNAME:
7530Sstevel@tonic-gate 				case F_COMM:
7540Sstevel@tonic-gate 				case F_ARGS:
7550Sstevel@tonic-gate 					/*
7560Sstevel@tonic-gate 					 * Print these headers full width
7570Sstevel@tonic-gate 					 * unless they appear at the end.
7580Sstevel@tonic-gate 					 */
7590Sstevel@tonic-gate 					if (f->next != NULL) {
7600Sstevel@tonic-gate 						(void) printf("%-*s",
7610Sstevel@tonic-gate 						    f->width, f->header);
7620Sstevel@tonic-gate 					} else {
7630Sstevel@tonic-gate 						(void) printf("%s",
7640Sstevel@tonic-gate 						    f->header);
7650Sstevel@tonic-gate 					}
7660Sstevel@tonic-gate 					break;
7670Sstevel@tonic-gate 				default:
7680Sstevel@tonic-gate 					(void) printf("%*s",
7690Sstevel@tonic-gate 					    f->width, f->header);
7700Sstevel@tonic-gate 					break;
7710Sstevel@tonic-gate 				}
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 			(void) printf("\n");
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 	} else {	/* print standard header */
7760Sstevel@tonic-gate 		if (lflg) {
7770Sstevel@tonic-gate 			if (yflg)
7780Sstevel@tonic-gate 				(void) printf(" S");
7790Sstevel@tonic-gate 			else
7800Sstevel@tonic-gate 				(void) printf(" F S");
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		if (Zflg)
7830Sstevel@tonic-gate 			(void) printf("    ZONE");
7840Sstevel@tonic-gate 		if (fflg) {
7850Sstevel@tonic-gate 			if (lflg)
7860Sstevel@tonic-gate 				(void) printf(" ");
7870Sstevel@tonic-gate 			(void) printf("     UID");
7880Sstevel@tonic-gate 		} else if (lflg)
7890Sstevel@tonic-gate 			(void) printf("    UID");
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		(void) printf(" %*s", pidwidth,  "PID");
7920Sstevel@tonic-gate 		if (lflg || fflg)
7930Sstevel@tonic-gate 			(void) printf(" %*s", pidwidth, "PPID");
7940Sstevel@tonic-gate 		if (jflg)
7950Sstevel@tonic-gate 			(void) printf(" %*s %*s", pidwidth, "PGID",
7960Sstevel@tonic-gate 			    pidwidth, "SID");
7970Sstevel@tonic-gate 		if (Lflg)
7980Sstevel@tonic-gate 			(void) printf("   LWP");
7990Sstevel@tonic-gate 		if (Pflg)
8000Sstevel@tonic-gate 			(void) printf(" PSR");
8010Sstevel@tonic-gate 		if (Lflg && fflg)
8020Sstevel@tonic-gate 			(void) printf("  NLWP");
8030Sstevel@tonic-gate 		if (cflg)
8040Sstevel@tonic-gate 			(void) printf("  CLS PRI");
8050Sstevel@tonic-gate 		else if (lflg || fflg) {
8060Sstevel@tonic-gate 			(void) printf("   C");
8070Sstevel@tonic-gate 			if (lflg)
8080Sstevel@tonic-gate 				(void) printf(" PRI NI");
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		if (lflg) {
8110Sstevel@tonic-gate 			if (yflg)
8120Sstevel@tonic-gate 				(void) printf("   RSS     SZ    WCHAN");
8130Sstevel@tonic-gate 			else
8140Sstevel@tonic-gate 				(void) printf("     ADDR     SZ    WCHAN");
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		if (fflg)
8172966Ssayama 			(void) printf(" %s", loc_stime_str);
8182685Sakolb 		if (Hflg)
8192685Sakolb 			(void) printf(" LGRP");
8200Sstevel@tonic-gate 		if (Lflg)
8210Sstevel@tonic-gate 			(void) printf(" TTY        LTIME CMD\n");
8220Sstevel@tonic-gate 		else
8230Sstevel@tonic-gate 			(void) printf(" TTY         TIME CMD\n");
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 
8272685Sakolb 	if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
8280Sstevel@tonic-gate 	    npid <= PTHRESHOLD) {
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * If we are looking at specific processes go straight
8310Sstevel@tonic-gate 		 * to their /proc entries and don't scan /proc.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		int i;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		(void) qsort(pid, npid, sizeof (pid_t), pidcmp);
8360Sstevel@tonic-gate 		for (i = 0; i < npid; i++) {
8370Sstevel@tonic-gate 			char pname[12];
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 			if (i >= 1 && pid[i] == pid[i - 1])
8400Sstevel@tonic-gate 				continue;
8410Sstevel@tonic-gate 			(void) sprintf(pname, "%d", (int)pid[i]);
8420Sstevel@tonic-gate 			if (print_proc(pname) == 0)
8430Sstevel@tonic-gate 				retcode = 0;
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 	} else {
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * Determine which processes to print info about by searching
8480Sstevel@tonic-gate 		 * the /proc directory and looking at each process.
8490Sstevel@tonic-gate 		 */
8500Sstevel@tonic-gate 		if ((dirp = opendir(procdir)) == NULL) {
8510Sstevel@tonic-gate 			(void) fprintf(stderr,
8520Sstevel@tonic-gate 			    gettext("ps: cannot open PROC directory %s\n"),
8530Sstevel@tonic-gate 			    procdir);
8540Sstevel@tonic-gate 			exit(1);
8550Sstevel@tonic-gate 		}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 		/* for each active process --- */
8580Sstevel@tonic-gate 		while (dentp = readdir(dirp)) {
8590Sstevel@tonic-gate 			if (dentp->d_name[0] == '.')    /* skip . and .. */
8600Sstevel@tonic-gate 				continue;
8610Sstevel@tonic-gate 			if (print_proc(dentp->d_name) == 0)
8620Sstevel@tonic-gate 				retcode = 0;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 		(void) closedir(dirp);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	return (retcode);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate int
8720Sstevel@tonic-gate print_proc(char *pid_name)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	char	pname[PATH_MAX];
8750Sstevel@tonic-gate 	int	pdlen;
8760Sstevel@tonic-gate 	int	found;
8770Sstevel@tonic-gate 	int	procfd; /* filedescriptor for /proc/nnnnn/psinfo */
8780Sstevel@tonic-gate 	char	*tp;    /* ptr to ttyname,  if any */
8790Sstevel@tonic-gate 	psinfo_t info;  /* process information from /proc */
8800Sstevel@tonic-gate 	lwpsinfo_t *lwpsinfo;   /* array of lwpsinfo structs */
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
8830Sstevel@tonic-gate 	if (pdlen >= sizeof (pname) - 10)
8840Sstevel@tonic-gate 		return (1);
8850Sstevel@tonic-gate retry:
8860Sstevel@tonic-gate 	(void) strcpy(&pname[pdlen], "psinfo");
8870Sstevel@tonic-gate 	if ((procfd = open(pname, O_RDONLY)) == -1) {
8880Sstevel@tonic-gate 		/* Process may have exited meanwhile. */
8890Sstevel@tonic-gate 		return (1);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * Get the info structure for the process and close quickly.
8930Sstevel@tonic-gate 	 */
8940Sstevel@tonic-gate 	if (read(procfd, (char *)&info, sizeof (info)) < 0) {
8950Sstevel@tonic-gate 		int	saverr = errno;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		(void) close(procfd);
8980Sstevel@tonic-gate 		if (saverr == EAGAIN)
8990Sstevel@tonic-gate 			goto retry;
9000Sstevel@tonic-gate 		if (saverr != ENOENT)
9010Sstevel@tonic-gate 			(void) fprintf(stderr,
9020Sstevel@tonic-gate 			    gettext("ps: read() on %s: %s\n"),
9030Sstevel@tonic-gate 			    pname, err_string(saverr));
9040Sstevel@tonic-gate 		return (1);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	(void) close(procfd);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	found = 0;
9090Sstevel@tonic-gate 	if (info.pr_lwp.pr_state == 0)	/* can't happen? */
9100Sstevel@tonic-gate 		return (1);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Omit session group leaders for 'a' and 'd' options.
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
9160Sstevel@tonic-gate 		return (1);
9170Sstevel@tonic-gate 	if (Aflg || dflg)
9180Sstevel@tonic-gate 		found++;
9190Sstevel@tonic-gate 	else if (pflg && search(pid, npid, info.pr_pid))
9200Sstevel@tonic-gate 		found++;	/* ppid in p option arg list */
9214321Scasper 	else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
9220Sstevel@tonic-gate 		found++;	/* puid in u option arg list */
9234321Scasper 	else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
9240Sstevel@tonic-gate 		found++;	/* puid in U option arg list */
9250Sstevel@tonic-gate #ifdef NOT_YET
9264321Scasper 	else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
9270Sstevel@tonic-gate 		found++;	/* pgid in g option arg list */
9280Sstevel@tonic-gate #endif	/* NOT_YET */
9294321Scasper 	else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
9300Sstevel@tonic-gate 		found++;	/* pgid in G option arg list */
9310Sstevel@tonic-gate 	else if (gflg && search(grpid, ngrpid, info.pr_pgid))
9320Sstevel@tonic-gate 		found++;	/* grpid in g option arg list */
9330Sstevel@tonic-gate 	else if (sflg && search(sessid, nsessid, info.pr_sid))
9340Sstevel@tonic-gate 		found++;	/* sessid in s option arg list */
9350Sstevel@tonic-gate 	else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
9360Sstevel@tonic-gate 		found++;	/* zoneid in z option arg list */
9372685Sakolb 	else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
9382685Sakolb 		found++;	/* home lgroup in h option arg list */
9390Sstevel@tonic-gate 	if (!found && !tflg && !aflg)
9400Sstevel@tonic-gate 		return (1);
9410Sstevel@tonic-gate 	if (!prfind(found, &info, &tp))
9420Sstevel@tonic-gate 		return (1);
9430Sstevel@tonic-gate 	if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
9440Sstevel@tonic-gate 		ssize_t prsz;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		(void) strcpy(&pname[pdlen], "lpsinfo");
9470Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
9480Sstevel@tonic-gate 			return (1);
9490Sstevel@tonic-gate 		/*
9500Sstevel@tonic-gate 		 * Get the info structures for the lwps.
9510Sstevel@tonic-gate 		 */
9520Sstevel@tonic-gate 		prsz = read(procfd, lpsinfobuf, lpbufsize);
9530Sstevel@tonic-gate 		if (prsz == -1) {
9540Sstevel@tonic-gate 			int	saverr = errno;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 			(void) close(procfd);
9570Sstevel@tonic-gate 			if (saverr == EAGAIN)
9580Sstevel@tonic-gate 				goto retry;
9590Sstevel@tonic-gate 			if (saverr != ENOENT)
9600Sstevel@tonic-gate 				(void) fprintf(stderr,
9610Sstevel@tonic-gate 				    gettext("ps: read() on %s: %s\n"),
9620Sstevel@tonic-gate 				    pname, err_string(saverr));
9630Sstevel@tonic-gate 			return (1);
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 		(void) close(procfd);
9660Sstevel@tonic-gate 		if (prsz == lpbufsize) {
9670Sstevel@tonic-gate 			/*
9680Sstevel@tonic-gate 			 * buffer overflow. Realloc new buffer.
9690Sstevel@tonic-gate 			 * Error handling is done in Realloc().
9700Sstevel@tonic-gate 			 */
9710Sstevel@tonic-gate 			lpbufsize *= 2;
9720Sstevel@tonic-gate 			lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
9730Sstevel@tonic-gate 			goto retry;
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
9760Sstevel@tonic-gate 			goto retry;
9770Sstevel@tonic-gate 		lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 	if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
9800Sstevel@tonic-gate 		prcom(&info, tp);
9810Sstevel@tonic-gate 	} else {
9820Sstevel@tonic-gate 		int nlwp = 0;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		do {
9850Sstevel@tonic-gate 			info.pr_lwp = *lwpsinfo;
9860Sstevel@tonic-gate 			prcom(&info, tp);
9870Sstevel@tonic-gate 			/* LINTED improper alignment */
9880Sstevel@tonic-gate 			lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
989*8233Sdme@sun.com 			    lpsinfobuf->pr_entsize);
9900Sstevel@tonic-gate 		} while (++nlwp < lpsinfobuf->pr_nent);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	return (0);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate static void
9970Sstevel@tonic-gate usage(void)		/* print usage message and quit */
9980Sstevel@tonic-gate {
9990Sstevel@tonic-gate 	static char usage1[] =
10002685Sakolb 	    "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
10010Sstevel@tonic-gate 	static char usage2[] =
10020Sstevel@tonic-gate 	    "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
10030Sstevel@tonic-gate 	static char usage3[] =
10042685Sakolb 	    "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] "
10052685Sakolb 	    "[-h lgrplist]";
10060Sstevel@tonic-gate 	static char usage4[] =
10070Sstevel@tonic-gate 	    "  'format' is one or more of:";
10080Sstevel@tonic-gate 	static char usage5[] =
10090Sstevel@tonic-gate 	    "\tuser ruser group rgroup uid ruid gid rgid pid ppid pgid "
10100Sstevel@tonic-gate 	    "sid taskid ctid";
10110Sstevel@tonic-gate 	static char usage6[] =
10120Sstevel@tonic-gate 	    "\tpri opri pcpu pmem vsz rss osz nice class time etime stime zone "
10130Sstevel@tonic-gate 	    "zoneid";
10140Sstevel@tonic-gate 	static char usage7[] =
10150Sstevel@tonic-gate 	    "\tf s c lwp nlwp psr tty addr wchan fname comm args "
10162685Sakolb 	    "projid project pset lgrp";
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	(void) fprintf(stderr,
10190Sstevel@tonic-gate 	    gettext("usage: %s\n%s\n%s\n%s\n%s\n%s\n%s\n"),
10200Sstevel@tonic-gate 	    gettext(usage1), gettext(usage2), gettext(usage3),
10210Sstevel@tonic-gate 	    gettext(usage4), gettext(usage5), gettext(usage6), gettext(usage7));
10220Sstevel@tonic-gate 	exit(1);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate  * getarg() finds the next argument in list and copies arg into argbuf.
10270Sstevel@tonic-gate  * p1 first pts to arg passed back from getopt routine.  p1 is then
10280Sstevel@tonic-gate  * bumped to next character that is not a comma or blank -- p1 NULL
10290Sstevel@tonic-gate  * indicates end of list.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate static char *
10320Sstevel@tonic-gate getarg(char **pp1)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	static char argbuf[ARGSIZ];
10350Sstevel@tonic-gate 	char *p1 = *pp1;
10360Sstevel@tonic-gate 	char *parga = argbuf;
10370Sstevel@tonic-gate 	int c;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10400Sstevel@tonic-gate 		p1++;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
10430Sstevel@tonic-gate 		if (parga < argbuf + ARGSIZ - 1)
10440Sstevel@tonic-gate 			*parga++ = c;
10450Sstevel@tonic-gate 		p1++;
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	*parga = '\0';
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10500Sstevel@tonic-gate 		p1++;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	*pp1 = p1;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	return (argbuf);
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate  * parse_format() takes the argument to the -o option,
10590Sstevel@tonic-gate  * sets up the next output field structure, and returns
10600Sstevel@tonic-gate  * a pointer to any further output field specifier(s).
10610Sstevel@tonic-gate  * As a side-effect, it increments errflg if encounters a format error.
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate static char *
10640Sstevel@tonic-gate parse_format(char *arg)
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate 	int c;
10670Sstevel@tonic-gate 	char *name;
10680Sstevel@tonic-gate 	char *header = NULL;
10690Sstevel@tonic-gate 	int width = 0;
10700Sstevel@tonic-gate 	struct def_field *df;
10710Sstevel@tonic-gate 	struct field *f;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
10740Sstevel@tonic-gate 		arg++;
10750Sstevel@tonic-gate 	if (c == '\0')
10760Sstevel@tonic-gate 		return (NULL);
10770Sstevel@tonic-gate 	name = arg;
10780Sstevel@tonic-gate 	arg = strpbrk(arg, " \t\r\v\f\n,=");
10790Sstevel@tonic-gate 	if (arg != NULL) {
10800Sstevel@tonic-gate 		c = *arg;
10810Sstevel@tonic-gate 		*arg++ = '\0';
10820Sstevel@tonic-gate 		if (c == '=') {
10830Sstevel@tonic-gate 			char *s;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 			header = arg;
10860Sstevel@tonic-gate 			arg = NULL;
10870Sstevel@tonic-gate 			width = strlen(header);
10880Sstevel@tonic-gate 			s = header + width;
10890Sstevel@tonic-gate 			while (s > header && isspace(*--s))
10900Sstevel@tonic-gate 				*s = '\0';
10910Sstevel@tonic-gate 			while (isspace(*header))
10920Sstevel@tonic-gate 				header++;
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	for (df = &fname[0]; df < &fname[NFIELDS]; df++)
10960Sstevel@tonic-gate 		if (strcmp(name, df->fname) == 0) {
10970Sstevel@tonic-gate 			if (strcmp(name, "lwp") == 0)
10980Sstevel@tonic-gate 				Lflg++;
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 	if (df >= &fname[NFIELDS]) {
11020Sstevel@tonic-gate 		(void) fprintf(stderr,
1103*8233Sdme@sun.com 		    gettext("ps: unknown output format: -o %s\n"),
1104*8233Sdme@sun.com 		    name);
11050Sstevel@tonic-gate 		errflg++;
11060Sstevel@tonic-gate 		return (arg);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 	if ((f = malloc(sizeof (*f))) == NULL) {
11090Sstevel@tonic-gate 		(void) fprintf(stderr,
11100Sstevel@tonic-gate 		    gettext("ps: malloc() for output format failed, %s\n"),
11110Sstevel@tonic-gate 		    err_string(errno));
11120Sstevel@tonic-gate 		exit(1);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	f->next = NULL;
11150Sstevel@tonic-gate 	f->fname = df - &fname[0];
11160Sstevel@tonic-gate 	f->header = header? header : df->header;
11170Sstevel@tonic-gate 	if (width == 0)
11180Sstevel@tonic-gate 		width = df->width;
11190Sstevel@tonic-gate 	if (*f->header != '\0')
11200Sstevel@tonic-gate 		do_header = 1;
11210Sstevel@tonic-gate 	f->width = max(width, df->minwidth);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (fields == NULL)
11240Sstevel@tonic-gate 		fields = last_field = f;
11250Sstevel@tonic-gate 	else {
11260Sstevel@tonic-gate 		last_field->next = f;
11270Sstevel@tonic-gate 		last_field = f;
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	return (arg);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate static char *
11340Sstevel@tonic-gate devlookup(dev_t ddev)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 	struct devl *dp;
11370Sstevel@tonic-gate 	int i;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	for (dp = devl, i = 0; i < ndev; dp++, i++) {
11400Sstevel@tonic-gate 		if (dp->ddev == ddev)
11410Sstevel@tonic-gate 			return (dp->dname);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	return (NULL);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate static char *
11470Sstevel@tonic-gate devadd(char *name, dev_t ddev)
11480Sstevel@tonic-gate {
11490Sstevel@tonic-gate 	struct devl *dp;
11500Sstevel@tonic-gate 	int leng, start, i;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if (ndev == maxdev) {
11530Sstevel@tonic-gate 		maxdev += DNINCR;
11540Sstevel@tonic-gate 		devl = Realloc(devl, maxdev * sizeof (struct devl));
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 	dp = &devl[ndev++];
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	dp->ddev = ddev;
11590Sstevel@tonic-gate 	if (name == NULL) {
11600Sstevel@tonic-gate 		(void) strcpy(dp->dname, "??");
11610Sstevel@tonic-gate 		return (dp->dname);
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	leng = strlen(name);
11650Sstevel@tonic-gate 	/* Strip off /dev/ */
11660Sstevel@tonic-gate 	if (leng < DNSIZE + 4)
11670Sstevel@tonic-gate 		(void) strcpy(dp->dname, &name[5]);
11680Sstevel@tonic-gate 	else {
11690Sstevel@tonic-gate 		start = leng - DNSIZE - 1;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		for (i = start; i < leng && name[i] != '/'; i++)
11720Sstevel@tonic-gate 				;
11730Sstevel@tonic-gate 		if (i == leng)
11740Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[start], DNSIZE);
11750Sstevel@tonic-gate 		else
11760Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[i+1], DNSIZE);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 	return (dp->dname);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * gettty returns the user's tty number or ? if none.
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate static char *
11850Sstevel@tonic-gate gettty(psinfo_t *psinfo)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate 	extern char *_ttyname_dev(dev_t, char *, size_t);
11880Sstevel@tonic-gate 	char devname[TTYNAME_MAX];
11890Sstevel@tonic-gate 	char *retval;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (psinfo->pr_ttydev == PRNODEV)
11920Sstevel@tonic-gate 		return ("?");
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
11950Sstevel@tonic-gate 		return (retval);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	return (devadd(retval, psinfo->pr_ttydev));
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate  * Find the process's tty and return 1 if process is to be printed.
12040Sstevel@tonic-gate  */
12050Sstevel@tonic-gate static int
12060Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	char	*tp;
12090Sstevel@tonic-gate 	struct tty *ttyp;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
12120Sstevel@tonic-gate 		/* process is a zombie */
12130Sstevel@tonic-gate 		*tpp = "?";
12140Sstevel@tonic-gate 		if (tflg && !found)
12150Sstevel@tonic-gate 			return (0);
12160Sstevel@tonic-gate 		return (1);
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	/*
12200Sstevel@tonic-gate 	 * Get current terminal.  If none ("?") and 'a' is set, don't print
12210Sstevel@tonic-gate 	 * info.  If 't' is set, check if term is in list of desired terminals
12220Sstevel@tonic-gate 	 * and print it if it is.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate 	tp = gettty(psinfo);
12250Sstevel@tonic-gate 	if (aflg && *tp == '?') {
12260Sstevel@tonic-gate 		*tpp = tp;
12270Sstevel@tonic-gate 		return (0);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 	if (tflg && !found) {
12300Sstevel@tonic-gate 		int match = 0;
12310Sstevel@tonic-gate 		char *other = NULL;
12320Sstevel@tonic-gate 		for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
12330Sstevel@tonic-gate 			/*
12340Sstevel@tonic-gate 			 * Look for a name match
12350Sstevel@tonic-gate 			 */
12360Sstevel@tonic-gate 			if (strcmp(tp, ttyp->tname) == 0) {
12370Sstevel@tonic-gate 				match = 1;
12380Sstevel@tonic-gate 				break;
12390Sstevel@tonic-gate 			}
12400Sstevel@tonic-gate 			/*
12410Sstevel@tonic-gate 			 * Look for same device under different names.
12420Sstevel@tonic-gate 			 */
12430Sstevel@tonic-gate 			if ((other == NULL) &&
12440Sstevel@tonic-gate 			    (ttyp->tdev != PRNODEV) &&
12450Sstevel@tonic-gate 			    (psinfo->pr_ttydev == ttyp->tdev))
12460Sstevel@tonic-gate 				other = ttyp->tname;
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 		if (!match && (other != NULL)) {
12490Sstevel@tonic-gate 			/*
12500Sstevel@tonic-gate 			 * found under a different name
12510Sstevel@tonic-gate 			 */
12520Sstevel@tonic-gate 			match = 1;
12530Sstevel@tonic-gate 			tp = other;
12540Sstevel@tonic-gate 		}
12554321Scasper 		if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
12560Sstevel@tonic-gate 			/*
12570Sstevel@tonic-gate 			 * not found OR not matching euid
12580Sstevel@tonic-gate 			 */
12590Sstevel@tonic-gate 			*tpp = tp;
12600Sstevel@tonic-gate 			return (0);
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 	*tpp = tp;
12640Sstevel@tonic-gate 	return (1);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /*
12680Sstevel@tonic-gate  * Print info about the process.
12690Sstevel@tonic-gate  */
12700Sstevel@tonic-gate static void
12710Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	char	*cp;
12740Sstevel@tonic-gate 	long	tm;
12750Sstevel@tonic-gate 	int	bytesleft;
12760Sstevel@tonic-gate 	int	wcnt, length;
12770Sstevel@tonic-gate 	wchar_t	wchar;
12780Sstevel@tonic-gate 	struct passwd *pwd;
12790Sstevel@tonic-gate 	int	zombie_lwp;
12800Sstevel@tonic-gate 	char	zonename[ZONENAME_MAX];
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * If process is zombie, call zombie print routine and return.
12840Sstevel@tonic-gate 	 */
12850Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
12860Sstevel@tonic-gate 		if (fields != NULL)
12870Sstevel@tonic-gate 			pr_fields(psinfo, ttyp, print_zombie_field);
12880Sstevel@tonic-gate 		else
12890Sstevel@tonic-gate 			przom(psinfo);
12900Sstevel@tonic-gate 		return;
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * If user specified '-o format', print requested fields and return.
12970Sstevel@tonic-gate 	 */
12980Sstevel@tonic-gate 	if (fields != NULL) {
12990Sstevel@tonic-gate 		pr_fields(psinfo, ttyp, print_field);
13000Sstevel@tonic-gate 		return;
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
13050Sstevel@tonic-gate 	 * spearator, rather than keeping track of which column is first.  All
13060Sstevel@tonic-gate 	 * other fields are printed with a leading space.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	if (lflg) {
13090Sstevel@tonic-gate 		if (!yflg)
13100Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
13110Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (Zflg) {						/* ZONE */
13150Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
13160Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
13170Sstevel@tonic-gate 			(void) printf("%7.7d ", ((int)psinfo->pr_zoneid));
13180Sstevel@tonic-gate 		} else {
13190Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
13200Sstevel@tonic-gate 		}
13210Sstevel@tonic-gate 	}
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	if (fflg) {						/* UID */
13240Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
13250Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
13260Sstevel@tonic-gate 		else
13274321Scasper 			(void) printf("%7.7u ", psinfo->pr_euid);
13280Sstevel@tonic-gate 	} else if (lflg) {
13294321Scasper 		(void) printf("%6u ", psinfo->pr_euid);
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
13320Sstevel@tonic-gate 	if (lflg || fflg)
13330Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13340Sstevel@tonic-gate 		    (int)psinfo->pr_ppid); /* PPID */
13350Sstevel@tonic-gate 	if (jflg) {
13360Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13370Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);	/* PGID */
13380Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13390Sstevel@tonic-gate 		    (int)psinfo->pr_sid);	/* SID  */
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 	if (Lflg)
13420Sstevel@tonic-gate 		(void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
13430Sstevel@tonic-gate 	if (Pflg) {
13440Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE)	/* PSR */
13450Sstevel@tonic-gate 			(void) printf("   -");
13460Sstevel@tonic-gate 		else
13470Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 	if (Lflg && fflg)					/* NLWP */
13500Sstevel@tonic-gate 		(void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
13510Sstevel@tonic-gate 	if (cflg) {
13520Sstevel@tonic-gate 		if (zombie_lwp)					/* CLS */
13530Sstevel@tonic-gate 			(void) printf("     ");
13540Sstevel@tonic-gate 		else
13550Sstevel@tonic-gate 			(void) printf(" %4s", psinfo->pr_lwp.pr_clname);
13560Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI */
13570Sstevel@tonic-gate 	} else if (lflg || fflg) {
13580Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
13590Sstevel@tonic-gate 		if (lflg) {					    /* PRI NI */
13600Sstevel@tonic-gate 			/*
13610Sstevel@tonic-gate 			 * Print priorities the old way (lower numbers
13620Sstevel@tonic-gate 			 * mean higher priority) and print nice value
13630Sstevel@tonic-gate 			 * for time sharing procs.
13640Sstevel@tonic-gate 			 */
13650Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
13660Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_oldpri != 0)
13670Sstevel@tonic-gate 				(void) printf(" %2d", psinfo->pr_lwp.pr_nice);
13680Sstevel@tonic-gate 			else
13690Sstevel@tonic-gate 				(void) printf(" %2.2s",
13700Sstevel@tonic-gate 				    psinfo->pr_lwp.pr_clname);
13710Sstevel@tonic-gate 		}
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 	if (lflg) {
13740Sstevel@tonic-gate 		if (yflg) {
13750Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* RSS */
13760Sstevel@tonic-gate 				(void) printf("     0");
13770Sstevel@tonic-gate 			else if (psinfo->pr_rssize)
13780Sstevel@tonic-gate 				(void) printf(" %5lu",
1379*8233Sdme@sun.com 				    (ulong_t)psinfo->pr_rssize);
13800Sstevel@tonic-gate 			else
13810Sstevel@tonic-gate 				(void) printf("     ?");
13820Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
13830Sstevel@tonic-gate 				(void) printf("      0");
13840Sstevel@tonic-gate 			else if (psinfo->pr_size)
13850Sstevel@tonic-gate 				(void) printf(" %6lu",
1386*8233Sdme@sun.com 				    (ulong_t)psinfo->pr_size);
13870Sstevel@tonic-gate 			else
13880Sstevel@tonic-gate 				(void) printf("      ?");
13890Sstevel@tonic-gate 		} else {
13900Sstevel@tonic-gate #ifndef _LP64
13910Sstevel@tonic-gate 			if (psinfo->pr_addr)			/* ADDR */
13920Sstevel@tonic-gate 				(void) printf(" %8lx",
1393*8233Sdme@sun.com 				    (ulong_t)psinfo->pr_addr);
13940Sstevel@tonic-gate 			else
13950Sstevel@tonic-gate #endif
13960Sstevel@tonic-gate 				(void) printf("        ?");
13970Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
13980Sstevel@tonic-gate 				(void) printf("      0");
13990Sstevel@tonic-gate 			else if (psinfo->pr_size)
14000Sstevel@tonic-gate 				(void) printf(" %6lu",
14010Sstevel@tonic-gate 				    (ulong_t)psinfo->pr_size / kbytes_per_page);
14020Sstevel@tonic-gate 			else
14030Sstevel@tonic-gate 				(void) printf("      ?");
14040Sstevel@tonic-gate 		}
14050Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_sname != 'S')		/* WCHAN */
14060Sstevel@tonic-gate 			(void) printf("         ");
14070Sstevel@tonic-gate #ifndef _LP64
14080Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_wchan)
14090Sstevel@tonic-gate 			(void) printf(" %8lx",
1410*8233Sdme@sun.com 			    (ulong_t)psinfo->pr_lwp.pr_wchan);
14110Sstevel@tonic-gate #endif
14120Sstevel@tonic-gate 		else
14130Sstevel@tonic-gate 			(void) printf("        ?");
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	if (fflg) {						/* STIME */
14162966Ssayama 		int width = fname[F_STIME].width;
14170Sstevel@tonic-gate 		if (Lflg)
14182966Ssayama 			prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
14190Sstevel@tonic-gate 		else
14202966Ssayama 			prtime(psinfo->pr_start, width + 1, 1);
14210Sstevel@tonic-gate 	}
14222685Sakolb 
14232685Sakolb 	if (Hflg) {
14242685Sakolb 		/* Display home lgroup */
14252685Sakolb 		(void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
14262685Sakolb 	}
14272685Sakolb 
14280Sstevel@tonic-gate 	(void) printf(" %-8.14s", ttyp);			/* TTY */
14290Sstevel@tonic-gate 	if (Lflg) {
14300Sstevel@tonic-gate 		tm = psinfo->pr_lwp.pr_time.tv_sec;
14310Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
14320Sstevel@tonic-gate 			tm++;
14330Sstevel@tonic-gate 	} else {
14340Sstevel@tonic-gate 		tm = psinfo->pr_time.tv_sec;
14350Sstevel@tonic-gate 		if (psinfo->pr_time.tv_nsec > 500000000)
14360Sstevel@tonic-gate 			tm++;
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);		/* [L]TIME */
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if (zombie_lwp) {
14410Sstevel@tonic-gate 		(void) printf(" <defunct>\n");
14420Sstevel@tonic-gate 		return;
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	if (!fflg) {						/* CMD */
14460Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, 8);
14470Sstevel@tonic-gate 		(void) printf(" %.*s\n", wcnt, psinfo->pr_fname);
14480Sstevel@tonic-gate 		return;
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 
14512685Sakolb 
14520Sstevel@tonic-gate 	/*
14530Sstevel@tonic-gate 	 * PRARGSZ == length of cmd arg string.
14540Sstevel@tonic-gate 	 */
14550Sstevel@tonic-gate 	psinfo->pr_psargs[PRARGSZ-1] = '\0';
14560Sstevel@tonic-gate 	bytesleft = PRARGSZ;
14570Sstevel@tonic-gate 	for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
14580Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
14590Sstevel@tonic-gate 		if (length == 0)
14600Sstevel@tonic-gate 			break;
14610Sstevel@tonic-gate 		if (length < 0 || !iswprint(wchar)) {
14620Sstevel@tonic-gate 			if (length < 0)
14630Sstevel@tonic-gate 				length = 1;
14640Sstevel@tonic-gate 			if (bytesleft <= length) {
14650Sstevel@tonic-gate 				*cp = '\0';
14660Sstevel@tonic-gate 				break;
14670Sstevel@tonic-gate 			}
14680Sstevel@tonic-gate 			/* omit the unprintable character */
14690Sstevel@tonic-gate 			(void) memmove(cp, cp+length, bytesleft-length);
14700Sstevel@tonic-gate 			length = 0;
14710Sstevel@tonic-gate 		}
14720Sstevel@tonic-gate 		bytesleft -= length;
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 	wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ);
14750Sstevel@tonic-gate 	(void) printf(" %.*s\n", wcnt, psinfo->pr_psargs);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate /*
14790Sstevel@tonic-gate  * Print percent from 16-bit binary fraction [0 .. 1]
14800Sstevel@tonic-gate  * Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
14810Sstevel@tonic-gate  */
14820Sstevel@tonic-gate static void
14830Sstevel@tonic-gate prtpct(ushort_t pct, int width)
14840Sstevel@tonic-gate {
14850Sstevel@tonic-gate 	uint_t value = pct;	/* need 32 bits to compute with */
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	value = ((value * 1000) + 0x7000) >> 15;	/* [0 .. 1000] */
14880Sstevel@tonic-gate 	if (value >= 1000)
14890Sstevel@tonic-gate 		value = 999;
14900Sstevel@tonic-gate 	if ((width -= 2) < 2)
14910Sstevel@tonic-gate 		width = 2;
14920Sstevel@tonic-gate 	(void) printf("%*u.%u", width, value / 10, value % 10);
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate static void
14960Sstevel@tonic-gate print_time(time_t tim, int width)
14970Sstevel@tonic-gate {
14980Sstevel@tonic-gate 	char buf[30];
14990Sstevel@tonic-gate 	time_t seconds;
15000Sstevel@tonic-gate 	time_t minutes;
15010Sstevel@tonic-gate 	time_t hours;
15020Sstevel@tonic-gate 	time_t days;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	if (tim < 0) {
15050Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
15060Sstevel@tonic-gate 		return;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	seconds = tim % 60;
15100Sstevel@tonic-gate 	tim /= 60;
15110Sstevel@tonic-gate 	minutes = tim % 60;
15120Sstevel@tonic-gate 	tim /= 60;
15130Sstevel@tonic-gate 	hours = tim % 24;
15140Sstevel@tonic-gate 	days = tim / 24;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if (days > 0) {
15170Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
15180Sstevel@tonic-gate 		    days, hours, minutes, seconds);
15190Sstevel@tonic-gate 	} else if (hours > 0) {
15200Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
15210Sstevel@tonic-gate 		    hours, minutes, seconds);
15220Sstevel@tonic-gate 	} else {
15230Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
15240Sstevel@tonic-gate 		    minutes, seconds);
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	(void) printf("%*s", width, buf);
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate static void
15310Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
15320Sstevel@tonic-gate {
15330Sstevel@tonic-gate 	int width = f->width;
15340Sstevel@tonic-gate 	struct passwd *pwd;
15350Sstevel@tonic-gate 	struct group *grp;
15360Sstevel@tonic-gate 	time_t cputime;
15370Sstevel@tonic-gate 	int bytesleft;
15380Sstevel@tonic-gate 	int wcnt;
15390Sstevel@tonic-gate 	wchar_t	wchar;
15400Sstevel@tonic-gate 	char *cp;
15410Sstevel@tonic-gate 	int length;
15420Sstevel@tonic-gate 	ulong_t mask;
15430Sstevel@tonic-gate 	char c, *csave;
15440Sstevel@tonic-gate 	int zombie_lwp;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	switch (f->fname) {
15490Sstevel@tonic-gate 	case F_RUSER:
15500Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_uid)) != NULL)
15510Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
15520Sstevel@tonic-gate 		else
15534321Scasper 			(void) printf("%*u", width, psinfo->pr_uid);
15540Sstevel@tonic-gate 		break;
15550Sstevel@tonic-gate 	case F_USER:
15560Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
15570Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
15580Sstevel@tonic-gate 		else
15594321Scasper 			(void) printf("%*u", width, psinfo->pr_euid);
15600Sstevel@tonic-gate 		break;
15610Sstevel@tonic-gate 	case F_RGROUP:
15620Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
15630Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
15640Sstevel@tonic-gate 		else
15654321Scasper 			(void) printf("%*u", width, psinfo->pr_gid);
15660Sstevel@tonic-gate 		break;
15670Sstevel@tonic-gate 	case F_GROUP:
15680Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
15690Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
15700Sstevel@tonic-gate 		else
15714321Scasper 			(void) printf("%*u", width, psinfo->pr_egid);
15720Sstevel@tonic-gate 		break;
15730Sstevel@tonic-gate 	case F_RUID:
15744321Scasper 		(void) printf("%*u", width, psinfo->pr_uid);
15750Sstevel@tonic-gate 		break;
15760Sstevel@tonic-gate 	case F_UID:
15774321Scasper 		(void) printf("%*u", width, psinfo->pr_euid);
15780Sstevel@tonic-gate 		break;
15790Sstevel@tonic-gate 	case F_RGID:
15804321Scasper 		(void) printf("%*u", width, psinfo->pr_gid);
15810Sstevel@tonic-gate 		break;
15820Sstevel@tonic-gate 	case F_GID:
15834321Scasper 		(void) printf("%*u", width, psinfo->pr_egid);
15840Sstevel@tonic-gate 		break;
15850Sstevel@tonic-gate 	case F_PID:
15860Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pid);
15870Sstevel@tonic-gate 		break;
15880Sstevel@tonic-gate 	case F_PPID:
15890Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_ppid);
15900Sstevel@tonic-gate 		break;
15910Sstevel@tonic-gate 	case F_PGID:
15920Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pgid);
15930Sstevel@tonic-gate 		break;
15940Sstevel@tonic-gate 	case F_SID:
15950Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_sid);
15960Sstevel@tonic-gate 		break;
15970Sstevel@tonic-gate 	case F_PSR:
15980Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
15990Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16000Sstevel@tonic-gate 		else
16010Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
16020Sstevel@tonic-gate 		break;
16030Sstevel@tonic-gate 	case F_LWP:
16040Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
16050Sstevel@tonic-gate 		break;
16060Sstevel@tonic-gate 	case F_NLWP:
16070Sstevel@tonic-gate 		(void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
16080Sstevel@tonic-gate 		break;
16090Sstevel@tonic-gate 	case F_OPRI:
16100Sstevel@tonic-gate 		if (zombie_lwp)
16110Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16120Sstevel@tonic-gate 		else
16130Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
16140Sstevel@tonic-gate 		break;
16150Sstevel@tonic-gate 	case F_PRI:
16160Sstevel@tonic-gate 		if (zombie_lwp)
16170Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16180Sstevel@tonic-gate 		else
16190Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
16200Sstevel@tonic-gate 		break;
16210Sstevel@tonic-gate 	case F_F:
16220Sstevel@tonic-gate 		mask = 0xffffffffUL;
16230Sstevel@tonic-gate 		if (width < 8)
16240Sstevel@tonic-gate 			mask >>= (8 - width) * 4;
16250Sstevel@tonic-gate 		(void) printf("%*lx", width, psinfo->pr_flag & mask);
16260Sstevel@tonic-gate 		break;
16270Sstevel@tonic-gate 	case F_S:
16280Sstevel@tonic-gate 		(void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
16290Sstevel@tonic-gate 		break;
16300Sstevel@tonic-gate 	case F_C:
16310Sstevel@tonic-gate 		if (zombie_lwp)
16320Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16330Sstevel@tonic-gate 		else
16340Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
16350Sstevel@tonic-gate 		break;
16360Sstevel@tonic-gate 	case F_PCPU:
16370Sstevel@tonic-gate 		if (zombie_lwp)
16380Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16390Sstevel@tonic-gate 		else if (Lflg)
16400Sstevel@tonic-gate 			prtpct(psinfo->pr_lwp.pr_pctcpu, width);
16410Sstevel@tonic-gate 		else
16420Sstevel@tonic-gate 			prtpct(psinfo->pr_pctcpu, width);
16430Sstevel@tonic-gate 		break;
16440Sstevel@tonic-gate 	case F_PMEM:
16450Sstevel@tonic-gate 		prtpct(psinfo->pr_pctmem, width);
16460Sstevel@tonic-gate 		break;
16470Sstevel@tonic-gate 	case F_OSZ:
16480Sstevel@tonic-gate 		(void) printf("%*lu", width,
1649*8233Sdme@sun.com 		    (ulong_t)psinfo->pr_size / kbytes_per_page);
16500Sstevel@tonic-gate 		break;
16510Sstevel@tonic-gate 	case F_VSZ:
16520Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
16530Sstevel@tonic-gate 		break;
16540Sstevel@tonic-gate 	case F_RSS:
16550Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
16560Sstevel@tonic-gate 		break;
16570Sstevel@tonic-gate 	case F_NICE:
16580Sstevel@tonic-gate 		/* if pr_oldpri is zero, then this class has no nice */
16590Sstevel@tonic-gate 		if (zombie_lwp)
16600Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16610Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_oldpri != 0)
16620Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
16630Sstevel@tonic-gate 		else
16640Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
1665*8233Sdme@sun.com 			    psinfo->pr_lwp.pr_clname);
16660Sstevel@tonic-gate 		break;
16670Sstevel@tonic-gate 	case F_CLASS:
16680Sstevel@tonic-gate 		if (zombie_lwp)
16690Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16700Sstevel@tonic-gate 		else
16710Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
1672*8233Sdme@sun.com 			    psinfo->pr_lwp.pr_clname);
16730Sstevel@tonic-gate 		break;
16740Sstevel@tonic-gate 	case F_STIME:
16750Sstevel@tonic-gate 		if (Lflg)
16760Sstevel@tonic-gate 			prtime(psinfo->pr_lwp.pr_start, width, 0);
16770Sstevel@tonic-gate 		else
16780Sstevel@tonic-gate 			prtime(psinfo->pr_start, width, 0);
16790Sstevel@tonic-gate 		break;
16800Sstevel@tonic-gate 	case F_ETIME:
16810Sstevel@tonic-gate 		if (Lflg)
16820Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_lwp.pr_start),
1683*8233Sdme@sun.com 			    width);
16840Sstevel@tonic-gate 		else
16850Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_start), width);
16860Sstevel@tonic-gate 		break;
16870Sstevel@tonic-gate 	case F_TIME:
16880Sstevel@tonic-gate 		if (Lflg) {
16890Sstevel@tonic-gate 			cputime = psinfo->pr_lwp.pr_time.tv_sec;
16900Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
16910Sstevel@tonic-gate 				cputime++;
16920Sstevel@tonic-gate 		} else {
16930Sstevel@tonic-gate 			cputime = psinfo->pr_time.tv_sec;
16940Sstevel@tonic-gate 			if (psinfo->pr_time.tv_nsec > 500000000)
16950Sstevel@tonic-gate 				cputime++;
16960Sstevel@tonic-gate 		}
16970Sstevel@tonic-gate 		print_time(cputime, width);
16980Sstevel@tonic-gate 		break;
16990Sstevel@tonic-gate 	case F_TTY:
17000Sstevel@tonic-gate 		(void) printf("%-*s", width, ttyp);
17010Sstevel@tonic-gate 		break;
17020Sstevel@tonic-gate 	case F_ADDR:
17030Sstevel@tonic-gate 		if (zombie_lwp)
17040Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
17050Sstevel@tonic-gate 		else if (Lflg)
17060Sstevel@tonic-gate 			(void) printf("%*lx", width,
1707*8233Sdme@sun.com 			    (long)psinfo->pr_lwp.pr_addr);
17080Sstevel@tonic-gate 		else
17090Sstevel@tonic-gate 			(void) printf("%*lx", width, (long)psinfo->pr_addr);
17100Sstevel@tonic-gate 		break;
17110Sstevel@tonic-gate 	case F_WCHAN:
17120Sstevel@tonic-gate 		if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
17130Sstevel@tonic-gate 			(void) printf("%*lx", width,
1714*8233Sdme@sun.com 			    (long)psinfo->pr_lwp.pr_wchan);
17150Sstevel@tonic-gate 		else
17160Sstevel@tonic-gate 			(void) printf("%*.*s", width, width, "-");
17170Sstevel@tonic-gate 		break;
17180Sstevel@tonic-gate 	case F_FNAME:
17190Sstevel@tonic-gate 		/*
17200Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
17210Sstevel@tonic-gate 		 */
17220Sstevel@tonic-gate 		if (zombie_lwp) {
17230Sstevel@tonic-gate 			if (f->next != NULL)
17240Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17250Sstevel@tonic-gate 			else
17260Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17270Sstevel@tonic-gate 			break;
17280Sstevel@tonic-gate 		}
17290Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, width);
17300Sstevel@tonic-gate 		if (f->next != NULL)
17310Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
17320Sstevel@tonic-gate 		else
17330Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, psinfo->pr_fname);
17340Sstevel@tonic-gate 		break;
17350Sstevel@tonic-gate 	case F_COMM:
17360Sstevel@tonic-gate 		if (zombie_lwp) {
17370Sstevel@tonic-gate 			if (f->next != NULL)
17380Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17390Sstevel@tonic-gate 			else
17400Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17410Sstevel@tonic-gate 			break;
17420Sstevel@tonic-gate 		}
17430Sstevel@tonic-gate 		csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n");
17440Sstevel@tonic-gate 		if (csave) {
17450Sstevel@tonic-gate 			c = *csave;
17460Sstevel@tonic-gate 			*csave = '\0';
17470Sstevel@tonic-gate 		}
17480Sstevel@tonic-gate 		/* FALLTHROUGH */
17490Sstevel@tonic-gate 	case F_ARGS:
17500Sstevel@tonic-gate 		/*
17510Sstevel@tonic-gate 		 * PRARGSZ == length of cmd arg string.
17520Sstevel@tonic-gate 		 */
17530Sstevel@tonic-gate 		if (zombie_lwp) {
17540Sstevel@tonic-gate 			(void) printf("%-*s", width, "<defunct>");
17550Sstevel@tonic-gate 			break;
17560Sstevel@tonic-gate 		}
17570Sstevel@tonic-gate 		psinfo->pr_psargs[PRARGSZ-1] = '\0';
17580Sstevel@tonic-gate 		bytesleft = PRARGSZ;
17590Sstevel@tonic-gate 		for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
17600Sstevel@tonic-gate 			length = mbtowc(&wchar, cp, MB_LEN_MAX);
17610Sstevel@tonic-gate 			if (length == 0)
17620Sstevel@tonic-gate 				break;
17630Sstevel@tonic-gate 			if (length < 0 || !iswprint(wchar)) {
17640Sstevel@tonic-gate 				if (length < 0)
17650Sstevel@tonic-gate 					length = 1;
17660Sstevel@tonic-gate 				if (bytesleft <= length) {
17670Sstevel@tonic-gate 					*cp = '\0';
17680Sstevel@tonic-gate 					break;
17690Sstevel@tonic-gate 				}
17700Sstevel@tonic-gate 				/* omit the unprintable character */
17710Sstevel@tonic-gate 				(void) memmove(cp, cp+length, bytesleft-length);
17720Sstevel@tonic-gate 				length = 0;
17730Sstevel@tonic-gate 			}
17740Sstevel@tonic-gate 			bytesleft -= length;
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width);
17770Sstevel@tonic-gate 		/*
17780Sstevel@tonic-gate 		 * Print full width unless this is the last format.
17790Sstevel@tonic-gate 		 */
17800Sstevel@tonic-gate 		if (f->next != NULL)
17810Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt,
17820Sstevel@tonic-gate 			    psinfo->pr_psargs);
17830Sstevel@tonic-gate 		else
17840Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt,
17850Sstevel@tonic-gate 			    psinfo->pr_psargs);
17860Sstevel@tonic-gate 		if (f->fname == F_COMM && csave)
17870Sstevel@tonic-gate 			*csave = c;
17880Sstevel@tonic-gate 		break;
17890Sstevel@tonic-gate 	case F_TASKID:
17900Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_taskid);
17910Sstevel@tonic-gate 		break;
17920Sstevel@tonic-gate 	case F_PROJID:
17930Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_projid);
17940Sstevel@tonic-gate 		break;
17950Sstevel@tonic-gate 	case F_PROJECT:
17960Sstevel@tonic-gate 		{
17970Sstevel@tonic-gate 			struct project cproj;
17980Sstevel@tonic-gate 			char proj_buf[PROJECT_BUFSZ];
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 			if ((getprojbyid(psinfo->pr_projid, &cproj,
18010Sstevel@tonic-gate 			    (void *)&proj_buf, PROJECT_BUFSZ)) == NULL)
18020Sstevel@tonic-gate 				(void) printf("%*d", width,
18030Sstevel@tonic-gate 				    (int)psinfo->pr_projid);
18040Sstevel@tonic-gate 			else
18050Sstevel@tonic-gate 				(void) printf("%*s", width,
18060Sstevel@tonic-gate 				    (cproj.pj_name != NULL) ?
18070Sstevel@tonic-gate 				    cproj.pj_name : "---");
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		break;
18100Sstevel@tonic-gate 	case F_PSET:
18110Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
18120Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18130Sstevel@tonic-gate 		else
18140Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
18150Sstevel@tonic-gate 		break;
18160Sstevel@tonic-gate 	case F_ZONEID:
18170Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_zoneid);
18180Sstevel@tonic-gate 		break;
18190Sstevel@tonic-gate 	case F_ZONE:
18200Sstevel@tonic-gate 		{
18210Sstevel@tonic-gate 			char zonename[ZONENAME_MAX];
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 			if (getzonenamebyid(psinfo->pr_zoneid, zonename,
18240Sstevel@tonic-gate 			    sizeof (zonename)) < 0) {
18250Sstevel@tonic-gate 				(void) printf("%*d", width,
18260Sstevel@tonic-gate 				    ((int)psinfo->pr_zoneid));
18270Sstevel@tonic-gate 			} else {
18280Sstevel@tonic-gate 				(void) printf("%*s", width, zonename);
18290Sstevel@tonic-gate 			}
18300Sstevel@tonic-gate 		}
18310Sstevel@tonic-gate 		break;
18320Sstevel@tonic-gate 	case F_CTID:
18330Sstevel@tonic-gate 		if (psinfo->pr_contract == -1)
18340Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18350Sstevel@tonic-gate 		else
18360Sstevel@tonic-gate 			(void) printf("%*ld", width, (long)psinfo->pr_contract);
18370Sstevel@tonic-gate 		break;
18382685Sakolb 	case F_LGRP:
18392685Sakolb 		/* Display home lgroup */
18402685Sakolb 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
18412685Sakolb 		break;
18420Sstevel@tonic-gate 	}
18430Sstevel@tonic-gate }
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate static void
18460Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	int wcnt;
18490Sstevel@tonic-gate 	int width = f->width;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	switch (f->fname) {
18520Sstevel@tonic-gate 	case F_FNAME:
18530Sstevel@tonic-gate 	case F_COMM:
18540Sstevel@tonic-gate 	case F_ARGS:
18550Sstevel@tonic-gate 		/*
18560Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
18570Sstevel@tonic-gate 		 */
18580Sstevel@tonic-gate 		wcnt = min(width, sizeof ("<defunct>"));
18590Sstevel@tonic-gate 		if (f->next != NULL)
18600Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, "<defunct>");
18610Sstevel@tonic-gate 		else
18620Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, "<defunct>");
18630Sstevel@tonic-gate 		break;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	case F_PSR:
18660Sstevel@tonic-gate 	case F_PCPU:
18670Sstevel@tonic-gate 	case F_PMEM:
18680Sstevel@tonic-gate 	case F_NICE:
18690Sstevel@tonic-gate 	case F_CLASS:
18700Sstevel@tonic-gate 	case F_STIME:
18710Sstevel@tonic-gate 	case F_ETIME:
18720Sstevel@tonic-gate 	case F_WCHAN:
18730Sstevel@tonic-gate 	case F_PSET:
18740Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
18750Sstevel@tonic-gate 		break;
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	case F_OPRI:
18780Sstevel@tonic-gate 	case F_PRI:
18790Sstevel@tonic-gate 	case F_OSZ:
18800Sstevel@tonic-gate 	case F_VSZ:
18810Sstevel@tonic-gate 	case F_RSS:
18820Sstevel@tonic-gate 		(void) printf("%*d", width, 0);
18830Sstevel@tonic-gate 		break;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	default:
18860Sstevel@tonic-gate 		print_field(psinfo, f, ttyp);
18870Sstevel@tonic-gate 		break;
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate static void
18920Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp,
18930Sstevel@tonic-gate 	void (*print_fld)(psinfo_t *, struct field *, const char *))
18940Sstevel@tonic-gate {
18950Sstevel@tonic-gate 	struct field *f;
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	for (f = fields; f != NULL; f = f->next) {
18980Sstevel@tonic-gate 		print_fld(psinfo, f, ttyp);
18990Sstevel@tonic-gate 		if (f->next != NULL)
19000Sstevel@tonic-gate 			(void) printf(" ");
19010Sstevel@tonic-gate 	}
19020Sstevel@tonic-gate 	(void) printf("\n");
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate  * Returns 1 if arg is found in array arr, of length num; 0 otherwise.
19070Sstevel@tonic-gate  */
19080Sstevel@tonic-gate static int
19090Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg)
19100Sstevel@tonic-gate {
19110Sstevel@tonic-gate 	int i;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	for (i = 0; i < number; i++)
19140Sstevel@tonic-gate 		if (arg == arr[i])
19150Sstevel@tonic-gate 			return (1);
19160Sstevel@tonic-gate 	return (0);
19170Sstevel@tonic-gate }
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate /*
19200Sstevel@tonic-gate  * Add an entry (user, group) to the specified table.
19210Sstevel@tonic-gate  */
19220Sstevel@tonic-gate static void
19230Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	struct ugdata *entp;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 	if (tbl->size == tbl->nent) {	/* reallocate the table entries */
19280Sstevel@tonic-gate 		if ((tbl->size *= 2) == 0)
19290Sstevel@tonic-gate 			tbl->size = 32;		/* first time */
19300Sstevel@tonic-gate 		tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 	entp = &tbl->ent[tbl->nent++];
19330Sstevel@tonic-gate 	entp->id = 0;
19340Sstevel@tonic-gate 	(void) strncpy(entp->name, name, MAXUGNAME);
19350Sstevel@tonic-gate 	entp->name[MAXUGNAME] = '\0';
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate static int
19390Sstevel@tonic-gate uconv(struct ughead *uhead)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	struct ugdata *utbl = uhead->ent;
19420Sstevel@tonic-gate 	int n = uhead->nent;
19430Sstevel@tonic-gate 	struct passwd *pwd;
19440Sstevel@tonic-gate 	int i;
19450Sstevel@tonic-gate 	int fnd = 0;
19460Sstevel@tonic-gate 	uid_t uid;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	/*
19490Sstevel@tonic-gate 	 * Ask the name service for names.
19500Sstevel@tonic-gate 	 */
19510Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19520Sstevel@tonic-gate 		/*
19530Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
19540Sstevel@tonic-gate 		 */
19554321Scasper 		if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
19560Sstevel@tonic-gate 			pwd = getpwuid(uid);
19570Sstevel@tonic-gate 		else
19580Sstevel@tonic-gate 			pwd = getpwnam(utbl[i].name);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		/*
19610Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
19620Sstevel@tonic-gate 		 */
19630Sstevel@tonic-gate 		if (pwd == NULL) {
19640Sstevel@tonic-gate 			(void) fprintf(stderr,
19650Sstevel@tonic-gate 			    gettext("ps: unknown user %s\n"), utbl[i].name);
19660Sstevel@tonic-gate 			continue;
19670Sstevel@tonic-gate 		}
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 		utbl[fnd].id = pwd->pw_uid;
19700Sstevel@tonic-gate 		(void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
19710Sstevel@tonic-gate 		fnd++;
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	uhead->nent = fnd;	/* in case it changed */
19750Sstevel@tonic-gate 	return (n - fnd);
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate static int
19790Sstevel@tonic-gate gconv(struct ughead *ghead)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate 	struct ugdata *gtbl = ghead->ent;
19820Sstevel@tonic-gate 	int n = ghead->nent;
19830Sstevel@tonic-gate 	struct group *grp;
19840Sstevel@tonic-gate 	gid_t gid;
19850Sstevel@tonic-gate 	int i;
19860Sstevel@tonic-gate 	int fnd = 0;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	/*
19890Sstevel@tonic-gate 	 * Ask the name service for names.
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19920Sstevel@tonic-gate 		/*
19930Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
19940Sstevel@tonic-gate 		 */
19954321Scasper 		if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
19960Sstevel@tonic-gate 			grp = getgrgid(gid);
19970Sstevel@tonic-gate 		else
19980Sstevel@tonic-gate 			grp = getgrnam(gtbl[i].name);
19990Sstevel@tonic-gate 		/*
20000Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
20010Sstevel@tonic-gate 		 */
20020Sstevel@tonic-gate 		if (grp == NULL) {
20030Sstevel@tonic-gate 			(void) fprintf(stderr,
20040Sstevel@tonic-gate 			    gettext("ps: unknown group %s\n"), gtbl[i].name);
20050Sstevel@tonic-gate 			continue;
20060Sstevel@tonic-gate 		}
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 		gtbl[fnd].id = grp->gr_gid;
20090Sstevel@tonic-gate 		(void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
20100Sstevel@tonic-gate 		fnd++;
20110Sstevel@tonic-gate 	}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	ghead->nent = fnd;	/* in case it changed */
20140Sstevel@tonic-gate 	return (n - fnd);
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate /*
20180Sstevel@tonic-gate  * Return 1 if puid is in table, otherwise 0.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate static int
20210Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	struct ugdata *utbl = ughead->ent;
20240Sstevel@tonic-gate 	int n = ughead->nent;
20250Sstevel@tonic-gate 	int i;
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	for (i = 0; i < n; i++)
20280Sstevel@tonic-gate 		if (utbl[i].id == id)
20290Sstevel@tonic-gate 			return (1);
20300Sstevel@tonic-gate 	return (0);
20310Sstevel@tonic-gate }
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate /*
20340Sstevel@tonic-gate  * Print starting time of process unless process started more than 24 hours
20350Sstevel@tonic-gate  * ago, in which case the date is printed.  The date is printed in the form
20360Sstevel@tonic-gate  * "MMM dd" if old format, else the blank is replaced with an '_' so
20370Sstevel@tonic-gate  * it appears as a single word (for parseability).
20380Sstevel@tonic-gate  */
20390Sstevel@tonic-gate static void
20400Sstevel@tonic-gate prtime(timestruc_t st, int width, int old)
20410Sstevel@tonic-gate {
20420Sstevel@tonic-gate 	char sttim[26];
20430Sstevel@tonic-gate 	time_t starttime;
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	starttime = st.tv_sec;
20460Sstevel@tonic-gate 	if (st.tv_nsec > 500000000)
20470Sstevel@tonic-gate 		starttime++;
20480Sstevel@tonic-gate 	if ((now.tv_sec - starttime) >= 24*60*60) {
20492966Ssayama 		(void) strftime(sttim, sizeof (sttim), old?
20502966Ssayama 		/*
20512966Ssayama 		 * TRANSLATION_NOTE
20522966Ssayama 		 * This time format is used by STIME field when -f option
20532966Ssayama 		 * is specified.  Used for processes that begun more than
20542966Ssayama 		 * 24 hours.
20552966Ssayama 		 */
20562966Ssayama 		    dcgettext(NULL, "%b %d", LC_TIME) :
20572966Ssayama 		/*
20582966Ssayama 		 * TRANSLATION_NOTE
20592966Ssayama 		 * This time format is used by STIME field when -o option
20602966Ssayama 		 * is specified.  Used for processes that begun more than
20612966Ssayama 		 * 24 hours.
20622966Ssayama 		 */
20632966Ssayama 		    dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
20640Sstevel@tonic-gate 	} else {
20652966Ssayama 		/*
20662966Ssayama 		 * TRANSLATION_NOTE
20672966Ssayama 		 * This time format is used by STIME field when -f or -o option
20682966Ssayama 		 * is specified.  Used for processes that begun less than
20692966Ssayama 		 * 24 hours.
20702966Ssayama 		 */
20712966Ssayama 		(void) strftime(sttim, sizeof (sttim),
20722966Ssayama 		    dcgettext(NULL, "%H:%M:%S", LC_TIME),
20732966Ssayama 		    localtime(&starttime));
20740Sstevel@tonic-gate 	}
20750Sstevel@tonic-gate 	(void) printf("%*.*s", width, width, sttim);
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate static void
20790Sstevel@tonic-gate przom(psinfo_t *psinfo)
20800Sstevel@tonic-gate {
20810Sstevel@tonic-gate 	long	tm;
20820Sstevel@tonic-gate 	struct passwd *pwd;
20830Sstevel@tonic-gate 	char zonename[ZONENAME_MAX];
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	/*
20860Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
20870Sstevel@tonic-gate 	 * spearator, rather than keeping track of which column is first.  All
20880Sstevel@tonic-gate 	 * other fields are printed with a leading space.
20890Sstevel@tonic-gate 	 */
20900Sstevel@tonic-gate 	if (lflg) {	/* F S */
20910Sstevel@tonic-gate 		if (!yflg)
20920Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
20930Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
20940Sstevel@tonic-gate 	}
20950Sstevel@tonic-gate 	if (Zflg) {
20960Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
20970Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
20980Sstevel@tonic-gate 			(void) printf("%7.7d ", ((int)psinfo->pr_zoneid));
20990Sstevel@tonic-gate 		} else {
21000Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
21010Sstevel@tonic-gate 		}
21020Sstevel@tonic-gate 	}
21032685Sakolb 	if (Hflg) {
21042685Sakolb 		/* Display home lgroup */
21052685Sakolb 		(void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
21062685Sakolb 	}
21070Sstevel@tonic-gate 	if (fflg) {
21080Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
21090Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
21100Sstevel@tonic-gate 		else
2111*8233Sdme@sun.com 			(void) printf(" %7.7u ", psinfo->pr_euid);
21120Sstevel@tonic-gate 	} else if (lflg)
21134321Scasper 		(void) printf("%6u ", psinfo->pr_euid);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid);	/* PID */
21160Sstevel@tonic-gate 	if (lflg || fflg)
21170Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21180Sstevel@tonic-gate 		    (int)psinfo->pr_ppid);			/* PPID */
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	if (jflg) {
21210Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21220Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);			/* PGID */
21230Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21240Sstevel@tonic-gate 		    (int)psinfo->pr_sid);			/* SID  */
21250Sstevel@tonic-gate 	}
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	if (Lflg)
21280Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* LWP */
21290Sstevel@tonic-gate 	if (Pflg)
21300Sstevel@tonic-gate 		(void) printf("   -");				/* PSR */
21310Sstevel@tonic-gate 	if (Lflg && fflg)
21320Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* NLWP */
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	if (cflg) {
21350Sstevel@tonic-gate 		(void) printf(" %4s", "-");	/* zombies have no class */
21360Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI	*/
21370Sstevel@tonic-gate 	} else if (lflg || fflg) {
21380Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
21390Sstevel@tonic-gate 		if (lflg)
21400Sstevel@tonic-gate 			(void) printf(" %3d %2s",
21410Sstevel@tonic-gate 			    psinfo->pr_lwp.pr_oldpri, "-");	/* PRI NI */
21420Sstevel@tonic-gate 	}
21430Sstevel@tonic-gate 	if (lflg) {
21440Sstevel@tonic-gate 		if (yflg)				/* RSS SZ WCHAN */
21450Sstevel@tonic-gate 			(void) printf(" %5d %6d %8s", 0, 0, "-");
21460Sstevel@tonic-gate 		else					/* ADDR SZ WCHAN */
21470Sstevel@tonic-gate 			(void) printf(" %8s %6d %8s", "-", 0, "-");
21480Sstevel@tonic-gate 	}
21492966Ssayama 	if (fflg) {
21502966Ssayama 		int width = fname[F_STIME].width;
21512966Ssayama 		(void) printf(" %*.*s", width, width, "-"); 	/* STIME */
21522966Ssayama 	}
21530Sstevel@tonic-gate 	(void) printf(" %-8.14s", "?");				/* TTY */
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	tm = psinfo->pr_time.tv_sec;
21560Sstevel@tonic-gate 	if (psinfo->pr_time.tv_nsec > 500000000)
21570Sstevel@tonic-gate 		tm++;
21580Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);	/* TIME */
21590Sstevel@tonic-gate 	(void) printf(" <defunct>\n");
21600Sstevel@tonic-gate }
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate /*
21630Sstevel@tonic-gate  * Function to compute the number of printable bytes in a multibyte
21640Sstevel@tonic-gate  * command string ("internationalization").
21650Sstevel@tonic-gate  */
21660Sstevel@tonic-gate static int
21670Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize)
21680Sstevel@tonic-gate {
21690Sstevel@tonic-gate 	int csiwcnt = 0, scrwcnt = 0;
21700Sstevel@tonic-gate 	int ncsisz, nscrsz;
21710Sstevel@tonic-gate 	wchar_t  wchar;
21720Sstevel@tonic-gate 	int	 len;
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	while (*cmd != '\0') {
21750Sstevel@tonic-gate 		if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
21760Sstevel@tonic-gate 			len = MB_CUR_MAX;
21770Sstevel@tonic-gate 		if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
21780Sstevel@tonic-gate 			return (8); /* default to use for illegal chars */
21790Sstevel@tonic-gate 		if ((nscrsz = wcwidth(wchar)) <= 0)
21800Sstevel@tonic-gate 			return (8);
21810Sstevel@tonic-gate 		if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize)
21820Sstevel@tonic-gate 			break;
21830Sstevel@tonic-gate 		csiwcnt += ncsisz;
21840Sstevel@tonic-gate 		scrwcnt += nscrsz;
21850Sstevel@tonic-gate 		cmd += ncsisz;
21860Sstevel@tonic-gate 	}
21870Sstevel@tonic-gate 	return (csiwcnt);
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate static char *
21910Sstevel@tonic-gate err_string(int err)
21920Sstevel@tonic-gate {
21930Sstevel@tonic-gate 	static char buf[32];
21940Sstevel@tonic-gate 	char *str = strerror(err);
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	if (str == NULL)
21970Sstevel@tonic-gate 		(void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 	return (str);
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate /* If allocation fails, die */
22030Sstevel@tonic-gate static void *
22040Sstevel@tonic-gate Realloc(void *ptr, size_t size)
22050Sstevel@tonic-gate {
22060Sstevel@tonic-gate 	ptr = realloc(ptr, size);
22070Sstevel@tonic-gate 	if (ptr == NULL) {
22080Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ps: no memory\n"));
22090Sstevel@tonic-gate 		exit(1);
22100Sstevel@tonic-gate 	}
22110Sstevel@tonic-gate 	return (ptr);
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate static time_t
22150Sstevel@tonic-gate delta_secs(const timestruc_t *start)
22160Sstevel@tonic-gate {
22170Sstevel@tonic-gate 	time_t seconds = now.tv_sec - start->tv_sec;
22180Sstevel@tonic-gate 	long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	if (nanosecs >= (NANOSEC / 2))
22210Sstevel@tonic-gate 		seconds++;
22220Sstevel@tonic-gate 	else if (nanosecs < -(NANOSEC / 2))
22230Sstevel@tonic-gate 		seconds--;
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	return (seconds);
22260Sstevel@tonic-gate }
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate /*
22290Sstevel@tonic-gate  * Returns the following:
22300Sstevel@tonic-gate  *
22310Sstevel@tonic-gate  * 	0	No error
22320Sstevel@tonic-gate  * 	EINVAL	Invalid number
22330Sstevel@tonic-gate  * 	ERANGE	Value exceeds (min, max) range
22340Sstevel@tonic-gate  */
22350Sstevel@tonic-gate static int
22360Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max)
22370Sstevel@tonic-gate {
22380Sstevel@tonic-gate 	char *q;
22390Sstevel@tonic-gate 	long number;
22400Sstevel@tonic-gate 	int error;
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	errno = 0;
22430Sstevel@tonic-gate 	number = strtol(p, &q, 10);
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	if (errno != 0 || q == p || *q != '\0') {
22460Sstevel@tonic-gate 		if ((error = errno) == 0) {
22470Sstevel@tonic-gate 			/*
22480Sstevel@tonic-gate 			 * strtol() can fail without setting errno, or it can
22490Sstevel@tonic-gate 			 * set it to EINVAL or ERANGE.  In the case errno is
22500Sstevel@tonic-gate 			 * still zero, return EINVAL.
22510Sstevel@tonic-gate 			 */
22520Sstevel@tonic-gate 			error = EINVAL;
22530Sstevel@tonic-gate 		}
22540Sstevel@tonic-gate 	} else if (number < min || number > max) {
22550Sstevel@tonic-gate 		error = ERANGE;
22560Sstevel@tonic-gate 	} else {
22570Sstevel@tonic-gate 		error = 0;
22580Sstevel@tonic-gate 	}
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	*val = number;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	return (error);
22630Sstevel@tonic-gate }
22640Sstevel@tonic-gate 
22654321Scasper /*
22664321Scasper  * Returns the following:
22674321Scasper  *
22684321Scasper  * 	0	No error
22694321Scasper  * 	EINVAL	Invalid number
22704321Scasper  * 	ERANGE	Value exceeds (min, max) range
22714321Scasper  */
22724321Scasper static int
22734321Scasper str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
22744321Scasper {
22754321Scasper 	char *q;
22764321Scasper 	unsigned long number;
22774321Scasper 	int error;
22784321Scasper 
22794321Scasper 	errno = 0;
22804321Scasper 	number = strtoul(p, &q, 10);
22814321Scasper 
22824321Scasper 	if (errno != 0 || q == p || *q != '\0') {
22834321Scasper 		if ((error = errno) == 0) {
22844321Scasper 			/*
22854321Scasper 			 * strtoul() can fail without setting errno, or it can
22864321Scasper 			 * set it to EINVAL or ERANGE.  In the case errno is
22874321Scasper 			 * still zero, return EINVAL.
22884321Scasper 			 */
22894321Scasper 			error = EINVAL;
22904321Scasper 		}
22914321Scasper 	} else if (number < min || number > max) {
22924321Scasper 		error = ERANGE;
22934321Scasper 	} else {
22944321Scasper 		error = 0;
22954321Scasper 	}
22964321Scasper 
22974321Scasper 	*val = number;
22984321Scasper 
22994321Scasper 	return (error);
23004321Scasper }
23014321Scasper 
23020Sstevel@tonic-gate static int
23030Sstevel@tonic-gate pidcmp(const void *p1, const void *p2)
23040Sstevel@tonic-gate {
23050Sstevel@tonic-gate 	pid_t i = *((pid_t *)p1);
23060Sstevel@tonic-gate 	pid_t j = *((pid_t *)p2);
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	return (i - j);
23090Sstevel@tonic-gate }
2310