xref: /onnv-gate/usr/src/cmd/ps/ps.c (revision 8822:486d6b3deddc)
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*8822SCasper.Dik@Sun.COM  * Copyright 2009 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 
295*8822SCasper.Dik@Sun.COM #define	UCB_OPTS	"-aceglnrtuvwxSU"
296*8822SCasper.Dik@Sun.COM 
2970Sstevel@tonic-gate static	void	usage(void);
2980Sstevel@tonic-gate static	char	*getarg(char **);
2990Sstevel@tonic-gate static	char	*parse_format(char *);
3000Sstevel@tonic-gate static	char	*gettty(psinfo_t *);
3010Sstevel@tonic-gate static	int	prfind(int, psinfo_t *, char **);
3020Sstevel@tonic-gate static	void	prcom(psinfo_t *, char *);
3030Sstevel@tonic-gate static	void	prtpct(ushort_t, int);
3040Sstevel@tonic-gate static	void	print_time(time_t, int);
3050Sstevel@tonic-gate static	void	print_field(psinfo_t *, struct field *, const char *);
3060Sstevel@tonic-gate static	void	print_zombie_field(psinfo_t *, struct field *, const char *);
3070Sstevel@tonic-gate static	void	pr_fields(psinfo_t *, const char *,
3080Sstevel@tonic-gate 		void (*print_fld)(psinfo_t *, struct field *, const char *));
3090Sstevel@tonic-gate static	int	search(pid_t *, int, pid_t);
3100Sstevel@tonic-gate static	void	add_ugentry(struct ughead *, char *);
3110Sstevel@tonic-gate static	int	uconv(struct ughead *);
3120Sstevel@tonic-gate static	int	gconv(struct ughead *);
3134321Scasper static	int	ugfind(id_t, struct ughead *);
3140Sstevel@tonic-gate static	void	prtime(timestruc_t, int, int);
3150Sstevel@tonic-gate static	void	przom(psinfo_t *);
3160Sstevel@tonic-gate static	int	namencnt(char *, int, int);
3170Sstevel@tonic-gate static	char	*err_string(int);
3180Sstevel@tonic-gate static	int	print_proc(char *pname);
3190Sstevel@tonic-gate static	time_t	delta_secs(const timestruc_t *);
3200Sstevel@tonic-gate static	int	str2id(const char *, pid_t *, long, long);
3214321Scasper static	int	str2uid(const char *,  uid_t *, unsigned long, unsigned long);
3220Sstevel@tonic-gate static	void	*Realloc(void *, size_t);
3230Sstevel@tonic-gate static	int	pidcmp(const void *p1, const void *p2);
3240Sstevel@tonic-gate 
325*8822SCasper.Dik@Sun.COM extern	int	ucbmain(int, char **);
326*8822SCasper.Dik@Sun.COM static	int	stdmain(int, char **);
327*8822SCasper.Dik@Sun.COM 
3280Sstevel@tonic-gate int
main(int argc,char ** argv)3290Sstevel@tonic-gate main(int argc, char **argv)
3300Sstevel@tonic-gate {
331*8822SCasper.Dik@Sun.COM 	const char *me;
332*8822SCasper.Dik@Sun.COM 
333*8822SCasper.Dik@Sun.COM 	/*
334*8822SCasper.Dik@Sun.COM 	 * The original two ps'es are linked in a single binary;
335*8822SCasper.Dik@Sun.COM 	 * their main()s are renamed to stdmain for /usr/bin/ps and
336*8822SCasper.Dik@Sun.COM 	 * ucbmain for /usr/ucb/ps.
337*8822SCasper.Dik@Sun.COM 	 * We try to figure out which instance of ps the user wants to run.
338*8822SCasper.Dik@Sun.COM 	 * Traditionally, the UCB variant doesn't require the flag argument
339*8822SCasper.Dik@Sun.COM 	 * start with a "-".  If the first argument doesn't start with a
340*8822SCasper.Dik@Sun.COM 	 * "-", we call "ucbmain".
341*8822SCasper.Dik@Sun.COM 	 * If there's a first argument and it starts with a "-", we check
342*8822SCasper.Dik@Sun.COM 	 * whether any of the options isn't acceptable to "ucbmain"; in that
343*8822SCasper.Dik@Sun.COM 	 * case we run "stdmain".
344*8822SCasper.Dik@Sun.COM 	 * If we can't tell from the options which main to call, we check
345*8822SCasper.Dik@Sun.COM 	 * the binary we are running.  We default to "stdmain" but
346*8822SCasper.Dik@Sun.COM 	 * any mention in the executable name of "ucb" causes us to call
347*8822SCasper.Dik@Sun.COM 	 * ucbmain.
348*8822SCasper.Dik@Sun.COM 	 */
349*8822SCasper.Dik@Sun.COM 	if (argv[1] != NULL) {
350*8822SCasper.Dik@Sun.COM 		if (argv[1][0] != '-')
351*8822SCasper.Dik@Sun.COM 			return (ucbmain(argc, argv));
352*8822SCasper.Dik@Sun.COM 		else if (argv[1][strspn(argv[1], UCB_OPTS)] != '\0')
353*8822SCasper.Dik@Sun.COM 			return (stdmain(argc, argv));
354*8822SCasper.Dik@Sun.COM 	}
355*8822SCasper.Dik@Sun.COM 
356*8822SCasper.Dik@Sun.COM 	me = getexecname();
357*8822SCasper.Dik@Sun.COM 
358*8822SCasper.Dik@Sun.COM 	if (me != NULL && strstr(me, "ucb") != NULL)
359*8822SCasper.Dik@Sun.COM 		return (ucbmain(argc, argv));
360*8822SCasper.Dik@Sun.COM 	else
361*8822SCasper.Dik@Sun.COM 		return (stdmain(argc, argv));
362*8822SCasper.Dik@Sun.COM }
363*8822SCasper.Dik@Sun.COM 
364*8822SCasper.Dik@Sun.COM static int
stdmain(int argc,char ** argv)365*8822SCasper.Dik@Sun.COM stdmain(int argc, char **argv)
366*8822SCasper.Dik@Sun.COM {
3670Sstevel@tonic-gate 	char	*p;
3680Sstevel@tonic-gate 	char	*p1;
3690Sstevel@tonic-gate 	char	*parg;
3700Sstevel@tonic-gate 	int	c;
3710Sstevel@tonic-gate 	int	i;
3720Sstevel@tonic-gate 	int	pgerrflg = 0;	/* err flg: non-numeric arg w/p & g options */
3732966Ssayama 	size_t	size, len;
3740Sstevel@tonic-gate 	DIR	*dirp;
3750Sstevel@tonic-gate 	struct dirent *dentp;
3760Sstevel@tonic-gate 	pid_t	maxpid;
3770Sstevel@tonic-gate 	pid_t	id;
3780Sstevel@tonic-gate 	int	ret;
3792966Ssayama 	char	loc_stime_str[32];
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3820Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3830Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
3840Sstevel@tonic-gate #endif
3850Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	(void) memset(&euid_tbl, 0, sizeof (euid_tbl));
3880Sstevel@tonic-gate 	(void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
3890Sstevel@tonic-gate 	(void) memset(&egid_tbl, 0, sizeof (egid_tbl));
3900Sstevel@tonic-gate 	(void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * calculate width of pid fields based on configured MAXPID
3980Sstevel@tonic-gate 	 * (must be at least 5 to retain output format compatibility)
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	id = maxpid = (pid_t)sysconf(_SC_MAXPID);
4010Sstevel@tonic-gate 	pidwidth = 1;
4020Sstevel@tonic-gate 	while ((id /= 10) > 0)
4030Sstevel@tonic-gate 		++pidwidth;
4040Sstevel@tonic-gate 	pidwidth = pidwidth < 5 ? 5 : pidwidth;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	fname[F_PID].width = fname[F_PPID].width = pidwidth;
4070Sstevel@tonic-gate 	fname[F_PGID].width = fname[F_SID].width = pidwidth;
4080Sstevel@tonic-gate 
4092966Ssayama 	/*
4102966Ssayama 	 * TRANSLATION_NOTE
4112966Ssayama 	 * Specify the printf format with width and precision for
4122966Ssayama 	 * the STIME field.
4132966Ssayama 	 */
4142966Ssayama 	len = snprintf(loc_stime_str, sizeof (loc_stime_str),
4152966Ssayama 	    dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
4162966Ssayama 	if (len >= sizeof (loc_stime_str))
4172966Ssayama 		len = sizeof (loc_stime_str) - 1;
4182966Ssayama 
4192966Ssayama 	fname[F_STIME].width = fname[F_STIME].minwidth = len;
4202966Ssayama 
4212685Sakolb 	while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
4222685Sakolb 	    != EOF)
4230Sstevel@tonic-gate 		switch (c) {
4242685Sakolb 		case 'H':		/* Show home lgroups */
4252685Sakolb 			Hflg++;
4262685Sakolb 			break;
4272685Sakolb 		case 'h':
4282685Sakolb 			/*
4292685Sakolb 			 * Show processes/threads with given home lgroups
4302685Sakolb 			 */
4312685Sakolb 			hflg++;
4322685Sakolb 			p1 = optarg;
4332685Sakolb 			do {
4342685Sakolb 				int id;
4352685Sakolb 
4362685Sakolb 				/*
4372685Sakolb 				 * Get all IDs in the list, verify for
4382685Sakolb 				 * correctness and place in lgrps array.
4392685Sakolb 				 */
4402685Sakolb 				parg = getarg(&p1);
4412685Sakolb 				/* Convert string to integer */
4422685Sakolb 				ret = str2id(parg, (pid_t *)&id, 0,
4438233Sdme@sun.com 				    MAX_LGRP_ID);
4442685Sakolb 				/* Complain if ID didn't parse correctly */
4452685Sakolb 				if (ret != 0) {
4462685Sakolb 					pgerrflg++;
4472685Sakolb 					(void) fprintf(stderr,
4482685Sakolb 					    gettext("ps: %s "), parg);
4492685Sakolb 					if (ret == EINVAL)
4502685Sakolb 						(void) fprintf(stderr,
4512685Sakolb 						    gettext("is an invalid "
4522685Sakolb 						    "non-numeric argument"));
4532685Sakolb 					else
4542685Sakolb 						(void) fprintf(stderr,
4552685Sakolb 						    gettext("exceeds valid "
4562685Sakolb 						    "range"));
4572685Sakolb 					(void) fprintf(stderr,
4582685Sakolb 					    gettext(" for -h option\n"));
4592685Sakolb 					continue;
4602685Sakolb 				}
4612685Sakolb 
4622685Sakolb 				/* Extend lgrps array if needed */
4632685Sakolb 				if (nlgrps == lgrps_size) {
4642685Sakolb 					/* Double the size of the lgrps array */
4652685Sakolb 					if (lgrps_size == 0)
4662685Sakolb 						lgrps_size = SIZ;
4672685Sakolb 					lgrps_size *= 2;
4682685Sakolb 					lgrps = Realloc(lgrps,
4692685Sakolb 					    lgrps_size * sizeof (int));
4702685Sakolb 				}
4712685Sakolb 				/* place the id in the lgrps table */
4722685Sakolb 				lgrps[nlgrps++] = id;
4732685Sakolb 			} while (*p1);
4742685Sakolb 			break;
4750Sstevel@tonic-gate 		case 'l':		/* long listing */
4760Sstevel@tonic-gate 			lflg++;
4770Sstevel@tonic-gate 			break;
4780Sstevel@tonic-gate 		case 'f':		/* full listing */
4790Sstevel@tonic-gate 			fflg++;
4800Sstevel@tonic-gate 			break;
4810Sstevel@tonic-gate 		case 'j':
4820Sstevel@tonic-gate 			jflg++;
4830Sstevel@tonic-gate 			break;
4840Sstevel@tonic-gate 		case 'c':
4850Sstevel@tonic-gate 			/*
4860Sstevel@tonic-gate 			 * Format output to reflect scheduler changes:
4870Sstevel@tonic-gate 			 * high numbers for high priorities and don't
4880Sstevel@tonic-gate 			 * print nice or p_cpu values.  'c' option only
4890Sstevel@tonic-gate 			 * effective when used with 'l' or 'f' options.
4900Sstevel@tonic-gate 			 */
4910Sstevel@tonic-gate 			cflg++;
4920Sstevel@tonic-gate 			break;
4930Sstevel@tonic-gate 		case 'A':		/* list every process */
4940Sstevel@tonic-gate 		case 'e':		/* (obsolete) list every process */
4950Sstevel@tonic-gate 			Aflg++;
4960Sstevel@tonic-gate 			tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
4972685Sakolb 			zflg = hflg = 0;
4980Sstevel@tonic-gate 			break;
4990Sstevel@tonic-gate 		case 'a':
5000Sstevel@tonic-gate 			/*
5010Sstevel@tonic-gate 			 * Same as 'e' except no session group leaders
5020Sstevel@tonic-gate 			 * and no non-terminal processes.
5030Sstevel@tonic-gate 			 */
5040Sstevel@tonic-gate 			aflg++;
5050Sstevel@tonic-gate 			break;
5060Sstevel@tonic-gate 		case 'd':	/* same as e except no session leaders */
5070Sstevel@tonic-gate 			dflg++;
5080Sstevel@tonic-gate 			break;
5090Sstevel@tonic-gate 		case 'L':	/* show lwps */
5100Sstevel@tonic-gate 			Lflg++;
5110Sstevel@tonic-gate 			break;
5120Sstevel@tonic-gate 		case 'P':	/* show bound processor */
5130Sstevel@tonic-gate 			Pflg++;
5140Sstevel@tonic-gate 			break;
5150Sstevel@tonic-gate 		case 'y':	/* omit F & ADDR, report RSS & SZ in Kby */
5160Sstevel@tonic-gate 			yflg++;
5170Sstevel@tonic-gate 			break;
5180Sstevel@tonic-gate 		case 'n':	/* no longer needed; retain as no-op */
5190Sstevel@tonic-gate 			(void) fprintf(stderr,
5200Sstevel@tonic-gate 			    gettext("ps: warning: -n option ignored\n"));
5210Sstevel@tonic-gate 			break;
5220Sstevel@tonic-gate 		case 't':		/* terminals */
5230Sstevel@tonic-gate #define	TSZ	30
5240Sstevel@tonic-gate 			tflg++;
5250Sstevel@tonic-gate 			p1 = optarg;
5260Sstevel@tonic-gate 			do {
5270Sstevel@tonic-gate 				char nambuf[TSZ+6];	/* for "/dev/" + '\0' */
5280Sstevel@tonic-gate 				struct stat64 s;
5290Sstevel@tonic-gate 				parg = getarg(&p1);
5300Sstevel@tonic-gate 				p = Realloc(NULL, TSZ+1);	/* for '\0' */
5310Sstevel@tonic-gate 				/* zero the buffer before using it */
5320Sstevel@tonic-gate 				p[0] = '\0';
5330Sstevel@tonic-gate 				size = TSZ;
5340Sstevel@tonic-gate 				if (isdigit(*parg)) {
5350Sstevel@tonic-gate 					(void) strcpy(p, "tty");
5360Sstevel@tonic-gate 					size -= 3;
5370Sstevel@tonic-gate 				}
5380Sstevel@tonic-gate 				(void) strncat(p, parg, size);
5390Sstevel@tonic-gate 				if (ntty == ttysz) {
5400Sstevel@tonic-gate 					if ((ttysz *= 2) == 0)
5410Sstevel@tonic-gate 						ttysz = NTTYS;
5420Sstevel@tonic-gate 					tty = Realloc(tty,
5430Sstevel@tonic-gate 					    (ttysz + 1) * sizeof (struct tty));
5440Sstevel@tonic-gate 				}
5450Sstevel@tonic-gate 				tty[ntty].tdev = PRNODEV;
5460Sstevel@tonic-gate 				(void) strcpy(nambuf, "/dev/");
5470Sstevel@tonic-gate 				(void) strcat(nambuf, p);
5480Sstevel@tonic-gate 				if (stat64(nambuf, &s) == 0)
5490Sstevel@tonic-gate 					tty[ntty].tdev = s.st_rdev;
5500Sstevel@tonic-gate 				tty[ntty++].tname = p;
5510Sstevel@tonic-gate 			} while (*p1);
5520Sstevel@tonic-gate 			break;
5530Sstevel@tonic-gate 		case 'p':		/* proc ids */
5540Sstevel@tonic-gate 			pflg++;
5550Sstevel@tonic-gate 			p1 = optarg;
5560Sstevel@tonic-gate 			do {
5570Sstevel@tonic-gate 				pid_t id;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 				parg = getarg(&p1);
5600Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5610Sstevel@tonic-gate 					pgerrflg++;
5620Sstevel@tonic-gate 					(void) fprintf(stderr,
5630Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5640Sstevel@tonic-gate 					if (ret == EINVAL)
5650Sstevel@tonic-gate 						(void) fprintf(stderr,
5660Sstevel@tonic-gate 						    gettext("is an invalid "
5670Sstevel@tonic-gate 						    "non-numeric argument"));
5680Sstevel@tonic-gate 					else
5690Sstevel@tonic-gate 						(void) fprintf(stderr,
5700Sstevel@tonic-gate 						    gettext("exceeds valid "
5710Sstevel@tonic-gate 						    "range"));
5720Sstevel@tonic-gate 					(void) fprintf(stderr,
5730Sstevel@tonic-gate 					    gettext(" for -p option\n"));
5740Sstevel@tonic-gate 					continue;
5750Sstevel@tonic-gate 				}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 				if (npid == pidsz) {
5780Sstevel@tonic-gate 					if ((pidsz *= 2) == 0)
5790Sstevel@tonic-gate 						pidsz = SIZ;
5800Sstevel@tonic-gate 					pid = Realloc(pid,
5810Sstevel@tonic-gate 					    pidsz * sizeof (pid_t));
5820Sstevel@tonic-gate 				}
5830Sstevel@tonic-gate 				pid[npid++] = id;
5840Sstevel@tonic-gate 			} while (*p1);
5850Sstevel@tonic-gate 			break;
5860Sstevel@tonic-gate 		case 's':		/* session */
5870Sstevel@tonic-gate 			sflg++;
5880Sstevel@tonic-gate 			p1 = optarg;
5890Sstevel@tonic-gate 			do {
5900Sstevel@tonic-gate 				pid_t id;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 				parg = getarg(&p1);
5930Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5940Sstevel@tonic-gate 					pgerrflg++;
5950Sstevel@tonic-gate 					(void) fprintf(stderr,
5960Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5970Sstevel@tonic-gate 					if (ret == EINVAL)
5980Sstevel@tonic-gate 						(void) fprintf(stderr,
5990Sstevel@tonic-gate 						    gettext("is an invalid "
6000Sstevel@tonic-gate 						    "non-numeric argument"));
6010Sstevel@tonic-gate 					else
6020Sstevel@tonic-gate 						(void) fprintf(stderr,
6030Sstevel@tonic-gate 						    gettext("exceeds valid "
6040Sstevel@tonic-gate 						    "range"));
6050Sstevel@tonic-gate 					(void) fprintf(stderr,
6060Sstevel@tonic-gate 					    gettext(" for -s option\n"));
6070Sstevel@tonic-gate 					continue;
6080Sstevel@tonic-gate 				}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 				if (nsessid == sessidsz) {
6110Sstevel@tonic-gate 					if ((sessidsz *= 2) == 0)
6120Sstevel@tonic-gate 						sessidsz = SIZ;
6130Sstevel@tonic-gate 					sessid = Realloc(sessid,
6140Sstevel@tonic-gate 					    sessidsz * sizeof (pid_t));
6150Sstevel@tonic-gate 				}
6160Sstevel@tonic-gate 				sessid[nsessid++] = id;
6170Sstevel@tonic-gate 			} while (*p1);
6180Sstevel@tonic-gate 			break;
6190Sstevel@tonic-gate 		case 'g':		/* proc group */
6200Sstevel@tonic-gate 			gflg++;
6210Sstevel@tonic-gate 			p1 = optarg;
6220Sstevel@tonic-gate 			do {
6230Sstevel@tonic-gate 				pid_t id;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 				parg = getarg(&p1);
6260Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
6270Sstevel@tonic-gate 					pgerrflg++;
6280Sstevel@tonic-gate 					(void) fprintf(stderr,
6290Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
6300Sstevel@tonic-gate 					if (ret == EINVAL)
6310Sstevel@tonic-gate 						(void) fprintf(stderr,
6320Sstevel@tonic-gate 						    gettext("is an invalid "
6330Sstevel@tonic-gate 						    "non-numeric argument"));
6340Sstevel@tonic-gate 					else
6350Sstevel@tonic-gate 						(void) fprintf(stderr,
6360Sstevel@tonic-gate 						    gettext("exceeds valid "
6370Sstevel@tonic-gate 						    "range"));
6380Sstevel@tonic-gate 					(void) fprintf(stderr,
6390Sstevel@tonic-gate 					    gettext(" for -g option\n"));
6400Sstevel@tonic-gate 					continue;
6410Sstevel@tonic-gate 				}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 				if (ngrpid == grpidsz) {
6440Sstevel@tonic-gate 					if ((grpidsz *= 2) == 0)
6450Sstevel@tonic-gate 						grpidsz = SIZ;
6460Sstevel@tonic-gate 					grpid = Realloc(grpid,
6470Sstevel@tonic-gate 					    grpidsz * sizeof (pid_t));
6480Sstevel@tonic-gate 				}
6490Sstevel@tonic-gate 				grpid[ngrpid++] = id;
6500Sstevel@tonic-gate 			} while (*p1);
6510Sstevel@tonic-gate 			break;
6520Sstevel@tonic-gate 		case 'u':		/* effective user name or number */
6530Sstevel@tonic-gate 			uflg++;
6540Sstevel@tonic-gate 			p1 = optarg;
6550Sstevel@tonic-gate 			do {
6560Sstevel@tonic-gate 				parg = getarg(&p1);
6570Sstevel@tonic-gate 				add_ugentry(&euid_tbl, parg);
6580Sstevel@tonic-gate 			} while (*p1);
6590Sstevel@tonic-gate 			break;
6600Sstevel@tonic-gate 		case 'U':		/* real user name or number */
6610Sstevel@tonic-gate 			Uflg++;
6620Sstevel@tonic-gate 			p1 = optarg;
6630Sstevel@tonic-gate 			do {
6640Sstevel@tonic-gate 				parg = getarg(&p1);
6650Sstevel@tonic-gate 				add_ugentry(&ruid_tbl, parg);
6660Sstevel@tonic-gate 			} while (*p1);
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		case 'G':		/* real group name or number */
6690Sstevel@tonic-gate 			Gflg++;
6700Sstevel@tonic-gate 			p1 = optarg;
6710Sstevel@tonic-gate 			do {
6720Sstevel@tonic-gate 				parg = getarg(&p1);
6730Sstevel@tonic-gate 				add_ugentry(&rgid_tbl, parg);
6740Sstevel@tonic-gate 			} while (*p1);
6750Sstevel@tonic-gate 			break;
6760Sstevel@tonic-gate 		case 'o':		/* output format */
6770Sstevel@tonic-gate 			p = optarg;
6780Sstevel@tonic-gate 			while ((p = parse_format(p)) != NULL)
6790Sstevel@tonic-gate 				;
6800Sstevel@tonic-gate 			break;
6810Sstevel@tonic-gate 		case 'z':		/* zone name or number */
6820Sstevel@tonic-gate 			zflg++;
6830Sstevel@tonic-gate 			p1 = optarg;
6840Sstevel@tonic-gate 			do {
6850Sstevel@tonic-gate 				zoneid_t id;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 				parg = getarg(&p1);
6880Sstevel@tonic-gate 				if (zone_get_id(parg, &id) != 0) {
6890Sstevel@tonic-gate 					pgerrflg++;
6900Sstevel@tonic-gate 					(void) fprintf(stderr,
6910Sstevel@tonic-gate 					    gettext("ps: unknown zone %s\n"),
6920Sstevel@tonic-gate 					    parg);
6930Sstevel@tonic-gate 					continue;
6940Sstevel@tonic-gate 				}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 				if (nzoneid == zoneidsz) {
6970Sstevel@tonic-gate 					if ((zoneidsz *= 2) == 0)
6980Sstevel@tonic-gate 						zoneidsz = SIZ;
6990Sstevel@tonic-gate 					zoneid = Realloc(zoneid,
7000Sstevel@tonic-gate 					    zoneidsz * sizeof (zoneid_t));
7010Sstevel@tonic-gate 				}
7020Sstevel@tonic-gate 				zoneid[nzoneid++] = id;
7030Sstevel@tonic-gate 			} while (*p1);
7040Sstevel@tonic-gate 			break;
7050Sstevel@tonic-gate 		case 'Z':		/* show zone name */
7060Sstevel@tonic-gate 			Zflg++;
7070Sstevel@tonic-gate 			break;
7080Sstevel@tonic-gate 		default:			/* error on ? */
7090Sstevel@tonic-gate 			errflg++;
7100Sstevel@tonic-gate 			break;
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (errflg || optind < argc || pgerrflg)
7140Sstevel@tonic-gate 		usage();
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (tflg)
7170Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * If an appropriate option has not been specified, use the
7200Sstevel@tonic-gate 	 * current terminal and effective uid as the default.
7210Sstevel@tonic-gate 	 */
7222685Sakolb 	if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
7230Sstevel@tonic-gate 		psinfo_t info;
7240Sstevel@tonic-gate 		int procfd;
7250Sstevel@tonic-gate 		char *name;
7260Sstevel@tonic-gate 		char pname[100];
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		/* get our own controlling tty name using /proc */
7290Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname),
7300Sstevel@tonic-gate 		    "%s/self/psinfo", procdir);
7310Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0 ||
7320Sstevel@tonic-gate 		    read(procfd, (char *)&info, sizeof (info)) < 0 ||
7330Sstevel@tonic-gate 		    info.pr_ttydev == PRNODEV) {
7340Sstevel@tonic-gate 			(void) fprintf(stderr,
7350Sstevel@tonic-gate 			    gettext("ps: no controlling terminal\n"));
7360Sstevel@tonic-gate 			exit(1);
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		(void) close(procfd);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		i = 0;
7410Sstevel@tonic-gate 		name = gettty(&info);
7420Sstevel@tonic-gate 		if (*name == '?') {
7430Sstevel@tonic-gate 			(void) fprintf(stderr,
7440Sstevel@tonic-gate 			    gettext("ps: can't find controlling terminal\n"));
7450Sstevel@tonic-gate 			exit(1);
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 		if (ntty == ttysz) {
7480Sstevel@tonic-gate 			if ((ttysz *= 2) == 0)
7490Sstevel@tonic-gate 				ttysz = NTTYS;
7500Sstevel@tonic-gate 			tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		tty[ntty].tdev = info.pr_ttydev;
7530Sstevel@tonic-gate 		tty[ntty++].tname = name;
7540Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7550Sstevel@tonic-gate 		tflg++;
7560Sstevel@tonic-gate 		tuid = getuid();
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 	if (Aflg) {
7590Sstevel@tonic-gate 		Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
7602685Sakolb 		zflg = hflg = 0;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 	if (Aflg | aflg | dflg)
7630Sstevel@tonic-gate 		tflg = 0;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	i = 0;		/* prepare to exit on name lookup errors */
7660Sstevel@tonic-gate 	i += uconv(&euid_tbl);
7670Sstevel@tonic-gate 	i += uconv(&ruid_tbl);
7680Sstevel@tonic-gate 	i += gconv(&egid_tbl);
7690Sstevel@tonic-gate 	i += gconv(&rgid_tbl);
7700Sstevel@tonic-gate 	if (i)
7710Sstevel@tonic-gate 		exit(1);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	/* allocate a buffer for lwpsinfo structures */
7740Sstevel@tonic-gate 	lpbufsize = 4096;
7750Sstevel@tonic-gate 	if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
7760Sstevel@tonic-gate 		(void) fprintf(stderr,
7770Sstevel@tonic-gate 		    gettext("ps: no memory\n"));
7780Sstevel@tonic-gate 		exit(1);
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (fields) {	/* print user-specified header */
7820Sstevel@tonic-gate 		if (do_header) {
7830Sstevel@tonic-gate 			struct field *f;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 			for (f = fields; f != NULL; f = f->next) {
7860Sstevel@tonic-gate 				if (f != fields)
7870Sstevel@tonic-gate 					(void) printf(" ");
7880Sstevel@tonic-gate 				switch (f->fname) {
7890Sstevel@tonic-gate 				case F_TTY:
7900Sstevel@tonic-gate 					(void) printf("%-*s",
7910Sstevel@tonic-gate 					    f->width, f->header);
7920Sstevel@tonic-gate 					break;
7930Sstevel@tonic-gate 				case F_FNAME:
7940Sstevel@tonic-gate 				case F_COMM:
7950Sstevel@tonic-gate 				case F_ARGS:
7960Sstevel@tonic-gate 					/*
7970Sstevel@tonic-gate 					 * Print these headers full width
7980Sstevel@tonic-gate 					 * unless they appear at the end.
7990Sstevel@tonic-gate 					 */
8000Sstevel@tonic-gate 					if (f->next != NULL) {
8010Sstevel@tonic-gate 						(void) printf("%-*s",
8020Sstevel@tonic-gate 						    f->width, f->header);
8030Sstevel@tonic-gate 					} else {
8040Sstevel@tonic-gate 						(void) printf("%s",
8050Sstevel@tonic-gate 						    f->header);
8060Sstevel@tonic-gate 					}
8070Sstevel@tonic-gate 					break;
8080Sstevel@tonic-gate 				default:
8090Sstevel@tonic-gate 					(void) printf("%*s",
8100Sstevel@tonic-gate 					    f->width, f->header);
8110Sstevel@tonic-gate 					break;
8120Sstevel@tonic-gate 				}
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 			(void) printf("\n");
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 	} else {	/* print standard header */
817*8822SCasper.Dik@Sun.COM 		/*
818*8822SCasper.Dik@Sun.COM 		 * All fields before 'PID' are printed with a trailing space
819*8822SCasper.Dik@Sun.COM 		 * as a separator and that is how we print the headers too.
820*8822SCasper.Dik@Sun.COM 		 */
8210Sstevel@tonic-gate 		if (lflg) {
8220Sstevel@tonic-gate 			if (yflg)
823*8822SCasper.Dik@Sun.COM 				(void) printf("S ");
8240Sstevel@tonic-gate 			else
825*8822SCasper.Dik@Sun.COM 				(void) printf(" F S ");
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 		if (Zflg)
828*8822SCasper.Dik@Sun.COM 			(void) printf("    ZONE ");
8290Sstevel@tonic-gate 		if (fflg) {
830*8822SCasper.Dik@Sun.COM 			(void) printf("     UID ");
8310Sstevel@tonic-gate 		} else if (lflg)
832*8822SCasper.Dik@Sun.COM 			(void) printf("   UID ");
8330Sstevel@tonic-gate 
834*8822SCasper.Dik@Sun.COM 		(void) printf("%*s", pidwidth,  "PID");
8350Sstevel@tonic-gate 		if (lflg || fflg)
8360Sstevel@tonic-gate 			(void) printf(" %*s", pidwidth, "PPID");
8370Sstevel@tonic-gate 		if (jflg)
8380Sstevel@tonic-gate 			(void) printf(" %*s %*s", pidwidth, "PGID",
8390Sstevel@tonic-gate 			    pidwidth, "SID");
8400Sstevel@tonic-gate 		if (Lflg)
8410Sstevel@tonic-gate 			(void) printf("   LWP");
8420Sstevel@tonic-gate 		if (Pflg)
8430Sstevel@tonic-gate 			(void) printf(" PSR");
8440Sstevel@tonic-gate 		if (Lflg && fflg)
8450Sstevel@tonic-gate 			(void) printf("  NLWP");
8460Sstevel@tonic-gate 		if (cflg)
8470Sstevel@tonic-gate 			(void) printf("  CLS PRI");
8480Sstevel@tonic-gate 		else if (lflg || fflg) {
8490Sstevel@tonic-gate 			(void) printf("   C");
8500Sstevel@tonic-gate 			if (lflg)
8510Sstevel@tonic-gate 				(void) printf(" PRI NI");
8520Sstevel@tonic-gate 		}
8530Sstevel@tonic-gate 		if (lflg) {
8540Sstevel@tonic-gate 			if (yflg)
8550Sstevel@tonic-gate 				(void) printf("   RSS     SZ    WCHAN");
8560Sstevel@tonic-gate 			else
8570Sstevel@tonic-gate 				(void) printf("     ADDR     SZ    WCHAN");
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 		if (fflg)
8602966Ssayama 			(void) printf(" %s", loc_stime_str);
8612685Sakolb 		if (Hflg)
8622685Sakolb 			(void) printf(" LGRP");
8630Sstevel@tonic-gate 		if (Lflg)
8640Sstevel@tonic-gate 			(void) printf(" TTY        LTIME CMD\n");
8650Sstevel@tonic-gate 		else
8660Sstevel@tonic-gate 			(void) printf(" TTY         TIME CMD\n");
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 
8702685Sakolb 	if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
8710Sstevel@tonic-gate 	    npid <= PTHRESHOLD) {
8720Sstevel@tonic-gate 		/*
8730Sstevel@tonic-gate 		 * If we are looking at specific processes go straight
8740Sstevel@tonic-gate 		 * to their /proc entries and don't scan /proc.
8750Sstevel@tonic-gate 		 */
8760Sstevel@tonic-gate 		int i;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		(void) qsort(pid, npid, sizeof (pid_t), pidcmp);
8790Sstevel@tonic-gate 		for (i = 0; i < npid; i++) {
8800Sstevel@tonic-gate 			char pname[12];
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 			if (i >= 1 && pid[i] == pid[i - 1])
8830Sstevel@tonic-gate 				continue;
8840Sstevel@tonic-gate 			(void) sprintf(pname, "%d", (int)pid[i]);
8850Sstevel@tonic-gate 			if (print_proc(pname) == 0)
8860Sstevel@tonic-gate 				retcode = 0;
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 	} else {
8890Sstevel@tonic-gate 		/*
8900Sstevel@tonic-gate 		 * Determine which processes to print info about by searching
8910Sstevel@tonic-gate 		 * the /proc directory and looking at each process.
8920Sstevel@tonic-gate 		 */
8930Sstevel@tonic-gate 		if ((dirp = opendir(procdir)) == NULL) {
8940Sstevel@tonic-gate 			(void) fprintf(stderr,
8950Sstevel@tonic-gate 			    gettext("ps: cannot open PROC directory %s\n"),
8960Sstevel@tonic-gate 			    procdir);
8970Sstevel@tonic-gate 			exit(1);
8980Sstevel@tonic-gate 		}
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 		/* for each active process --- */
9010Sstevel@tonic-gate 		while (dentp = readdir(dirp)) {
9020Sstevel@tonic-gate 			if (dentp->d_name[0] == '.')    /* skip . and .. */
9030Sstevel@tonic-gate 				continue;
9040Sstevel@tonic-gate 			if (print_proc(dentp->d_name) == 0)
9050Sstevel@tonic-gate 				retcode = 0;
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		(void) closedir(dirp);
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 	return (retcode);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate int
print_proc(char * pid_name)9150Sstevel@tonic-gate print_proc(char *pid_name)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	char	pname[PATH_MAX];
9180Sstevel@tonic-gate 	int	pdlen;
9190Sstevel@tonic-gate 	int	found;
9200Sstevel@tonic-gate 	int	procfd; /* filedescriptor for /proc/nnnnn/psinfo */
9210Sstevel@tonic-gate 	char	*tp;    /* ptr to ttyname,  if any */
9220Sstevel@tonic-gate 	psinfo_t info;  /* process information from /proc */
9230Sstevel@tonic-gate 	lwpsinfo_t *lwpsinfo;   /* array of lwpsinfo structs */
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
9260Sstevel@tonic-gate 	if (pdlen >= sizeof (pname) - 10)
9270Sstevel@tonic-gate 		return (1);
9280Sstevel@tonic-gate retry:
9290Sstevel@tonic-gate 	(void) strcpy(&pname[pdlen], "psinfo");
9300Sstevel@tonic-gate 	if ((procfd = open(pname, O_RDONLY)) == -1) {
9310Sstevel@tonic-gate 		/* Process may have exited meanwhile. */
9320Sstevel@tonic-gate 		return (1);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Get the info structure for the process and close quickly.
9360Sstevel@tonic-gate 	 */
9370Sstevel@tonic-gate 	if (read(procfd, (char *)&info, sizeof (info)) < 0) {
9380Sstevel@tonic-gate 		int	saverr = errno;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		(void) close(procfd);
9410Sstevel@tonic-gate 		if (saverr == EAGAIN)
9420Sstevel@tonic-gate 			goto retry;
9430Sstevel@tonic-gate 		if (saverr != ENOENT)
9440Sstevel@tonic-gate 			(void) fprintf(stderr,
9450Sstevel@tonic-gate 			    gettext("ps: read() on %s: %s\n"),
9460Sstevel@tonic-gate 			    pname, err_string(saverr));
9470Sstevel@tonic-gate 		return (1);
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 	(void) close(procfd);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	found = 0;
9520Sstevel@tonic-gate 	if (info.pr_lwp.pr_state == 0)	/* can't happen? */
9530Sstevel@tonic-gate 		return (1);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * Omit session group leaders for 'a' and 'd' options.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
9590Sstevel@tonic-gate 		return (1);
9600Sstevel@tonic-gate 	if (Aflg || dflg)
9610Sstevel@tonic-gate 		found++;
9620Sstevel@tonic-gate 	else if (pflg && search(pid, npid, info.pr_pid))
9630Sstevel@tonic-gate 		found++;	/* ppid in p option arg list */
9644321Scasper 	else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
9650Sstevel@tonic-gate 		found++;	/* puid in u option arg list */
9664321Scasper 	else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
9670Sstevel@tonic-gate 		found++;	/* puid in U option arg list */
9680Sstevel@tonic-gate #ifdef NOT_YET
9694321Scasper 	else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
9700Sstevel@tonic-gate 		found++;	/* pgid in g option arg list */
9710Sstevel@tonic-gate #endif	/* NOT_YET */
9724321Scasper 	else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
9730Sstevel@tonic-gate 		found++;	/* pgid in G option arg list */
9740Sstevel@tonic-gate 	else if (gflg && search(grpid, ngrpid, info.pr_pgid))
9750Sstevel@tonic-gate 		found++;	/* grpid in g option arg list */
9760Sstevel@tonic-gate 	else if (sflg && search(sessid, nsessid, info.pr_sid))
9770Sstevel@tonic-gate 		found++;	/* sessid in s option arg list */
9780Sstevel@tonic-gate 	else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
9790Sstevel@tonic-gate 		found++;	/* zoneid in z option arg list */
9802685Sakolb 	else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
9812685Sakolb 		found++;	/* home lgroup in h option arg list */
9820Sstevel@tonic-gate 	if (!found && !tflg && !aflg)
9830Sstevel@tonic-gate 		return (1);
9840Sstevel@tonic-gate 	if (!prfind(found, &info, &tp))
9850Sstevel@tonic-gate 		return (1);
9860Sstevel@tonic-gate 	if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
9870Sstevel@tonic-gate 		ssize_t prsz;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		(void) strcpy(&pname[pdlen], "lpsinfo");
9900Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
9910Sstevel@tonic-gate 			return (1);
9920Sstevel@tonic-gate 		/*
9930Sstevel@tonic-gate 		 * Get the info structures for the lwps.
9940Sstevel@tonic-gate 		 */
9950Sstevel@tonic-gate 		prsz = read(procfd, lpsinfobuf, lpbufsize);
9960Sstevel@tonic-gate 		if (prsz == -1) {
9970Sstevel@tonic-gate 			int	saverr = errno;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 			(void) close(procfd);
10000Sstevel@tonic-gate 			if (saverr == EAGAIN)
10010Sstevel@tonic-gate 				goto retry;
10020Sstevel@tonic-gate 			if (saverr != ENOENT)
10030Sstevel@tonic-gate 				(void) fprintf(stderr,
10040Sstevel@tonic-gate 				    gettext("ps: read() on %s: %s\n"),
10050Sstevel@tonic-gate 				    pname, err_string(saverr));
10060Sstevel@tonic-gate 			return (1);
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 		(void) close(procfd);
10090Sstevel@tonic-gate 		if (prsz == lpbufsize) {
10100Sstevel@tonic-gate 			/*
10110Sstevel@tonic-gate 			 * buffer overflow. Realloc new buffer.
10120Sstevel@tonic-gate 			 * Error handling is done in Realloc().
10130Sstevel@tonic-gate 			 */
10140Sstevel@tonic-gate 			lpbufsize *= 2;
10150Sstevel@tonic-gate 			lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
10160Sstevel@tonic-gate 			goto retry;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
10190Sstevel@tonic-gate 			goto retry;
10200Sstevel@tonic-gate 		lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 	if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
10230Sstevel@tonic-gate 		prcom(&info, tp);
10240Sstevel@tonic-gate 	} else {
10250Sstevel@tonic-gate 		int nlwp = 0;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 		do {
10280Sstevel@tonic-gate 			info.pr_lwp = *lwpsinfo;
10290Sstevel@tonic-gate 			prcom(&info, tp);
10300Sstevel@tonic-gate 			/* LINTED improper alignment */
10310Sstevel@tonic-gate 			lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
10328233Sdme@sun.com 			    lpsinfobuf->pr_entsize);
10330Sstevel@tonic-gate 		} while (++nlwp < lpsinfobuf->pr_nent);
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 	return (0);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate static void
usage(void)10400Sstevel@tonic-gate usage(void)		/* print usage message and quit */
10410Sstevel@tonic-gate {
10420Sstevel@tonic-gate 	static char usage1[] =
10432685Sakolb 	    "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
10440Sstevel@tonic-gate 	static char usage2[] =
10450Sstevel@tonic-gate 	    "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
10460Sstevel@tonic-gate 	static char usage3[] =
10472685Sakolb 	    "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] "
10482685Sakolb 	    "[-h lgrplist]";
10490Sstevel@tonic-gate 	static char usage4[] =
10500Sstevel@tonic-gate 	    "  'format' is one or more of:";
10510Sstevel@tonic-gate 	static char usage5[] =
10520Sstevel@tonic-gate 	    "\tuser ruser group rgroup uid ruid gid rgid pid ppid pgid "
10530Sstevel@tonic-gate 	    "sid taskid ctid";
10540Sstevel@tonic-gate 	static char usage6[] =
10550Sstevel@tonic-gate 	    "\tpri opri pcpu pmem vsz rss osz nice class time etime stime zone "
10560Sstevel@tonic-gate 	    "zoneid";
10570Sstevel@tonic-gate 	static char usage7[] =
10580Sstevel@tonic-gate 	    "\tf s c lwp nlwp psr tty addr wchan fname comm args "
10592685Sakolb 	    "projid project pset lgrp";
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	(void) fprintf(stderr,
10620Sstevel@tonic-gate 	    gettext("usage: %s\n%s\n%s\n%s\n%s\n%s\n%s\n"),
10630Sstevel@tonic-gate 	    gettext(usage1), gettext(usage2), gettext(usage3),
10640Sstevel@tonic-gate 	    gettext(usage4), gettext(usage5), gettext(usage6), gettext(usage7));
10650Sstevel@tonic-gate 	exit(1);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate  * getarg() finds the next argument in list and copies arg into argbuf.
10700Sstevel@tonic-gate  * p1 first pts to arg passed back from getopt routine.  p1 is then
10710Sstevel@tonic-gate  * bumped to next character that is not a comma or blank -- p1 NULL
10720Sstevel@tonic-gate  * indicates end of list.
10730Sstevel@tonic-gate  */
10740Sstevel@tonic-gate static char *
getarg(char ** pp1)10750Sstevel@tonic-gate getarg(char **pp1)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	static char argbuf[ARGSIZ];
10780Sstevel@tonic-gate 	char *p1 = *pp1;
10790Sstevel@tonic-gate 	char *parga = argbuf;
10800Sstevel@tonic-gate 	int c;
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10830Sstevel@tonic-gate 		p1++;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
10860Sstevel@tonic-gate 		if (parga < argbuf + ARGSIZ - 1)
10870Sstevel@tonic-gate 			*parga++ = c;
10880Sstevel@tonic-gate 		p1++;
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	*parga = '\0';
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
10930Sstevel@tonic-gate 		p1++;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	*pp1 = p1;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	return (argbuf);
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * parse_format() takes the argument to the -o option,
11020Sstevel@tonic-gate  * sets up the next output field structure, and returns
11030Sstevel@tonic-gate  * a pointer to any further output field specifier(s).
11040Sstevel@tonic-gate  * As a side-effect, it increments errflg if encounters a format error.
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate static char *
parse_format(char * arg)11070Sstevel@tonic-gate parse_format(char *arg)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 	int c;
11100Sstevel@tonic-gate 	char *name;
11110Sstevel@tonic-gate 	char *header = NULL;
11120Sstevel@tonic-gate 	int width = 0;
11130Sstevel@tonic-gate 	struct def_field *df;
11140Sstevel@tonic-gate 	struct field *f;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
11170Sstevel@tonic-gate 		arg++;
11180Sstevel@tonic-gate 	if (c == '\0')
11190Sstevel@tonic-gate 		return (NULL);
11200Sstevel@tonic-gate 	name = arg;
11210Sstevel@tonic-gate 	arg = strpbrk(arg, " \t\r\v\f\n,=");
11220Sstevel@tonic-gate 	if (arg != NULL) {
11230Sstevel@tonic-gate 		c = *arg;
11240Sstevel@tonic-gate 		*arg++ = '\0';
11250Sstevel@tonic-gate 		if (c == '=') {
11260Sstevel@tonic-gate 			char *s;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 			header = arg;
11290Sstevel@tonic-gate 			arg = NULL;
11300Sstevel@tonic-gate 			width = strlen(header);
11310Sstevel@tonic-gate 			s = header + width;
11320Sstevel@tonic-gate 			while (s > header && isspace(*--s))
11330Sstevel@tonic-gate 				*s = '\0';
11340Sstevel@tonic-gate 			while (isspace(*header))
11350Sstevel@tonic-gate 				header++;
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 	for (df = &fname[0]; df < &fname[NFIELDS]; df++)
11390Sstevel@tonic-gate 		if (strcmp(name, df->fname) == 0) {
11400Sstevel@tonic-gate 			if (strcmp(name, "lwp") == 0)
11410Sstevel@tonic-gate 				Lflg++;
11420Sstevel@tonic-gate 			break;
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 	if (df >= &fname[NFIELDS]) {
11450Sstevel@tonic-gate 		(void) fprintf(stderr,
11468233Sdme@sun.com 		    gettext("ps: unknown output format: -o %s\n"),
11478233Sdme@sun.com 		    name);
11480Sstevel@tonic-gate 		errflg++;
11490Sstevel@tonic-gate 		return (arg);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 	if ((f = malloc(sizeof (*f))) == NULL) {
11520Sstevel@tonic-gate 		(void) fprintf(stderr,
11530Sstevel@tonic-gate 		    gettext("ps: malloc() for output format failed, %s\n"),
11540Sstevel@tonic-gate 		    err_string(errno));
11550Sstevel@tonic-gate 		exit(1);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	f->next = NULL;
11580Sstevel@tonic-gate 	f->fname = df - &fname[0];
11590Sstevel@tonic-gate 	f->header = header? header : df->header;
11600Sstevel@tonic-gate 	if (width == 0)
11610Sstevel@tonic-gate 		width = df->width;
11620Sstevel@tonic-gate 	if (*f->header != '\0')
11630Sstevel@tonic-gate 		do_header = 1;
11640Sstevel@tonic-gate 	f->width = max(width, df->minwidth);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (fields == NULL)
11670Sstevel@tonic-gate 		fields = last_field = f;
11680Sstevel@tonic-gate 	else {
11690Sstevel@tonic-gate 		last_field->next = f;
11700Sstevel@tonic-gate 		last_field = f;
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	return (arg);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate static char *
devlookup(dev_t ddev)11770Sstevel@tonic-gate devlookup(dev_t ddev)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate 	struct devl *dp;
11800Sstevel@tonic-gate 	int i;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	for (dp = devl, i = 0; i < ndev; dp++, i++) {
11830Sstevel@tonic-gate 		if (dp->ddev == ddev)
11840Sstevel@tonic-gate 			return (dp->dname);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 	return (NULL);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate static char *
devadd(char * name,dev_t ddev)11900Sstevel@tonic-gate devadd(char *name, dev_t ddev)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	struct devl *dp;
11930Sstevel@tonic-gate 	int leng, start, i;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if (ndev == maxdev) {
11960Sstevel@tonic-gate 		maxdev += DNINCR;
11970Sstevel@tonic-gate 		devl = Realloc(devl, maxdev * sizeof (struct devl));
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 	dp = &devl[ndev++];
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	dp->ddev = ddev;
12020Sstevel@tonic-gate 	if (name == NULL) {
12030Sstevel@tonic-gate 		(void) strcpy(dp->dname, "??");
12040Sstevel@tonic-gate 		return (dp->dname);
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	leng = strlen(name);
12080Sstevel@tonic-gate 	/* Strip off /dev/ */
12090Sstevel@tonic-gate 	if (leng < DNSIZE + 4)
12100Sstevel@tonic-gate 		(void) strcpy(dp->dname, &name[5]);
12110Sstevel@tonic-gate 	else {
12120Sstevel@tonic-gate 		start = leng - DNSIZE - 1;
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 		for (i = start; i < leng && name[i] != '/'; i++)
12150Sstevel@tonic-gate 				;
12160Sstevel@tonic-gate 		if (i == leng)
12170Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[start], DNSIZE);
12180Sstevel@tonic-gate 		else
12190Sstevel@tonic-gate 			(void) strncpy(dp->dname, &name[i+1], DNSIZE);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 	return (dp->dname);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate /*
12250Sstevel@tonic-gate  * gettty returns the user's tty number or ? if none.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate static char *
gettty(psinfo_t * psinfo)12280Sstevel@tonic-gate gettty(psinfo_t *psinfo)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	extern char *_ttyname_dev(dev_t, char *, size_t);
12310Sstevel@tonic-gate 	char devname[TTYNAME_MAX];
12320Sstevel@tonic-gate 	char *retval;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	if (psinfo->pr_ttydev == PRNODEV)
12350Sstevel@tonic-gate 		return ("?");
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
12380Sstevel@tonic-gate 		return (retval);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	return (devadd(retval, psinfo->pr_ttydev));
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * Find the process's tty and return 1 if process is to be printed.
12470Sstevel@tonic-gate  */
12480Sstevel@tonic-gate static int
prfind(int found,psinfo_t * psinfo,char ** tpp)12490Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	char	*tp;
12520Sstevel@tonic-gate 	struct tty *ttyp;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
12550Sstevel@tonic-gate 		/* process is a zombie */
12560Sstevel@tonic-gate 		*tpp = "?";
12570Sstevel@tonic-gate 		if (tflg && !found)
12580Sstevel@tonic-gate 			return (0);
12590Sstevel@tonic-gate 		return (1);
12600Sstevel@tonic-gate 	}
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * Get current terminal.  If none ("?") and 'a' is set, don't print
12640Sstevel@tonic-gate 	 * info.  If 't' is set, check if term is in list of desired terminals
12650Sstevel@tonic-gate 	 * and print it if it is.
12660Sstevel@tonic-gate 	 */
12670Sstevel@tonic-gate 	tp = gettty(psinfo);
12680Sstevel@tonic-gate 	if (aflg && *tp == '?') {
12690Sstevel@tonic-gate 		*tpp = tp;
12700Sstevel@tonic-gate 		return (0);
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 	if (tflg && !found) {
12730Sstevel@tonic-gate 		int match = 0;
12740Sstevel@tonic-gate 		char *other = NULL;
12750Sstevel@tonic-gate 		for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
12760Sstevel@tonic-gate 			/*
12770Sstevel@tonic-gate 			 * Look for a name match
12780Sstevel@tonic-gate 			 */
12790Sstevel@tonic-gate 			if (strcmp(tp, ttyp->tname) == 0) {
12800Sstevel@tonic-gate 				match = 1;
12810Sstevel@tonic-gate 				break;
12820Sstevel@tonic-gate 			}
12830Sstevel@tonic-gate 			/*
12840Sstevel@tonic-gate 			 * Look for same device under different names.
12850Sstevel@tonic-gate 			 */
12860Sstevel@tonic-gate 			if ((other == NULL) &&
12870Sstevel@tonic-gate 			    (ttyp->tdev != PRNODEV) &&
12880Sstevel@tonic-gate 			    (psinfo->pr_ttydev == ttyp->tdev))
12890Sstevel@tonic-gate 				other = ttyp->tname;
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 		if (!match && (other != NULL)) {
12920Sstevel@tonic-gate 			/*
12930Sstevel@tonic-gate 			 * found under a different name
12940Sstevel@tonic-gate 			 */
12950Sstevel@tonic-gate 			match = 1;
12960Sstevel@tonic-gate 			tp = other;
12970Sstevel@tonic-gate 		}
12984321Scasper 		if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
12990Sstevel@tonic-gate 			/*
13000Sstevel@tonic-gate 			 * not found OR not matching euid
13010Sstevel@tonic-gate 			 */
13020Sstevel@tonic-gate 			*tpp = tp;
13030Sstevel@tonic-gate 			return (0);
13040Sstevel@tonic-gate 		}
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 	*tpp = tp;
13070Sstevel@tonic-gate 	return (1);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate /*
13110Sstevel@tonic-gate  * Print info about the process.
13120Sstevel@tonic-gate  */
13130Sstevel@tonic-gate static void
prcom(psinfo_t * psinfo,char * ttyp)13140Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 	char	*cp;
13170Sstevel@tonic-gate 	long	tm;
13180Sstevel@tonic-gate 	int	bytesleft;
13190Sstevel@tonic-gate 	int	wcnt, length;
13200Sstevel@tonic-gate 	wchar_t	wchar;
13210Sstevel@tonic-gate 	struct passwd *pwd;
13220Sstevel@tonic-gate 	int	zombie_lwp;
13230Sstevel@tonic-gate 	char	zonename[ZONENAME_MAX];
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * If process is zombie, call zombie print routine and return.
13270Sstevel@tonic-gate 	 */
13280Sstevel@tonic-gate 	if (psinfo->pr_nlwp == 0) {
13290Sstevel@tonic-gate 		if (fields != NULL)
13300Sstevel@tonic-gate 			pr_fields(psinfo, ttyp, print_zombie_field);
13310Sstevel@tonic-gate 		else
13320Sstevel@tonic-gate 			przom(psinfo);
13330Sstevel@tonic-gate 		return;
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	/*
13390Sstevel@tonic-gate 	 * If user specified '-o format', print requested fields and return.
13400Sstevel@tonic-gate 	 */
13410Sstevel@tonic-gate 	if (fields != NULL) {
13420Sstevel@tonic-gate 		pr_fields(psinfo, ttyp, print_field);
13430Sstevel@tonic-gate 		return;
13440Sstevel@tonic-gate 	}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	/*
13470Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
1348*8822SCasper.Dik@Sun.COM 	 * separator, rather than keeping track of which column is first.  All
13490Sstevel@tonic-gate 	 * other fields are printed with a leading space.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	if (lflg) {
13520Sstevel@tonic-gate 		if (!yflg)
13530Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
13540Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if (Zflg) {						/* ZONE */
13580Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
13590Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
1360*8822SCasper.Dik@Sun.COM 			(void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
13610Sstevel@tonic-gate 		} else {
13620Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
13630Sstevel@tonic-gate 		}
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	if (fflg) {						/* UID */
13670Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
13680Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
13690Sstevel@tonic-gate 		else
1370*8822SCasper.Dik@Sun.COM 			(void) printf(" %7.7u ", psinfo->pr_euid);
13710Sstevel@tonic-gate 	} else if (lflg) {
13724321Scasper 		(void) printf("%6u ", psinfo->pr_euid);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
13750Sstevel@tonic-gate 	if (lflg || fflg)
13760Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13770Sstevel@tonic-gate 		    (int)psinfo->pr_ppid); /* PPID */
13780Sstevel@tonic-gate 	if (jflg) {
13790Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13800Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);	/* PGID */
13810Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
13820Sstevel@tonic-gate 		    (int)psinfo->pr_sid);	/* SID  */
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 	if (Lflg)
13850Sstevel@tonic-gate 		(void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
13860Sstevel@tonic-gate 	if (Pflg) {
13870Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE)	/* PSR */
13880Sstevel@tonic-gate 			(void) printf("   -");
13890Sstevel@tonic-gate 		else
13900Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 	if (Lflg && fflg)					/* NLWP */
13930Sstevel@tonic-gate 		(void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
13940Sstevel@tonic-gate 	if (cflg) {
13950Sstevel@tonic-gate 		if (zombie_lwp)					/* CLS */
13960Sstevel@tonic-gate 			(void) printf("     ");
13970Sstevel@tonic-gate 		else
13980Sstevel@tonic-gate 			(void) printf(" %4s", psinfo->pr_lwp.pr_clname);
13990Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI */
14000Sstevel@tonic-gate 	} else if (lflg || fflg) {
14010Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
14020Sstevel@tonic-gate 		if (lflg) {					    /* PRI NI */
14030Sstevel@tonic-gate 			/*
14040Sstevel@tonic-gate 			 * Print priorities the old way (lower numbers
14050Sstevel@tonic-gate 			 * mean higher priority) and print nice value
14060Sstevel@tonic-gate 			 * for time sharing procs.
14070Sstevel@tonic-gate 			 */
14080Sstevel@tonic-gate 			(void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
14090Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_oldpri != 0)
14100Sstevel@tonic-gate 				(void) printf(" %2d", psinfo->pr_lwp.pr_nice);
14110Sstevel@tonic-gate 			else
14120Sstevel@tonic-gate 				(void) printf(" %2.2s",
14130Sstevel@tonic-gate 				    psinfo->pr_lwp.pr_clname);
14140Sstevel@tonic-gate 		}
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 	if (lflg) {
14170Sstevel@tonic-gate 		if (yflg) {
14180Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* RSS */
14190Sstevel@tonic-gate 				(void) printf("     0");
14200Sstevel@tonic-gate 			else if (psinfo->pr_rssize)
14210Sstevel@tonic-gate 				(void) printf(" %5lu",
14228233Sdme@sun.com 				    (ulong_t)psinfo->pr_rssize);
14230Sstevel@tonic-gate 			else
14240Sstevel@tonic-gate 				(void) printf("     ?");
14250Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
14260Sstevel@tonic-gate 				(void) printf("      0");
14270Sstevel@tonic-gate 			else if (psinfo->pr_size)
14280Sstevel@tonic-gate 				(void) printf(" %6lu",
14298233Sdme@sun.com 				    (ulong_t)psinfo->pr_size);
14300Sstevel@tonic-gate 			else
14310Sstevel@tonic-gate 				(void) printf("      ?");
14320Sstevel@tonic-gate 		} else {
14330Sstevel@tonic-gate #ifndef _LP64
14340Sstevel@tonic-gate 			if (psinfo->pr_addr)			/* ADDR */
14350Sstevel@tonic-gate 				(void) printf(" %8lx",
14368233Sdme@sun.com 				    (ulong_t)psinfo->pr_addr);
14370Sstevel@tonic-gate 			else
14380Sstevel@tonic-gate #endif
14390Sstevel@tonic-gate 				(void) printf("        ?");
14400Sstevel@tonic-gate 			if (psinfo->pr_flag & SSYS)		/* SZ */
14410Sstevel@tonic-gate 				(void) printf("      0");
14420Sstevel@tonic-gate 			else if (psinfo->pr_size)
14430Sstevel@tonic-gate 				(void) printf(" %6lu",
14440Sstevel@tonic-gate 				    (ulong_t)psinfo->pr_size / kbytes_per_page);
14450Sstevel@tonic-gate 			else
14460Sstevel@tonic-gate 				(void) printf("      ?");
14470Sstevel@tonic-gate 		}
14480Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_sname != 'S')		/* WCHAN */
14490Sstevel@tonic-gate 			(void) printf("         ");
14500Sstevel@tonic-gate #ifndef _LP64
14510Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_wchan)
14520Sstevel@tonic-gate 			(void) printf(" %8lx",
14538233Sdme@sun.com 			    (ulong_t)psinfo->pr_lwp.pr_wchan);
14540Sstevel@tonic-gate #endif
14550Sstevel@tonic-gate 		else
14560Sstevel@tonic-gate 			(void) printf("        ?");
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 	if (fflg) {						/* STIME */
14592966Ssayama 		int width = fname[F_STIME].width;
14600Sstevel@tonic-gate 		if (Lflg)
14612966Ssayama 			prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
14620Sstevel@tonic-gate 		else
14632966Ssayama 			prtime(psinfo->pr_start, width + 1, 1);
14640Sstevel@tonic-gate 	}
14652685Sakolb 
14662685Sakolb 	if (Hflg) {
14672685Sakolb 		/* Display home lgroup */
14682685Sakolb 		(void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
14692685Sakolb 	}
14702685Sakolb 
14710Sstevel@tonic-gate 	(void) printf(" %-8.14s", ttyp);			/* TTY */
14720Sstevel@tonic-gate 	if (Lflg) {
14730Sstevel@tonic-gate 		tm = psinfo->pr_lwp.pr_time.tv_sec;
14740Sstevel@tonic-gate 		if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
14750Sstevel@tonic-gate 			tm++;
14760Sstevel@tonic-gate 	} else {
14770Sstevel@tonic-gate 		tm = psinfo->pr_time.tv_sec;
14780Sstevel@tonic-gate 		if (psinfo->pr_time.tv_nsec > 500000000)
14790Sstevel@tonic-gate 			tm++;
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);		/* [L]TIME */
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	if (zombie_lwp) {
14840Sstevel@tonic-gate 		(void) printf(" <defunct>\n");
14850Sstevel@tonic-gate 		return;
14860Sstevel@tonic-gate 	}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	if (!fflg) {						/* CMD */
14890Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, 8);
14900Sstevel@tonic-gate 		(void) printf(" %.*s\n", wcnt, psinfo->pr_fname);
14910Sstevel@tonic-gate 		return;
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 
14942685Sakolb 
14950Sstevel@tonic-gate 	/*
14960Sstevel@tonic-gate 	 * PRARGSZ == length of cmd arg string.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate 	psinfo->pr_psargs[PRARGSZ-1] = '\0';
14990Sstevel@tonic-gate 	bytesleft = PRARGSZ;
15000Sstevel@tonic-gate 	for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
15010Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
15020Sstevel@tonic-gate 		if (length == 0)
15030Sstevel@tonic-gate 			break;
15040Sstevel@tonic-gate 		if (length < 0 || !iswprint(wchar)) {
15050Sstevel@tonic-gate 			if (length < 0)
15060Sstevel@tonic-gate 				length = 1;
15070Sstevel@tonic-gate 			if (bytesleft <= length) {
15080Sstevel@tonic-gate 				*cp = '\0';
15090Sstevel@tonic-gate 				break;
15100Sstevel@tonic-gate 			}
15110Sstevel@tonic-gate 			/* omit the unprintable character */
15120Sstevel@tonic-gate 			(void) memmove(cp, cp+length, bytesleft-length);
15130Sstevel@tonic-gate 			length = 0;
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 		bytesleft -= length;
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate 	wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ);
15180Sstevel@tonic-gate 	(void) printf(" %.*s\n", wcnt, psinfo->pr_psargs);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate /*
15220Sstevel@tonic-gate  * Print percent from 16-bit binary fraction [0 .. 1]
15230Sstevel@tonic-gate  * Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate static void
prtpct(ushort_t pct,int width)15260Sstevel@tonic-gate prtpct(ushort_t pct, int width)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	uint_t value = pct;	/* need 32 bits to compute with */
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	value = ((value * 1000) + 0x7000) >> 15;	/* [0 .. 1000] */
15310Sstevel@tonic-gate 	if (value >= 1000)
15320Sstevel@tonic-gate 		value = 999;
15330Sstevel@tonic-gate 	if ((width -= 2) < 2)
15340Sstevel@tonic-gate 		width = 2;
15350Sstevel@tonic-gate 	(void) printf("%*u.%u", width, value / 10, value % 10);
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate static void
print_time(time_t tim,int width)15390Sstevel@tonic-gate print_time(time_t tim, int width)
15400Sstevel@tonic-gate {
15410Sstevel@tonic-gate 	char buf[30];
15420Sstevel@tonic-gate 	time_t seconds;
15430Sstevel@tonic-gate 	time_t minutes;
15440Sstevel@tonic-gate 	time_t hours;
15450Sstevel@tonic-gate 	time_t days;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	if (tim < 0) {
15480Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
15490Sstevel@tonic-gate 		return;
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	seconds = tim % 60;
15530Sstevel@tonic-gate 	tim /= 60;
15540Sstevel@tonic-gate 	minutes = tim % 60;
15550Sstevel@tonic-gate 	tim /= 60;
15560Sstevel@tonic-gate 	hours = tim % 24;
15570Sstevel@tonic-gate 	days = tim / 24;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	if (days > 0) {
15600Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
15610Sstevel@tonic-gate 		    days, hours, minutes, seconds);
15620Sstevel@tonic-gate 	} else if (hours > 0) {
15630Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
15640Sstevel@tonic-gate 		    hours, minutes, seconds);
15650Sstevel@tonic-gate 	} else {
15660Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
15670Sstevel@tonic-gate 		    minutes, seconds);
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	(void) printf("%*s", width, buf);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate static void
print_field(psinfo_t * psinfo,struct field * f,const char * ttyp)15740Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
15750Sstevel@tonic-gate {
15760Sstevel@tonic-gate 	int width = f->width;
15770Sstevel@tonic-gate 	struct passwd *pwd;
15780Sstevel@tonic-gate 	struct group *grp;
15790Sstevel@tonic-gate 	time_t cputime;
15800Sstevel@tonic-gate 	int bytesleft;
15810Sstevel@tonic-gate 	int wcnt;
15820Sstevel@tonic-gate 	wchar_t	wchar;
15830Sstevel@tonic-gate 	char *cp;
15840Sstevel@tonic-gate 	int length;
15850Sstevel@tonic-gate 	ulong_t mask;
15860Sstevel@tonic-gate 	char c, *csave;
15870Sstevel@tonic-gate 	int zombie_lwp;
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	switch (f->fname) {
15920Sstevel@tonic-gate 	case F_RUSER:
15930Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_uid)) != NULL)
15940Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
15950Sstevel@tonic-gate 		else
15964321Scasper 			(void) printf("%*u", width, psinfo->pr_uid);
15970Sstevel@tonic-gate 		break;
15980Sstevel@tonic-gate 	case F_USER:
15990Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
16000Sstevel@tonic-gate 			(void) printf("%*s", width, pwd->pw_name);
16010Sstevel@tonic-gate 		else
16024321Scasper 			(void) printf("%*u", width, psinfo->pr_euid);
16030Sstevel@tonic-gate 		break;
16040Sstevel@tonic-gate 	case F_RGROUP:
16050Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
16060Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
16070Sstevel@tonic-gate 		else
16084321Scasper 			(void) printf("%*u", width, psinfo->pr_gid);
16090Sstevel@tonic-gate 		break;
16100Sstevel@tonic-gate 	case F_GROUP:
16110Sstevel@tonic-gate 		if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
16120Sstevel@tonic-gate 			(void) printf("%*s", width, grp->gr_name);
16130Sstevel@tonic-gate 		else
16144321Scasper 			(void) printf("%*u", width, psinfo->pr_egid);
16150Sstevel@tonic-gate 		break;
16160Sstevel@tonic-gate 	case F_RUID:
16174321Scasper 		(void) printf("%*u", width, psinfo->pr_uid);
16180Sstevel@tonic-gate 		break;
16190Sstevel@tonic-gate 	case F_UID:
16204321Scasper 		(void) printf("%*u", width, psinfo->pr_euid);
16210Sstevel@tonic-gate 		break;
16220Sstevel@tonic-gate 	case F_RGID:
16234321Scasper 		(void) printf("%*u", width, psinfo->pr_gid);
16240Sstevel@tonic-gate 		break;
16250Sstevel@tonic-gate 	case F_GID:
16264321Scasper 		(void) printf("%*u", width, psinfo->pr_egid);
16270Sstevel@tonic-gate 		break;
16280Sstevel@tonic-gate 	case F_PID:
16290Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pid);
16300Sstevel@tonic-gate 		break;
16310Sstevel@tonic-gate 	case F_PPID:
16320Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_ppid);
16330Sstevel@tonic-gate 		break;
16340Sstevel@tonic-gate 	case F_PGID:
16350Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_pgid);
16360Sstevel@tonic-gate 		break;
16370Sstevel@tonic-gate 	case F_SID:
16380Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_sid);
16390Sstevel@tonic-gate 		break;
16400Sstevel@tonic-gate 	case F_PSR:
16410Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
16420Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16430Sstevel@tonic-gate 		else
16440Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
16450Sstevel@tonic-gate 		break;
16460Sstevel@tonic-gate 	case F_LWP:
16470Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
16480Sstevel@tonic-gate 		break;
16490Sstevel@tonic-gate 	case F_NLWP:
16500Sstevel@tonic-gate 		(void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
16510Sstevel@tonic-gate 		break;
16520Sstevel@tonic-gate 	case F_OPRI:
16530Sstevel@tonic-gate 		if (zombie_lwp)
16540Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16550Sstevel@tonic-gate 		else
16560Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
16570Sstevel@tonic-gate 		break;
16580Sstevel@tonic-gate 	case F_PRI:
16590Sstevel@tonic-gate 		if (zombie_lwp)
16600Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16610Sstevel@tonic-gate 		else
16620Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
16630Sstevel@tonic-gate 		break;
16640Sstevel@tonic-gate 	case F_F:
16650Sstevel@tonic-gate 		mask = 0xffffffffUL;
16660Sstevel@tonic-gate 		if (width < 8)
16670Sstevel@tonic-gate 			mask >>= (8 - width) * 4;
16680Sstevel@tonic-gate 		(void) printf("%*lx", width, psinfo->pr_flag & mask);
16690Sstevel@tonic-gate 		break;
16700Sstevel@tonic-gate 	case F_S:
16710Sstevel@tonic-gate 		(void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
16720Sstevel@tonic-gate 		break;
16730Sstevel@tonic-gate 	case F_C:
16740Sstevel@tonic-gate 		if (zombie_lwp)
16750Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16760Sstevel@tonic-gate 		else
16770Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
16780Sstevel@tonic-gate 		break;
16790Sstevel@tonic-gate 	case F_PCPU:
16800Sstevel@tonic-gate 		if (zombie_lwp)
16810Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
16820Sstevel@tonic-gate 		else if (Lflg)
16830Sstevel@tonic-gate 			prtpct(psinfo->pr_lwp.pr_pctcpu, width);
16840Sstevel@tonic-gate 		else
16850Sstevel@tonic-gate 			prtpct(psinfo->pr_pctcpu, width);
16860Sstevel@tonic-gate 		break;
16870Sstevel@tonic-gate 	case F_PMEM:
16880Sstevel@tonic-gate 		prtpct(psinfo->pr_pctmem, width);
16890Sstevel@tonic-gate 		break;
16900Sstevel@tonic-gate 	case F_OSZ:
16910Sstevel@tonic-gate 		(void) printf("%*lu", width,
16928233Sdme@sun.com 		    (ulong_t)psinfo->pr_size / kbytes_per_page);
16930Sstevel@tonic-gate 		break;
16940Sstevel@tonic-gate 	case F_VSZ:
16950Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
16960Sstevel@tonic-gate 		break;
16970Sstevel@tonic-gate 	case F_RSS:
16980Sstevel@tonic-gate 		(void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
16990Sstevel@tonic-gate 		break;
17000Sstevel@tonic-gate 	case F_NICE:
17010Sstevel@tonic-gate 		/* if pr_oldpri is zero, then this class has no nice */
17020Sstevel@tonic-gate 		if (zombie_lwp)
17030Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
17040Sstevel@tonic-gate 		else if (psinfo->pr_lwp.pr_oldpri != 0)
17050Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
17060Sstevel@tonic-gate 		else
17070Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
17088233Sdme@sun.com 			    psinfo->pr_lwp.pr_clname);
17090Sstevel@tonic-gate 		break;
17100Sstevel@tonic-gate 	case F_CLASS:
17110Sstevel@tonic-gate 		if (zombie_lwp)
17120Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
17130Sstevel@tonic-gate 		else
17140Sstevel@tonic-gate 			(void) printf("%*.*s", width, width,
17158233Sdme@sun.com 			    psinfo->pr_lwp.pr_clname);
17160Sstevel@tonic-gate 		break;
17170Sstevel@tonic-gate 	case F_STIME:
17180Sstevel@tonic-gate 		if (Lflg)
17190Sstevel@tonic-gate 			prtime(psinfo->pr_lwp.pr_start, width, 0);
17200Sstevel@tonic-gate 		else
17210Sstevel@tonic-gate 			prtime(psinfo->pr_start, width, 0);
17220Sstevel@tonic-gate 		break;
17230Sstevel@tonic-gate 	case F_ETIME:
17240Sstevel@tonic-gate 		if (Lflg)
17250Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_lwp.pr_start),
17268233Sdme@sun.com 			    width);
17270Sstevel@tonic-gate 		else
17280Sstevel@tonic-gate 			print_time(delta_secs(&psinfo->pr_start), width);
17290Sstevel@tonic-gate 		break;
17300Sstevel@tonic-gate 	case F_TIME:
17310Sstevel@tonic-gate 		if (Lflg) {
17320Sstevel@tonic-gate 			cputime = psinfo->pr_lwp.pr_time.tv_sec;
17330Sstevel@tonic-gate 			if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
17340Sstevel@tonic-gate 				cputime++;
17350Sstevel@tonic-gate 		} else {
17360Sstevel@tonic-gate 			cputime = psinfo->pr_time.tv_sec;
17370Sstevel@tonic-gate 			if (psinfo->pr_time.tv_nsec > 500000000)
17380Sstevel@tonic-gate 				cputime++;
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 		print_time(cputime, width);
17410Sstevel@tonic-gate 		break;
17420Sstevel@tonic-gate 	case F_TTY:
17430Sstevel@tonic-gate 		(void) printf("%-*s", width, ttyp);
17440Sstevel@tonic-gate 		break;
17450Sstevel@tonic-gate 	case F_ADDR:
17460Sstevel@tonic-gate 		if (zombie_lwp)
17470Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
17480Sstevel@tonic-gate 		else if (Lflg)
17490Sstevel@tonic-gate 			(void) printf("%*lx", width,
17508233Sdme@sun.com 			    (long)psinfo->pr_lwp.pr_addr);
17510Sstevel@tonic-gate 		else
17520Sstevel@tonic-gate 			(void) printf("%*lx", width, (long)psinfo->pr_addr);
17530Sstevel@tonic-gate 		break;
17540Sstevel@tonic-gate 	case F_WCHAN:
17550Sstevel@tonic-gate 		if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
17560Sstevel@tonic-gate 			(void) printf("%*lx", width,
17578233Sdme@sun.com 			    (long)psinfo->pr_lwp.pr_wchan);
17580Sstevel@tonic-gate 		else
17590Sstevel@tonic-gate 			(void) printf("%*.*s", width, width, "-");
17600Sstevel@tonic-gate 		break;
17610Sstevel@tonic-gate 	case F_FNAME:
17620Sstevel@tonic-gate 		/*
17630Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
17640Sstevel@tonic-gate 		 */
17650Sstevel@tonic-gate 		if (zombie_lwp) {
17660Sstevel@tonic-gate 			if (f->next != NULL)
17670Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17680Sstevel@tonic-gate 			else
17690Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17700Sstevel@tonic-gate 			break;
17710Sstevel@tonic-gate 		}
17720Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_fname, 16, width);
17730Sstevel@tonic-gate 		if (f->next != NULL)
17740Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
17750Sstevel@tonic-gate 		else
17760Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, psinfo->pr_fname);
17770Sstevel@tonic-gate 		break;
17780Sstevel@tonic-gate 	case F_COMM:
17790Sstevel@tonic-gate 		if (zombie_lwp) {
17800Sstevel@tonic-gate 			if (f->next != NULL)
17810Sstevel@tonic-gate 				(void) printf("%-*s", width, "<defunct>");
17820Sstevel@tonic-gate 			else
17830Sstevel@tonic-gate 				(void) printf("%s", "<defunct>");
17840Sstevel@tonic-gate 			break;
17850Sstevel@tonic-gate 		}
17860Sstevel@tonic-gate 		csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n");
17870Sstevel@tonic-gate 		if (csave) {
17880Sstevel@tonic-gate 			c = *csave;
17890Sstevel@tonic-gate 			*csave = '\0';
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 		/* FALLTHROUGH */
17920Sstevel@tonic-gate 	case F_ARGS:
17930Sstevel@tonic-gate 		/*
17940Sstevel@tonic-gate 		 * PRARGSZ == length of cmd arg string.
17950Sstevel@tonic-gate 		 */
17960Sstevel@tonic-gate 		if (zombie_lwp) {
17970Sstevel@tonic-gate 			(void) printf("%-*s", width, "<defunct>");
17980Sstevel@tonic-gate 			break;
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 		psinfo->pr_psargs[PRARGSZ-1] = '\0';
18010Sstevel@tonic-gate 		bytesleft = PRARGSZ;
18020Sstevel@tonic-gate 		for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
18030Sstevel@tonic-gate 			length = mbtowc(&wchar, cp, MB_LEN_MAX);
18040Sstevel@tonic-gate 			if (length == 0)
18050Sstevel@tonic-gate 				break;
18060Sstevel@tonic-gate 			if (length < 0 || !iswprint(wchar)) {
18070Sstevel@tonic-gate 				if (length < 0)
18080Sstevel@tonic-gate 					length = 1;
18090Sstevel@tonic-gate 				if (bytesleft <= length) {
18100Sstevel@tonic-gate 					*cp = '\0';
18110Sstevel@tonic-gate 					break;
18120Sstevel@tonic-gate 				}
18130Sstevel@tonic-gate 				/* omit the unprintable character */
18140Sstevel@tonic-gate 				(void) memmove(cp, cp+length, bytesleft-length);
18150Sstevel@tonic-gate 				length = 0;
18160Sstevel@tonic-gate 			}
18170Sstevel@tonic-gate 			bytesleft -= length;
18180Sstevel@tonic-gate 		}
18190Sstevel@tonic-gate 		wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width);
18200Sstevel@tonic-gate 		/*
18210Sstevel@tonic-gate 		 * Print full width unless this is the last format.
18220Sstevel@tonic-gate 		 */
18230Sstevel@tonic-gate 		if (f->next != NULL)
18240Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt,
18250Sstevel@tonic-gate 			    psinfo->pr_psargs);
18260Sstevel@tonic-gate 		else
18270Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt,
18280Sstevel@tonic-gate 			    psinfo->pr_psargs);
18290Sstevel@tonic-gate 		if (f->fname == F_COMM && csave)
18300Sstevel@tonic-gate 			*csave = c;
18310Sstevel@tonic-gate 		break;
18320Sstevel@tonic-gate 	case F_TASKID:
18330Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_taskid);
18340Sstevel@tonic-gate 		break;
18350Sstevel@tonic-gate 	case F_PROJID:
18360Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_projid);
18370Sstevel@tonic-gate 		break;
18380Sstevel@tonic-gate 	case F_PROJECT:
18390Sstevel@tonic-gate 		{
18400Sstevel@tonic-gate 			struct project cproj;
18410Sstevel@tonic-gate 			char proj_buf[PROJECT_BUFSZ];
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 			if ((getprojbyid(psinfo->pr_projid, &cproj,
18440Sstevel@tonic-gate 			    (void *)&proj_buf, PROJECT_BUFSZ)) == NULL)
18450Sstevel@tonic-gate 				(void) printf("%*d", width,
18460Sstevel@tonic-gate 				    (int)psinfo->pr_projid);
18470Sstevel@tonic-gate 			else
18480Sstevel@tonic-gate 				(void) printf("%*s", width,
18490Sstevel@tonic-gate 				    (cproj.pj_name != NULL) ?
18500Sstevel@tonic-gate 				    cproj.pj_name : "---");
18510Sstevel@tonic-gate 		}
18520Sstevel@tonic-gate 		break;
18530Sstevel@tonic-gate 	case F_PSET:
18540Sstevel@tonic-gate 		if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
18550Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18560Sstevel@tonic-gate 		else
18570Sstevel@tonic-gate 			(void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
18580Sstevel@tonic-gate 		break;
18590Sstevel@tonic-gate 	case F_ZONEID:
18600Sstevel@tonic-gate 		(void) printf("%*d", width, (int)psinfo->pr_zoneid);
18610Sstevel@tonic-gate 		break;
18620Sstevel@tonic-gate 	case F_ZONE:
18630Sstevel@tonic-gate 		{
18640Sstevel@tonic-gate 			char zonename[ZONENAME_MAX];
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 			if (getzonenamebyid(psinfo->pr_zoneid, zonename,
18670Sstevel@tonic-gate 			    sizeof (zonename)) < 0) {
18680Sstevel@tonic-gate 				(void) printf("%*d", width,
18690Sstevel@tonic-gate 				    ((int)psinfo->pr_zoneid));
18700Sstevel@tonic-gate 			} else {
18710Sstevel@tonic-gate 				(void) printf("%*s", width, zonename);
18720Sstevel@tonic-gate 			}
18730Sstevel@tonic-gate 		}
18740Sstevel@tonic-gate 		break;
18750Sstevel@tonic-gate 	case F_CTID:
18760Sstevel@tonic-gate 		if (psinfo->pr_contract == -1)
18770Sstevel@tonic-gate 			(void) printf("%*s", width, "-");
18780Sstevel@tonic-gate 		else
18790Sstevel@tonic-gate 			(void) printf("%*ld", width, (long)psinfo->pr_contract);
18800Sstevel@tonic-gate 		break;
18812685Sakolb 	case F_LGRP:
18822685Sakolb 		/* Display home lgroup */
18832685Sakolb 		(void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
18842685Sakolb 		break;
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate static void
print_zombie_field(psinfo_t * psinfo,struct field * f,const char * ttyp)18890Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
18900Sstevel@tonic-gate {
18910Sstevel@tonic-gate 	int wcnt;
18920Sstevel@tonic-gate 	int width = f->width;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	switch (f->fname) {
18950Sstevel@tonic-gate 	case F_FNAME:
18960Sstevel@tonic-gate 	case F_COMM:
18970Sstevel@tonic-gate 	case F_ARGS:
18980Sstevel@tonic-gate 		/*
18990Sstevel@tonic-gate 		 * Print full width unless this is the last output format.
19000Sstevel@tonic-gate 		 */
19010Sstevel@tonic-gate 		wcnt = min(width, sizeof ("<defunct>"));
19020Sstevel@tonic-gate 		if (f->next != NULL)
19030Sstevel@tonic-gate 			(void) printf("%-*.*s", width, wcnt, "<defunct>");
19040Sstevel@tonic-gate 		else
19050Sstevel@tonic-gate 			(void) printf("%-.*s", wcnt, "<defunct>");
19060Sstevel@tonic-gate 		break;
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	case F_PSR:
19090Sstevel@tonic-gate 	case F_PCPU:
19100Sstevel@tonic-gate 	case F_PMEM:
19110Sstevel@tonic-gate 	case F_NICE:
19120Sstevel@tonic-gate 	case F_CLASS:
19130Sstevel@tonic-gate 	case F_STIME:
19140Sstevel@tonic-gate 	case F_ETIME:
19150Sstevel@tonic-gate 	case F_WCHAN:
19160Sstevel@tonic-gate 	case F_PSET:
19170Sstevel@tonic-gate 		(void) printf("%*s", width, "-");
19180Sstevel@tonic-gate 		break;
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	case F_OPRI:
19210Sstevel@tonic-gate 	case F_PRI:
19220Sstevel@tonic-gate 	case F_OSZ:
19230Sstevel@tonic-gate 	case F_VSZ:
19240Sstevel@tonic-gate 	case F_RSS:
19250Sstevel@tonic-gate 		(void) printf("%*d", width, 0);
19260Sstevel@tonic-gate 		break;
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	default:
19290Sstevel@tonic-gate 		print_field(psinfo, f, ttyp);
19300Sstevel@tonic-gate 		break;
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate static void
pr_fields(psinfo_t * psinfo,const char * ttyp,void (* print_fld)(psinfo_t *,struct field *,const char *))19350Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp,
19360Sstevel@tonic-gate 	void (*print_fld)(psinfo_t *, struct field *, const char *))
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	struct field *f;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	for (f = fields; f != NULL; f = f->next) {
19410Sstevel@tonic-gate 		print_fld(psinfo, f, ttyp);
19420Sstevel@tonic-gate 		if (f->next != NULL)
19430Sstevel@tonic-gate 			(void) printf(" ");
19440Sstevel@tonic-gate 	}
19450Sstevel@tonic-gate 	(void) printf("\n");
19460Sstevel@tonic-gate }
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate /*
19490Sstevel@tonic-gate  * Returns 1 if arg is found in array arr, of length num; 0 otherwise.
19500Sstevel@tonic-gate  */
19510Sstevel@tonic-gate static int
search(pid_t * arr,int number,pid_t arg)19520Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg)
19530Sstevel@tonic-gate {
19540Sstevel@tonic-gate 	int i;
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	for (i = 0; i < number; i++)
19570Sstevel@tonic-gate 		if (arg == arr[i])
19580Sstevel@tonic-gate 			return (1);
19590Sstevel@tonic-gate 	return (0);
19600Sstevel@tonic-gate }
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate /*
19630Sstevel@tonic-gate  * Add an entry (user, group) to the specified table.
19640Sstevel@tonic-gate  */
19650Sstevel@tonic-gate static void
add_ugentry(struct ughead * tbl,char * name)19660Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name)
19670Sstevel@tonic-gate {
19680Sstevel@tonic-gate 	struct ugdata *entp;
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	if (tbl->size == tbl->nent) {	/* reallocate the table entries */
19710Sstevel@tonic-gate 		if ((tbl->size *= 2) == 0)
19720Sstevel@tonic-gate 			tbl->size = 32;		/* first time */
19730Sstevel@tonic-gate 		tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 	entp = &tbl->ent[tbl->nent++];
19760Sstevel@tonic-gate 	entp->id = 0;
19770Sstevel@tonic-gate 	(void) strncpy(entp->name, name, MAXUGNAME);
19780Sstevel@tonic-gate 	entp->name[MAXUGNAME] = '\0';
19790Sstevel@tonic-gate }
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate static int
uconv(struct ughead * uhead)19820Sstevel@tonic-gate uconv(struct ughead *uhead)
19830Sstevel@tonic-gate {
19840Sstevel@tonic-gate 	struct ugdata *utbl = uhead->ent;
19850Sstevel@tonic-gate 	int n = uhead->nent;
19860Sstevel@tonic-gate 	struct passwd *pwd;
19870Sstevel@tonic-gate 	int i;
19880Sstevel@tonic-gate 	int fnd = 0;
19890Sstevel@tonic-gate 	uid_t uid;
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	/*
19920Sstevel@tonic-gate 	 * Ask the name service for names.
19930Sstevel@tonic-gate 	 */
19940Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
19950Sstevel@tonic-gate 		/*
19960Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
19970Sstevel@tonic-gate 		 */
19984321Scasper 		if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
19990Sstevel@tonic-gate 			pwd = getpwuid(uid);
20000Sstevel@tonic-gate 		else
20010Sstevel@tonic-gate 			pwd = getpwnam(utbl[i].name);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 		/*
20040Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
20050Sstevel@tonic-gate 		 */
20060Sstevel@tonic-gate 		if (pwd == NULL) {
20070Sstevel@tonic-gate 			(void) fprintf(stderr,
20080Sstevel@tonic-gate 			    gettext("ps: unknown user %s\n"), utbl[i].name);
20090Sstevel@tonic-gate 			continue;
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		utbl[fnd].id = pwd->pw_uid;
20130Sstevel@tonic-gate 		(void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
20140Sstevel@tonic-gate 		fnd++;
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	uhead->nent = fnd;	/* in case it changed */
20180Sstevel@tonic-gate 	return (n - fnd);
20190Sstevel@tonic-gate }
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate static int
gconv(struct ughead * ghead)20220Sstevel@tonic-gate gconv(struct ughead *ghead)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	struct ugdata *gtbl = ghead->ent;
20250Sstevel@tonic-gate 	int n = ghead->nent;
20260Sstevel@tonic-gate 	struct group *grp;
20270Sstevel@tonic-gate 	gid_t gid;
20280Sstevel@tonic-gate 	int i;
20290Sstevel@tonic-gate 	int fnd = 0;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	/*
20320Sstevel@tonic-gate 	 * Ask the name service for names.
20330Sstevel@tonic-gate 	 */
20340Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
20350Sstevel@tonic-gate 		/*
20360Sstevel@tonic-gate 		 * If name is numeric, ask for numeric id
20370Sstevel@tonic-gate 		 */
20384321Scasper 		if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
20390Sstevel@tonic-gate 			grp = getgrgid(gid);
20400Sstevel@tonic-gate 		else
20410Sstevel@tonic-gate 			grp = getgrnam(gtbl[i].name);
20420Sstevel@tonic-gate 		/*
20430Sstevel@tonic-gate 		 * If found, enter found index into tbl array.
20440Sstevel@tonic-gate 		 */
20450Sstevel@tonic-gate 		if (grp == NULL) {
20460Sstevel@tonic-gate 			(void) fprintf(stderr,
20470Sstevel@tonic-gate 			    gettext("ps: unknown group %s\n"), gtbl[i].name);
20480Sstevel@tonic-gate 			continue;
20490Sstevel@tonic-gate 		}
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 		gtbl[fnd].id = grp->gr_gid;
20520Sstevel@tonic-gate 		(void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
20530Sstevel@tonic-gate 		fnd++;
20540Sstevel@tonic-gate 	}
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	ghead->nent = fnd;	/* in case it changed */
20570Sstevel@tonic-gate 	return (n - fnd);
20580Sstevel@tonic-gate }
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate /*
20610Sstevel@tonic-gate  * Return 1 if puid is in table, otherwise 0.
20620Sstevel@tonic-gate  */
20630Sstevel@tonic-gate static int
ugfind(id_t id,struct ughead * ughead)20640Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead)
20650Sstevel@tonic-gate {
20660Sstevel@tonic-gate 	struct ugdata *utbl = ughead->ent;
20670Sstevel@tonic-gate 	int n = ughead->nent;
20680Sstevel@tonic-gate 	int i;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	for (i = 0; i < n; i++)
20710Sstevel@tonic-gate 		if (utbl[i].id == id)
20720Sstevel@tonic-gate 			return (1);
20730Sstevel@tonic-gate 	return (0);
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate /*
20770Sstevel@tonic-gate  * Print starting time of process unless process started more than 24 hours
20780Sstevel@tonic-gate  * ago, in which case the date is printed.  The date is printed in the form
20790Sstevel@tonic-gate  * "MMM dd" if old format, else the blank is replaced with an '_' so
20800Sstevel@tonic-gate  * it appears as a single word (for parseability).
20810Sstevel@tonic-gate  */
20820Sstevel@tonic-gate static void
prtime(timestruc_t st,int width,int old)20830Sstevel@tonic-gate prtime(timestruc_t st, int width, int old)
20840Sstevel@tonic-gate {
20850Sstevel@tonic-gate 	char sttim[26];
20860Sstevel@tonic-gate 	time_t starttime;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	starttime = st.tv_sec;
20890Sstevel@tonic-gate 	if (st.tv_nsec > 500000000)
20900Sstevel@tonic-gate 		starttime++;
20910Sstevel@tonic-gate 	if ((now.tv_sec - starttime) >= 24*60*60) {
20922966Ssayama 		(void) strftime(sttim, sizeof (sttim), old?
20932966Ssayama 		/*
20942966Ssayama 		 * TRANSLATION_NOTE
20952966Ssayama 		 * This time format is used by STIME field when -f option
20962966Ssayama 		 * is specified.  Used for processes that begun more than
20972966Ssayama 		 * 24 hours.
20982966Ssayama 		 */
20992966Ssayama 		    dcgettext(NULL, "%b %d", LC_TIME) :
21002966Ssayama 		/*
21012966Ssayama 		 * TRANSLATION_NOTE
21022966Ssayama 		 * This time format is used by STIME field when -o option
21032966Ssayama 		 * is specified.  Used for processes that begun more than
21042966Ssayama 		 * 24 hours.
21052966Ssayama 		 */
21062966Ssayama 		    dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
21070Sstevel@tonic-gate 	} else {
21082966Ssayama 		/*
21092966Ssayama 		 * TRANSLATION_NOTE
21102966Ssayama 		 * This time format is used by STIME field when -f or -o option
21112966Ssayama 		 * is specified.  Used for processes that begun less than
21122966Ssayama 		 * 24 hours.
21132966Ssayama 		 */
21142966Ssayama 		(void) strftime(sttim, sizeof (sttim),
21152966Ssayama 		    dcgettext(NULL, "%H:%M:%S", LC_TIME),
21162966Ssayama 		    localtime(&starttime));
21170Sstevel@tonic-gate 	}
21180Sstevel@tonic-gate 	(void) printf("%*.*s", width, width, sttim);
21190Sstevel@tonic-gate }
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate static void
przom(psinfo_t * psinfo)21220Sstevel@tonic-gate przom(psinfo_t *psinfo)
21230Sstevel@tonic-gate {
21240Sstevel@tonic-gate 	long	tm;
21250Sstevel@tonic-gate 	struct passwd *pwd;
21260Sstevel@tonic-gate 	char zonename[ZONENAME_MAX];
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	/*
21290Sstevel@tonic-gate 	 * All fields before 'PID' are printed with a trailing space as a
21300Sstevel@tonic-gate 	 * spearator, rather than keeping track of which column is first.  All
21310Sstevel@tonic-gate 	 * other fields are printed with a leading space.
21320Sstevel@tonic-gate 	 */
21330Sstevel@tonic-gate 	if (lflg) {	/* F S */
21340Sstevel@tonic-gate 		if (!yflg)
21350Sstevel@tonic-gate 			(void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
21360Sstevel@tonic-gate 		(void) printf("%c ", psinfo->pr_lwp.pr_sname);	/* S */
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 	if (Zflg) {
21390Sstevel@tonic-gate 		if (getzonenamebyid(psinfo->pr_zoneid, zonename,
21400Sstevel@tonic-gate 		    sizeof (zonename)) < 0) {
2141*8822SCasper.Dik@Sun.COM 			(void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
21420Sstevel@tonic-gate 		} else {
21430Sstevel@tonic-gate 			(void) printf("%8.8s ", zonename);
21440Sstevel@tonic-gate 		}
21450Sstevel@tonic-gate 	}
21462685Sakolb 	if (Hflg) {
21472685Sakolb 		/* Display home lgroup */
21482685Sakolb 		(void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
21492685Sakolb 	}
21500Sstevel@tonic-gate 	if (fflg) {
21510Sstevel@tonic-gate 		if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
21520Sstevel@tonic-gate 			(void) printf("%8.8s ", pwd->pw_name);
21530Sstevel@tonic-gate 		else
21548233Sdme@sun.com 			(void) printf(" %7.7u ", psinfo->pr_euid);
21550Sstevel@tonic-gate 	} else if (lflg)
21564321Scasper 		(void) printf("%6u ", psinfo->pr_euid);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	(void) printf("%*d", pidwidth, (int)psinfo->pr_pid);	/* PID */
21590Sstevel@tonic-gate 	if (lflg || fflg)
21600Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21610Sstevel@tonic-gate 		    (int)psinfo->pr_ppid);			/* PPID */
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if (jflg) {
21640Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21650Sstevel@tonic-gate 		    (int)psinfo->pr_pgid);			/* PGID */
21660Sstevel@tonic-gate 		(void) printf(" %*d", pidwidth,
21670Sstevel@tonic-gate 		    (int)psinfo->pr_sid);			/* SID  */
21680Sstevel@tonic-gate 	}
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	if (Lflg)
21710Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* LWP */
21720Sstevel@tonic-gate 	if (Pflg)
21730Sstevel@tonic-gate 		(void) printf("   -");				/* PSR */
21740Sstevel@tonic-gate 	if (Lflg && fflg)
21750Sstevel@tonic-gate 		(void) printf(" %5d", 0);			/* NLWP */
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	if (cflg) {
21780Sstevel@tonic-gate 		(void) printf(" %4s", "-");	/* zombies have no class */
21790Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_pri);	/* PRI	*/
21800Sstevel@tonic-gate 	} else if (lflg || fflg) {
21810Sstevel@tonic-gate 		(void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C   */
21820Sstevel@tonic-gate 		if (lflg)
21830Sstevel@tonic-gate 			(void) printf(" %3d %2s",
21840Sstevel@tonic-gate 			    psinfo->pr_lwp.pr_oldpri, "-");	/* PRI NI */
21850Sstevel@tonic-gate 	}
21860Sstevel@tonic-gate 	if (lflg) {
21870Sstevel@tonic-gate 		if (yflg)				/* RSS SZ WCHAN */
21880Sstevel@tonic-gate 			(void) printf(" %5d %6d %8s", 0, 0, "-");
21890Sstevel@tonic-gate 		else					/* ADDR SZ WCHAN */
21900Sstevel@tonic-gate 			(void) printf(" %8s %6d %8s", "-", 0, "-");
21910Sstevel@tonic-gate 	}
21922966Ssayama 	if (fflg) {
21932966Ssayama 		int width = fname[F_STIME].width;
21942966Ssayama 		(void) printf(" %*.*s", width, width, "-"); 	/* STIME */
21952966Ssayama 	}
21960Sstevel@tonic-gate 	(void) printf(" %-8.14s", "?");				/* TTY */
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	tm = psinfo->pr_time.tv_sec;
21990Sstevel@tonic-gate 	if (psinfo->pr_time.tv_nsec > 500000000)
22000Sstevel@tonic-gate 		tm++;
22010Sstevel@tonic-gate 	(void) printf(" %4ld:%.2ld", tm / 60, tm % 60);	/* TIME */
22020Sstevel@tonic-gate 	(void) printf(" <defunct>\n");
22030Sstevel@tonic-gate }
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate /*
22060Sstevel@tonic-gate  * Function to compute the number of printable bytes in a multibyte
22070Sstevel@tonic-gate  * command string ("internationalization").
22080Sstevel@tonic-gate  */
22090Sstevel@tonic-gate static int
namencnt(char * cmd,int csisize,int scrsize)22100Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize)
22110Sstevel@tonic-gate {
22120Sstevel@tonic-gate 	int csiwcnt = 0, scrwcnt = 0;
22130Sstevel@tonic-gate 	int ncsisz, nscrsz;
22140Sstevel@tonic-gate 	wchar_t  wchar;
22150Sstevel@tonic-gate 	int	 len;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	while (*cmd != '\0') {
22180Sstevel@tonic-gate 		if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
22190Sstevel@tonic-gate 			len = MB_CUR_MAX;
22200Sstevel@tonic-gate 		if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
22210Sstevel@tonic-gate 			return (8); /* default to use for illegal chars */
22220Sstevel@tonic-gate 		if ((nscrsz = wcwidth(wchar)) <= 0)
22230Sstevel@tonic-gate 			return (8);
22240Sstevel@tonic-gate 		if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize)
22250Sstevel@tonic-gate 			break;
22260Sstevel@tonic-gate 		csiwcnt += ncsisz;
22270Sstevel@tonic-gate 		scrwcnt += nscrsz;
22280Sstevel@tonic-gate 		cmd += ncsisz;
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 	return (csiwcnt);
22310Sstevel@tonic-gate }
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate static char *
err_string(int err)22340Sstevel@tonic-gate err_string(int err)
22350Sstevel@tonic-gate {
22360Sstevel@tonic-gate 	static char buf[32];
22370Sstevel@tonic-gate 	char *str = strerror(err);
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 	if (str == NULL)
22400Sstevel@tonic-gate 		(void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	return (str);
22430Sstevel@tonic-gate }
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate /* If allocation fails, die */
22460Sstevel@tonic-gate static void *
Realloc(void * ptr,size_t size)22470Sstevel@tonic-gate Realloc(void *ptr, size_t size)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	ptr = realloc(ptr, size);
22500Sstevel@tonic-gate 	if (ptr == NULL) {
22510Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("ps: no memory\n"));
22520Sstevel@tonic-gate 		exit(1);
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 	return (ptr);
22550Sstevel@tonic-gate }
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate static time_t
delta_secs(const timestruc_t * start)22580Sstevel@tonic-gate delta_secs(const timestruc_t *start)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	time_t seconds = now.tv_sec - start->tv_sec;
22610Sstevel@tonic-gate 	long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	if (nanosecs >= (NANOSEC / 2))
22640Sstevel@tonic-gate 		seconds++;
22650Sstevel@tonic-gate 	else if (nanosecs < -(NANOSEC / 2))
22660Sstevel@tonic-gate 		seconds--;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	return (seconds);
22690Sstevel@tonic-gate }
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate /*
22720Sstevel@tonic-gate  * Returns the following:
22730Sstevel@tonic-gate  *
22740Sstevel@tonic-gate  * 	0	No error
22750Sstevel@tonic-gate  * 	EINVAL	Invalid number
22760Sstevel@tonic-gate  * 	ERANGE	Value exceeds (min, max) range
22770Sstevel@tonic-gate  */
22780Sstevel@tonic-gate static int
str2id(const char * p,pid_t * val,long min,long max)22790Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate 	char *q;
22820Sstevel@tonic-gate 	long number;
22830Sstevel@tonic-gate 	int error;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	errno = 0;
22860Sstevel@tonic-gate 	number = strtol(p, &q, 10);
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	if (errno != 0 || q == p || *q != '\0') {
22890Sstevel@tonic-gate 		if ((error = errno) == 0) {
22900Sstevel@tonic-gate 			/*
22910Sstevel@tonic-gate 			 * strtol() can fail without setting errno, or it can
22920Sstevel@tonic-gate 			 * set it to EINVAL or ERANGE.  In the case errno is
22930Sstevel@tonic-gate 			 * still zero, return EINVAL.
22940Sstevel@tonic-gate 			 */
22950Sstevel@tonic-gate 			error = EINVAL;
22960Sstevel@tonic-gate 		}
22970Sstevel@tonic-gate 	} else if (number < min || number > max) {
22980Sstevel@tonic-gate 		error = ERANGE;
22990Sstevel@tonic-gate 	} else {
23000Sstevel@tonic-gate 		error = 0;
23010Sstevel@tonic-gate 	}
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	*val = number;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	return (error);
23060Sstevel@tonic-gate }
23070Sstevel@tonic-gate 
23084321Scasper /*
23094321Scasper  * Returns the following:
23104321Scasper  *
23114321Scasper  * 	0	No error
23124321Scasper  * 	EINVAL	Invalid number
23134321Scasper  * 	ERANGE	Value exceeds (min, max) range
23144321Scasper  */
23154321Scasper static int
str2uid(const char * p,uid_t * val,unsigned long min,unsigned long max)23164321Scasper str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
23174321Scasper {
23184321Scasper 	char *q;
23194321Scasper 	unsigned long number;
23204321Scasper 	int error;
23214321Scasper 
23224321Scasper 	errno = 0;
23234321Scasper 	number = strtoul(p, &q, 10);
23244321Scasper 
23254321Scasper 	if (errno != 0 || q == p || *q != '\0') {
23264321Scasper 		if ((error = errno) == 0) {
23274321Scasper 			/*
23284321Scasper 			 * strtoul() can fail without setting errno, or it can
23294321Scasper 			 * set it to EINVAL or ERANGE.  In the case errno is
23304321Scasper 			 * still zero, return EINVAL.
23314321Scasper 			 */
23324321Scasper 			error = EINVAL;
23334321Scasper 		}
23344321Scasper 	} else if (number < min || number > max) {
23354321Scasper 		error = ERANGE;
23364321Scasper 	} else {
23374321Scasper 		error = 0;
23384321Scasper 	}
23394321Scasper 
23404321Scasper 	*val = number;
23414321Scasper 
23424321Scasper 	return (error);
23434321Scasper }
23444321Scasper 
23450Sstevel@tonic-gate static int
pidcmp(const void * p1,const void * p2)23460Sstevel@tonic-gate pidcmp(const void *p1, const void *p2)
23470Sstevel@tonic-gate {
23480Sstevel@tonic-gate 	pid_t i = *((pid_t *)p1);
23490Sstevel@tonic-gate 	pid_t j = *((pid_t *)p2);
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	return (i - j);
23520Sstevel@tonic-gate }
2353