xref: /illumos-gate/usr/src/cmd/w/w.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * This is the new w command which takes advantage of
44*7c478bd9Sstevel@tonic-gate  * the /proc interface to gain access to the information
45*7c478bd9Sstevel@tonic-gate  * of all the processes currently on the system.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * This program also implements 'uptime'.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * Maintenance note:
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  * Much of this code is replicated in whodo.c.  If you're
52*7c478bd9Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #include <stdio.h>
56*7c478bd9Sstevel@tonic-gate #include <string.h>
57*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
58*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
59*7c478bd9Sstevel@tonic-gate #include <ctype.h>
60*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
61*7c478bd9Sstevel@tonic-gate #include <time.h>
62*7c478bd9Sstevel@tonic-gate #include <errno.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
64*7c478bd9Sstevel@tonic-gate #include <utmpx.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
66*7c478bd9Sstevel@tonic-gate #include <dirent.h>
67*7c478bd9Sstevel@tonic-gate #include <procfs.h>		/* /proc header file */
68*7c478bd9Sstevel@tonic-gate #include <locale.h>
69*7c478bd9Sstevel@tonic-gate #include <unistd.h>
70*7c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
71*7c478bd9Sstevel@tonic-gate #include <limits.h>
72*7c478bd9Sstevel@tonic-gate #include <priv_utils.h>
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * utmpx defines wider fields for user and line.  For compatibility of output,
76*7c478bd9Sstevel@tonic-gate  * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
77*7c478bd9Sstevel@tonic-gate  * to use the full lengths.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate #ifndef UTMPX_NAMELEN
80*7c478bd9Sstevel@tonic-gate /* XXX - utmp - fix name length */
81*7c478bd9Sstevel@tonic-gate #define	NMAX		(_POSIX_LOGIN_NAME_MAX - 1)
82*7c478bd9Sstevel@tonic-gate #define	LMAX		12
83*7c478bd9Sstevel@tonic-gate #else	/* UTMPX_NAMELEN */
84*7c478bd9Sstevel@tonic-gate static struct utmpx dummy;
85*7c478bd9Sstevel@tonic-gate #define	NMAX		(sizeof (dummy.ut_user))
86*7c478bd9Sstevel@tonic-gate #define	LMAX		(sizeof (dummy.ut_line))
87*7c478bd9Sstevel@tonic-gate #endif /* UTMPX_NAMELEN */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)	/* x/60 rounded */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #ifdef ERR
92*7c478bd9Sstevel@tonic-gate #undef ERR
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate #define	ERR		(-1)
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	HSIZE		256		/* size of process hash table 	*/
97*7c478bd9Sstevel@tonic-gate #define	PROCDIR		"/proc"
98*7c478bd9Sstevel@tonic-gate #define	INITPROCESS	(pid_t)1	/* init process pid */
99*7c478bd9Sstevel@tonic-gate #define	NONE		'n'		/* no state */
100*7c478bd9Sstevel@tonic-gate #define	RUNNING		'r'		/* runnable process */
101*7c478bd9Sstevel@tonic-gate #define	ZOMBIE		'z'		/* zombie process */
102*7c478bd9Sstevel@tonic-gate #define	VISITED		'v'		/* marked node as visited */
103*7c478bd9Sstevel@tonic-gate #define	PRINTF(a)	if (printf a < 0) { \
104*7c478bd9Sstevel@tonic-gate 		perror((gettext("%s: printf failed"), prog)); \
105*7c478bd9Sstevel@tonic-gate 		exit(1); }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate struct uproc {
108*7c478bd9Sstevel@tonic-gate 	pid_t	p_upid;			/* process id */
109*7c478bd9Sstevel@tonic-gate 	char	p_state;		/* numeric value of process state */
110*7c478bd9Sstevel@tonic-gate 	dev_t   p_ttyd;			/* controlling tty of process */
111*7c478bd9Sstevel@tonic-gate 	time_t  p_time;			/* seconds of user & system time */
112*7c478bd9Sstevel@tonic-gate 	time_t	p_ctime;		/* seconds of child user & sys time */
113*7c478bd9Sstevel@tonic-gate 	int	p_igintr;		/* 1 = ignores SIGQUIT and SIGINT */
114*7c478bd9Sstevel@tonic-gate 	char    p_comm[PRARGSZ+1];	/* command */
115*7c478bd9Sstevel@tonic-gate 	char    p_args[PRARGSZ+1];	/* command line arguments */
116*7c478bd9Sstevel@tonic-gate 	struct uproc	*p_child,	/* first child pointer */
117*7c478bd9Sstevel@tonic-gate 			*p_sibling,	/* sibling pointer */
118*7c478bd9Sstevel@tonic-gate 			*p_pgrpl,	/* pgrp link */
119*7c478bd9Sstevel@tonic-gate 			*p_link;	/* hash table chain pointer */
120*7c478bd9Sstevel@tonic-gate };
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /*
123*7c478bd9Sstevel@tonic-gate  *  	define hash table for struct uproc
124*7c478bd9Sstevel@tonic-gate  *	Hash function uses process id
125*7c478bd9Sstevel@tonic-gate  *	and the size of the hash table(HSIZE)
126*7c478bd9Sstevel@tonic-gate  *	to determine process index into the table.
127*7c478bd9Sstevel@tonic-gate  */
128*7c478bd9Sstevel@tonic-gate static struct uproc	pr_htbl[HSIZE];
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate static struct 	uproc	*findhash(pid_t);
131*7c478bd9Sstevel@tonic-gate static time_t  	findidle(char *);
132*7c478bd9Sstevel@tonic-gate static void	clnarglist(char *);
133*7c478bd9Sstevel@tonic-gate static void	showtotals(struct uproc *);
134*7c478bd9Sstevel@tonic-gate static void	calctotals(struct uproc *);
135*7c478bd9Sstevel@tonic-gate static void	prttime(time_t, char *);
136*7c478bd9Sstevel@tonic-gate static void	prtat(time_t *time);
137*7c478bd9Sstevel@tonic-gate static void	checkampm(char *str);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate static char	*prog;		/* pointer to invocation name */
140*7c478bd9Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
141*7c478bd9Sstevel@tonic-gate static int	lflag = 1;	/* set if -l flag; 0 for -s flag: short form */
142*7c478bd9Sstevel@tonic-gate static char	*sel_user;	/* login of particular user selected */
143*7c478bd9Sstevel@tonic-gate static char 	firstchar;	/* first char of name of prog invoked as */
144*7c478bd9Sstevel@tonic-gate static int	login;		/* true if invoked as login shell */
145*7c478bd9Sstevel@tonic-gate static time_t	now;		/* current time of day */
146*7c478bd9Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
147*7c478bd9Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
148*7c478bd9Sstevel@tonic-gate static time_t	idle;		/* number of minutes user is idle */
149*7c478bd9Sstevel@tonic-gate static time_t	jobtime;	/* total cpu time visible */
150*7c478bd9Sstevel@tonic-gate static char	doing[520];	/* process attached to terminal */
151*7c478bd9Sstevel@tonic-gate static time_t	proctime;	/* cpu time of process in doing */
152*7c478bd9Sstevel@tonic-gate static pid_t	curpid, empty;
153*7c478bd9Sstevel@tonic-gate static int	add_times;	/* boolean: add the cpu times or not */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate #if SIGQUIT > SIGINT
156*7c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
157*7c478bd9Sstevel@tonic-gate #else
158*7c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGINT
159*7c478bd9Sstevel@tonic-gate #endif
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate int
162*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	struct utmpx	*ut;
165*7c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
166*7c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpend;
167*7c478bd9Sstevel@tonic-gate 	struct utmpx	*utp;
168*7c478bd9Sstevel@tonic-gate 	struct uproc	*up, *parent, *pgrp;
169*7c478bd9Sstevel@tonic-gate 	struct psinfo	info;
170*7c478bd9Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
171*7c478bd9Sstevel@tonic-gate 	struct pstatus	statinfo;
172*7c478bd9Sstevel@tonic-gate 	size_t		size;
173*7c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
174*7c478bd9Sstevel@tonic-gate 	DIR   		*dirp;
175*7c478bd9Sstevel@tonic-gate 	struct	dirent	*dp;
176*7c478bd9Sstevel@tonic-gate 	char		pname[64];
177*7c478bd9Sstevel@tonic-gate 	char 		*fname;
178*7c478bd9Sstevel@tonic-gate 	int		procfd;
179*7c478bd9Sstevel@tonic-gate 	char		*cp;
180*7c478bd9Sstevel@tonic-gate 	int		i;
181*7c478bd9Sstevel@tonic-gate 	int		days, hrs, mins;
182*7c478bd9Sstevel@tonic-gate 	int		entries;
183*7c478bd9Sstevel@tonic-gate 	double		loadavg[3];
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/*
186*7c478bd9Sstevel@tonic-gate 	 * This program needs the proc_owner privilege
187*7c478bd9Sstevel@tonic-gate 	 */
188*7c478bd9Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER,
189*7c478bd9Sstevel@tonic-gate 	    (char *)NULL);
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
192*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
193*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
194*7c478bd9Sstevel@tonic-gate #endif
195*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	login = (argv[0][0] == '-');
198*7c478bd9Sstevel@tonic-gate 	cp = strrchr(argv[0], '/');
199*7c478bd9Sstevel@tonic-gate 	firstchar = login ? argv[0][1] : (cp == 0) ? argv[0][0] : cp[1];
200*7c478bd9Sstevel@tonic-gate 	prog = argv[0];
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	while (argc > 1) {
203*7c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
204*7c478bd9Sstevel@tonic-gate 			for (i = 1; argv[1][i]; i++) {
205*7c478bd9Sstevel@tonic-gate 				switch (argv[1][i]) {
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 				case 'h':
208*7c478bd9Sstevel@tonic-gate 					header = 0;
209*7c478bd9Sstevel@tonic-gate 					break;
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 				case 'l':
212*7c478bd9Sstevel@tonic-gate 					lflag++;
213*7c478bd9Sstevel@tonic-gate 					break;
214*7c478bd9Sstevel@tonic-gate 				case 's':
215*7c478bd9Sstevel@tonic-gate 					lflag = 0;
216*7c478bd9Sstevel@tonic-gate 					break;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 				case 'u':
219*7c478bd9Sstevel@tonic-gate 				case 'w':
220*7c478bd9Sstevel@tonic-gate 					firstchar = argv[1][i];
221*7c478bd9Sstevel@tonic-gate 					break;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 				default:
224*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
225*7c478bd9Sstevel@tonic-gate 					    "%s: bad flag %s\n"),
226*7c478bd9Sstevel@tonic-gate 					    prog, argv[1]);
227*7c478bd9Sstevel@tonic-gate 					exit(1);
228*7c478bd9Sstevel@tonic-gate 				}
229*7c478bd9Sstevel@tonic-gate 			}
230*7c478bd9Sstevel@tonic-gate 		} else {
231*7c478bd9Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
232*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
233*7c478bd9Sstevel@tonic-gate 				    "usage: %s [ -hlsuw ] [ user ]\n"), prog);
234*7c478bd9Sstevel@tonic-gate 				exit(1);
235*7c478bd9Sstevel@tonic-gate 			} else
236*7c478bd9Sstevel@tonic-gate 				sel_user = argv[1];
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate 		argc--; argv++;
239*7c478bd9Sstevel@tonic-gate 	}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	/*
242*7c478bd9Sstevel@tonic-gate 	 * read the UTMP_FILE (contains information about each logged in user)
243*7c478bd9Sstevel@tonic-gate 	 */
244*7c478bd9Sstevel@tonic-gate 	if (stat(UTMPX_FILE, &sbuf) == ERR) {
245*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
246*7c478bd9Sstevel@tonic-gate 			prog, UTMPX_FILE, strerror(errno));
247*7c478bd9Sstevel@tonic-gate 		exit(1);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
250*7c478bd9Sstevel@tonic-gate 	size = sizeof (struct utmpx) * entries;
251*7c478bd9Sstevel@tonic-gate 	if ((ut = malloc(size)) == NULL) {
252*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
253*7c478bd9Sstevel@tonic-gate 			prog, UTMPX_FILE, strerror(errno));
254*7c478bd9Sstevel@tonic-gate 		exit(1);
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	utmpbegin = ut;
260*7c478bd9Sstevel@tonic-gate 	utmpend = (struct utmpx *)((char *)utmpbegin + size);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	setutxent();
263*7c478bd9Sstevel@tonic-gate 	while ((utp = getutxent()) != NULL)
264*7c478bd9Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
265*7c478bd9Sstevel@tonic-gate 	endutxent();
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if (header) {	/* print a header */
270*7c478bd9Sstevel@tonic-gate 		prtat(&now);
271*7c478bd9Sstevel@tonic-gate 		for (ut = utmpbegin; ut < utmpend; ut++) {
272*7c478bd9Sstevel@tonic-gate 			if (ut->ut_type == USER_PROCESS) {
273*7c478bd9Sstevel@tonic-gate 				if (!nonuser(*ut))
274*7c478bd9Sstevel@tonic-gate 					nusers++;
275*7c478bd9Sstevel@tonic-gate 			} else if (ut->ut_type == BOOT_TIME) {
276*7c478bd9Sstevel@tonic-gate 				uptime = now - ut->ut_xtime;
277*7c478bd9Sstevel@tonic-gate 				uptime += 30;
278*7c478bd9Sstevel@tonic-gate 				days = uptime / (60*60*24);
279*7c478bd9Sstevel@tonic-gate 				uptime %= (60*60*24);
280*7c478bd9Sstevel@tonic-gate 				hrs = uptime / (60*60);
281*7c478bd9Sstevel@tonic-gate 				uptime %= (60*60);
282*7c478bd9Sstevel@tonic-gate 				mins = uptime / 60;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 				PRINTF((gettext("  up")));
285*7c478bd9Sstevel@tonic-gate 				if (days > 0)
286*7c478bd9Sstevel@tonic-gate 					PRINTF((gettext(
287*7c478bd9Sstevel@tonic-gate 					    " %d day(s),"), days));
288*7c478bd9Sstevel@tonic-gate 				if (hrs > 0 && mins > 0) {
289*7c478bd9Sstevel@tonic-gate 					PRINTF((" %2d:%02d,", hrs, mins));
290*7c478bd9Sstevel@tonic-gate 				} else {
291*7c478bd9Sstevel@tonic-gate 					if (hrs > 0)
292*7c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
293*7c478bd9Sstevel@tonic-gate 						    " %d hr(s),"), hrs));
294*7c478bd9Sstevel@tonic-gate 					if (mins > 0)
295*7c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
296*7c478bd9Sstevel@tonic-gate 						    " %d min(s),"), mins));
297*7c478bd9Sstevel@tonic-gate 				}
298*7c478bd9Sstevel@tonic-gate 			}
299*7c478bd9Sstevel@tonic-gate 		}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 		ut = utmpbegin;	/* rewind utmp data */
302*7c478bd9Sstevel@tonic-gate 		PRINTF((((nusers == 1) ?
303*7c478bd9Sstevel@tonic-gate 		    gettext("  %d user") : gettext("  %d users")), nusers));
304*7c478bd9Sstevel@tonic-gate 		/*
305*7c478bd9Sstevel@tonic-gate 		 * Print 1, 5, and 15 minute load averages.
306*7c478bd9Sstevel@tonic-gate 		 */
307*7c478bd9Sstevel@tonic-gate 		(void) getloadavg(loadavg, 3);
308*7c478bd9Sstevel@tonic-gate 		PRINTF((gettext(",  load average: %.2f, %.2f, %.2f\n"),
309*7c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
310*7c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_15MIN]));
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 		if (firstchar == 'u')	/* uptime command */
313*7c478bd9Sstevel@tonic-gate 			exit(0);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 		if (lflag) {
316*7c478bd9Sstevel@tonic-gate 			PRINTF((dcgettext(NULL, "User     tty           "
317*7c478bd9Sstevel@tonic-gate 			    "login@  idle   JCPU   PCPU  what\n", LC_TIME)));
318*7c478bd9Sstevel@tonic-gate 		} else {
319*7c478bd9Sstevel@tonic-gate 			PRINTF((dcgettext(NULL,
320*7c478bd9Sstevel@tonic-gate 			    "User     tty           idle   what\n", LC_TIME)));
321*7c478bd9Sstevel@tonic-gate 		}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		if (fflush(stdout) == EOF) {
324*7c478bd9Sstevel@tonic-gate 			perror((gettext("%s: fflush failed\n"), prog));
325*7c478bd9Sstevel@tonic-gate 			exit(1);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	/*
330*7c478bd9Sstevel@tonic-gate 	 * loop through /proc, reading info about each process
331*7c478bd9Sstevel@tonic-gate 	 * and build the parent/child tree
332*7c478bd9Sstevel@tonic-gate 	 */
333*7c478bd9Sstevel@tonic-gate 	if (!(dirp = opendir(PROCDIR))) {
334*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
335*7c478bd9Sstevel@tonic-gate 			prog, PROCDIR, strerror(errno));
336*7c478bd9Sstevel@tonic-gate 		exit(1);
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
340*7c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
341*7c478bd9Sstevel@tonic-gate 			continue;
342*7c478bd9Sstevel@tonic-gate retry:
343*7c478bd9Sstevel@tonic-gate 		(void) sprintf(pname, "%s/%s/", PROCDIR, dp->d_name);
344*7c478bd9Sstevel@tonic-gate 		fname = pname + strlen(pname);
345*7c478bd9Sstevel@tonic-gate 		(void) strcpy(fname, "psinfo");
346*7c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0)
347*7c478bd9Sstevel@tonic-gate 			continue;
348*7c478bd9Sstevel@tonic-gate 		if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
349*7c478bd9Sstevel@tonic-gate 			int err = errno;
350*7c478bd9Sstevel@tonic-gate 			(void) close(procfd);
351*7c478bd9Sstevel@tonic-gate 			if (err == EAGAIN)
352*7c478bd9Sstevel@tonic-gate 				goto retry;
353*7c478bd9Sstevel@tonic-gate 			if (err != ENOENT)
354*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
355*7c478bd9Sstevel@tonic-gate 				    "%s: read() failed on %s: %s \n"),
356*7c478bd9Sstevel@tonic-gate 				    prog, pname, strerror(err));
357*7c478bd9Sstevel@tonic-gate 			continue;
358*7c478bd9Sstevel@tonic-gate 		}
359*7c478bd9Sstevel@tonic-gate 		(void) close(procfd);
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 		up = findhash(info.pr_pid);
362*7c478bd9Sstevel@tonic-gate 		up->p_ttyd = info.pr_ttydev;
363*7c478bd9Sstevel@tonic-gate 		up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
364*7c478bd9Sstevel@tonic-gate 		up->p_time = 0;
365*7c478bd9Sstevel@tonic-gate 		up->p_ctime = 0;
366*7c478bd9Sstevel@tonic-gate 		up->p_igintr = 0;
367*7c478bd9Sstevel@tonic-gate 		(void) strncpy(up->p_comm, info.pr_fname,
368*7c478bd9Sstevel@tonic-gate 		    sizeof (info.pr_fname));
369*7c478bd9Sstevel@tonic-gate 		up->p_args[0] = 0;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 		if (up->p_state != NONE && up->p_state != ZOMBIE) {
372*7c478bd9Sstevel@tonic-gate 			(void) strcpy(fname, "status");
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
375*7c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
380*7c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 			if (procfd < 0)
383*7c478bd9Sstevel@tonic-gate 				continue;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 			if (read(procfd, &statinfo, sizeof (statinfo))
386*7c478bd9Sstevel@tonic-gate 			    != sizeof (statinfo)) {
387*7c478bd9Sstevel@tonic-gate 				int err = errno;
388*7c478bd9Sstevel@tonic-gate 				(void) close(procfd);
389*7c478bd9Sstevel@tonic-gate 				if (err == EAGAIN)
390*7c478bd9Sstevel@tonic-gate 					goto retry;
391*7c478bd9Sstevel@tonic-gate 				if (err != ENOENT)
392*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
393*7c478bd9Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
394*7c478bd9Sstevel@tonic-gate 					    prog, pname, strerror(err));
395*7c478bd9Sstevel@tonic-gate 				continue;
396*7c478bd9Sstevel@tonic-gate 			}
397*7c478bd9Sstevel@tonic-gate 			(void) close(procfd);
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 			up->p_time = statinfo.pr_utime.tv_sec +
400*7c478bd9Sstevel@tonic-gate 			    statinfo.pr_stime.tv_sec;	/* seconds */
401*7c478bd9Sstevel@tonic-gate 			up->p_ctime = statinfo.pr_cutime.tv_sec +
402*7c478bd9Sstevel@tonic-gate 			    statinfo.pr_cstime.tv_sec;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 			(void) strcpy(fname, "sigact");
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
407*7c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
412*7c478bd9Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 			if (procfd < 0)
415*7c478bd9Sstevel@tonic-gate 				continue;
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 			if (read(procfd, actinfo, sizeof (actinfo))
418*7c478bd9Sstevel@tonic-gate 			    != sizeof (actinfo)) {
419*7c478bd9Sstevel@tonic-gate 				int err = errno;
420*7c478bd9Sstevel@tonic-gate 				(void) close(procfd);
421*7c478bd9Sstevel@tonic-gate 				if (err == EAGAIN)
422*7c478bd9Sstevel@tonic-gate 					goto retry;
423*7c478bd9Sstevel@tonic-gate 				if (err != ENOENT)
424*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
425*7c478bd9Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
426*7c478bd9Sstevel@tonic-gate 					    prog, pname, strerror(err));
427*7c478bd9Sstevel@tonic-gate 				continue;
428*7c478bd9Sstevel@tonic-gate 			}
429*7c478bd9Sstevel@tonic-gate 			(void) close(procfd);
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 			up->p_igintr =
432*7c478bd9Sstevel@tonic-gate 				actinfo[SIGINT-1].sa_handler == SIG_IGN &&
433*7c478bd9Sstevel@tonic-gate 				actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 			/*
436*7c478bd9Sstevel@tonic-gate 			 * Process args.
437*7c478bd9Sstevel@tonic-gate 			 */
438*7c478bd9Sstevel@tonic-gate 			up->p_args[0] = 0;
439*7c478bd9Sstevel@tonic-gate 			clnarglist(info.pr_psargs);
440*7c478bd9Sstevel@tonic-gate 			(void) strcat(up->p_args, info.pr_psargs);
441*7c478bd9Sstevel@tonic-gate 			if (up->p_args[0] == 0 ||
442*7c478bd9Sstevel@tonic-gate 			    up->p_args[0] == '-' && up->p_args[1] <= ' ' ||
443*7c478bd9Sstevel@tonic-gate 			    up->p_args[0] == '?') {
444*7c478bd9Sstevel@tonic-gate 				(void) strcat(up->p_args, " (");
445*7c478bd9Sstevel@tonic-gate 				(void) strcat(up->p_args, up->p_comm);
446*7c478bd9Sstevel@tonic-gate 				(void) strcat(up->p_args, ")");
447*7c478bd9Sstevel@tonic-gate 			}
448*7c478bd9Sstevel@tonic-gate 		}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 		/*
451*7c478bd9Sstevel@tonic-gate 		 * link pgrp together in case parents go away
452*7c478bd9Sstevel@tonic-gate 		 * Pgrp chain is a single linked list originating
453*7c478bd9Sstevel@tonic-gate 		 * from the pgrp leader to its group member.
454*7c478bd9Sstevel@tonic-gate 		 */
455*7c478bd9Sstevel@tonic-gate 		if (info.pr_pgid != info.pr_pid) {	/* not pgrp leader */
456*7c478bd9Sstevel@tonic-gate 			pgrp = findhash(info.pr_pgid);
457*7c478bd9Sstevel@tonic-gate 			up->p_pgrpl = pgrp->p_pgrpl;
458*7c478bd9Sstevel@tonic-gate 			pgrp->p_pgrpl = up;
459*7c478bd9Sstevel@tonic-gate 		}
460*7c478bd9Sstevel@tonic-gate 		parent = findhash(info.pr_ppid);
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 		/* if this is the new member, link it in */
463*7c478bd9Sstevel@tonic-gate 		if (parent->p_upid != INITPROCESS) {
464*7c478bd9Sstevel@tonic-gate 			if (parent->p_child) {
465*7c478bd9Sstevel@tonic-gate 				up->p_sibling = parent->p_child;
466*7c478bd9Sstevel@tonic-gate 				up->p_child = 0;
467*7c478bd9Sstevel@tonic-gate 			}
468*7c478bd9Sstevel@tonic-gate 			parent->p_child = up;
469*7c478bd9Sstevel@tonic-gate 		}
470*7c478bd9Sstevel@tonic-gate 	}
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	/* revert to non-privileged user after opening */
473*7c478bd9Sstevel@tonic-gate 	(void) __priv_relinquish();
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
476*7c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	/*
479*7c478bd9Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
480*7c478bd9Sstevel@tonic-gate 	 * about each logged in user
481*7c478bd9Sstevel@tonic-gate 	 */
482*7c478bd9Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
483*7c478bd9Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
484*7c478bd9Sstevel@tonic-gate 			continue;
485*7c478bd9Sstevel@tonic-gate 		if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
486*7c478bd9Sstevel@tonic-gate 			continue;	/* we're looking for somebody else */
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 		/* print login name of the user */
489*7c478bd9Sstevel@tonic-gate 		PRINTF(("%-*.*s ", NMAX, NMAX, ut->ut_name));
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 		/* print tty user is on */
492*7c478bd9Sstevel@tonic-gate 		if (lflag) {
493*7c478bd9Sstevel@tonic-gate 			PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));
494*7c478bd9Sstevel@tonic-gate 		} else {
495*7c478bd9Sstevel@tonic-gate 			if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
496*7c478bd9Sstevel@tonic-gate 			    ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
497*7c478bd9Sstevel@tonic-gate 				PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
498*7c478bd9Sstevel@tonic-gate 			} else {
499*7c478bd9Sstevel@tonic-gate 				PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));
500*7c478bd9Sstevel@tonic-gate 			}
501*7c478bd9Sstevel@tonic-gate 		}
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 		/* print when the user logged in */
504*7c478bd9Sstevel@tonic-gate 		if (lflag) {
505*7c478bd9Sstevel@tonic-gate 			time_t tim = ut->ut_xtime;
506*7c478bd9Sstevel@tonic-gate 			prtat(&tim);
507*7c478bd9Sstevel@tonic-gate 		}
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 		/* print idle time */
510*7c478bd9Sstevel@tonic-gate 		idle = findidle(ut->ut_line);
511*7c478bd9Sstevel@tonic-gate 		if (idle >= 36 * 60) {
512*7c478bd9Sstevel@tonic-gate 			PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
513*7c478bd9Sstevel@tonic-gate 			    (idle + 12 * 60) / (24 * 60)));
514*7c478bd9Sstevel@tonic-gate 		} else
515*7c478bd9Sstevel@tonic-gate 			prttime(idle, " ");
516*7c478bd9Sstevel@tonic-gate 		showtotals(findhash(ut->ut_pid));
517*7c478bd9Sstevel@tonic-gate 	}
518*7c478bd9Sstevel@tonic-gate 	if (fclose(stdout) == EOF) {
519*7c478bd9Sstevel@tonic-gate 		perror((gettext("%s: fclose failed"), prog));
520*7c478bd9Sstevel@tonic-gate 		exit(1);
521*7c478bd9Sstevel@tonic-gate 	}
522*7c478bd9Sstevel@tonic-gate 	return (0);
523*7c478bd9Sstevel@tonic-gate }
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate /*
526*7c478bd9Sstevel@tonic-gate  *  Prints the CPU time for all processes & children,
527*7c478bd9Sstevel@tonic-gate  *  and the cpu time for interesting process,
528*7c478bd9Sstevel@tonic-gate  *  and what the user is doing.
529*7c478bd9Sstevel@tonic-gate  */
530*7c478bd9Sstevel@tonic-gate static void
531*7c478bd9Sstevel@tonic-gate showtotals(struct uproc *up)
532*7c478bd9Sstevel@tonic-gate {
533*7c478bd9Sstevel@tonic-gate 	jobtime = 0;
534*7c478bd9Sstevel@tonic-gate 	proctime = 0;
535*7c478bd9Sstevel@tonic-gate 	empty = 1;
536*7c478bd9Sstevel@tonic-gate 	curpid = -1;
537*7c478bd9Sstevel@tonic-gate 	add_times = 1;
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	calctotals(up);
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	if (lflag) {
542*7c478bd9Sstevel@tonic-gate 		/* print CPU time for all processes & children */
543*7c478bd9Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
544*7c478bd9Sstevel@tonic-gate 		prttime((time_t)jobtime, " ");
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 		/* print cpu time for interesting process */
547*7c478bd9Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
548*7c478bd9Sstevel@tonic-gate 		prttime((time_t)proctime, " ");
549*7c478bd9Sstevel@tonic-gate 	}
550*7c478bd9Sstevel@tonic-gate 	/* what user is doing, current process */
551*7c478bd9Sstevel@tonic-gate 	PRINTF((" %-.32s\n", doing));
552*7c478bd9Sstevel@tonic-gate }
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /*
555*7c478bd9Sstevel@tonic-gate  *  This recursive routine descends the process
556*7c478bd9Sstevel@tonic-gate  *  tree starting from the given process pointer(up).
557*7c478bd9Sstevel@tonic-gate  *  It used depth-first search strategy and also marked
558*7c478bd9Sstevel@tonic-gate  *  each node as visited as it traversed down the tree.
559*7c478bd9Sstevel@tonic-gate  *  It calculates the process time for all processes &
560*7c478bd9Sstevel@tonic-gate  *  children.  It also finds the interesting process
561*7c478bd9Sstevel@tonic-gate  *  and determines its cpu time and command.
562*7c478bd9Sstevel@tonic-gate  */
563*7c478bd9Sstevel@tonic-gate static void
564*7c478bd9Sstevel@tonic-gate calctotals(struct uproc *up)
565*7c478bd9Sstevel@tonic-gate {
566*7c478bd9Sstevel@tonic-gate 	struct uproc   *zp;
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	/*
569*7c478bd9Sstevel@tonic-gate 	 * Once a node has been visited, stop adding cpu times
570*7c478bd9Sstevel@tonic-gate 	 * for its children so they don't get totalled twice.
571*7c478bd9Sstevel@tonic-gate 	 * Still look for the interesting job for this utmp
572*7c478bd9Sstevel@tonic-gate 	 * entry, however.
573*7c478bd9Sstevel@tonic-gate 	 */
574*7c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED)
575*7c478bd9Sstevel@tonic-gate 		add_times = 0;
576*7c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
577*7c478bd9Sstevel@tonic-gate 	if (up->p_state == NONE || up->p_state == ZOMBIE)
578*7c478bd9Sstevel@tonic-gate 		return;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	if (empty && !up->p_igintr) {
581*7c478bd9Sstevel@tonic-gate 		empty = 0;
582*7c478bd9Sstevel@tonic-gate 		curpid = -1;
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	if (up->p_upid > curpid && (!up->p_igintr || empty)) {
586*7c478bd9Sstevel@tonic-gate 		curpid = up->p_upid;
587*7c478bd9Sstevel@tonic-gate 		if (lflag)
588*7c478bd9Sstevel@tonic-gate 			(void) strcpy(doing, up->p_args);
589*7c478bd9Sstevel@tonic-gate 		else
590*7c478bd9Sstevel@tonic-gate 			(void) strcpy(doing, up->p_comm);
591*7c478bd9Sstevel@tonic-gate 	}
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	if (add_times == 1) {
594*7c478bd9Sstevel@tonic-gate 		jobtime += up->p_time + up->p_ctime;
595*7c478bd9Sstevel@tonic-gate 		proctime += up->p_time;
596*7c478bd9Sstevel@tonic-gate 	}
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	/* descend for its children */
599*7c478bd9Sstevel@tonic-gate 	if (up->p_child) {
600*7c478bd9Sstevel@tonic-gate 		calctotals(up->p_child);
601*7c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
602*7c478bd9Sstevel@tonic-gate 			calctotals(zp);
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate }
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate /*
607*7c478bd9Sstevel@tonic-gate  *   Findhash  finds the appropriate entry in the process
608*7c478bd9Sstevel@tonic-gate  *   hash table (pr_htbl) for the given pid in case that
609*7c478bd9Sstevel@tonic-gate  *   pid exists on the hash chain. It returns back a pointer
610*7c478bd9Sstevel@tonic-gate  *   to that uproc structure. If this is a new pid, it allocates
611*7c478bd9Sstevel@tonic-gate  *   a new node, initializes it, links it into the chain (after
612*7c478bd9Sstevel@tonic-gate  *   head) and returns a structure pointer.
613*7c478bd9Sstevel@tonic-gate  */
614*7c478bd9Sstevel@tonic-gate static struct uproc *
615*7c478bd9Sstevel@tonic-gate findhash(pid_t pid)
616*7c478bd9Sstevel@tonic-gate {
617*7c478bd9Sstevel@tonic-gate 	struct uproc *up, *tp;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	tp = up = &pr_htbl[pid % HSIZE];
620*7c478bd9Sstevel@tonic-gate 	if (up->p_upid == 0) {			/* empty slot */
621*7c478bd9Sstevel@tonic-gate 		up->p_upid = pid;
622*7c478bd9Sstevel@tonic-gate 		up->p_state = NONE;
623*7c478bd9Sstevel@tonic-gate 		up->p_child = up->p_sibling = up->p_pgrpl = up->p_link = 0;
624*7c478bd9Sstevel@tonic-gate 		return (up);
625*7c478bd9Sstevel@tonic-gate 	}
626*7c478bd9Sstevel@tonic-gate 	if (up->p_upid == pid) {		/* found in hash table */
627*7c478bd9Sstevel@tonic-gate 		return (up);
628*7c478bd9Sstevel@tonic-gate 	}
629*7c478bd9Sstevel@tonic-gate 	for (tp = up->p_link; tp; tp = tp->p_link) {	/* follow chain */
630*7c478bd9Sstevel@tonic-gate 		if (tp->p_upid == pid)
631*7c478bd9Sstevel@tonic-gate 			return (tp);
632*7c478bd9Sstevel@tonic-gate 	}
633*7c478bd9Sstevel@tonic-gate 	tp = malloc(sizeof (*tp));		/* add new node */
634*7c478bd9Sstevel@tonic-gate 	if (!tp) {
635*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory!: %s\n"),
636*7c478bd9Sstevel@tonic-gate 			prog, strerror(errno));
637*7c478bd9Sstevel@tonic-gate 		exit(1);
638*7c478bd9Sstevel@tonic-gate 	}
639*7c478bd9Sstevel@tonic-gate 	(void) memset(tp, 0, sizeof (*tp));
640*7c478bd9Sstevel@tonic-gate 	tp->p_upid = pid;
641*7c478bd9Sstevel@tonic-gate 	tp->p_state = NONE;
642*7c478bd9Sstevel@tonic-gate 	tp->p_child = tp->p_sibling = tp->p_pgrpl = 0;
643*7c478bd9Sstevel@tonic-gate 	tp->p_link = up->p_link;		/* insert after head */
644*7c478bd9Sstevel@tonic-gate 	up->p_link = tp;
645*7c478bd9Sstevel@tonic-gate 	return (tp);
646*7c478bd9Sstevel@tonic-gate }
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate #define	HR	(60 * 60)
649*7c478bd9Sstevel@tonic-gate #define	DAY	(24 * HR)
650*7c478bd9Sstevel@tonic-gate #define	MON	(30 * DAY)
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate /*
653*7c478bd9Sstevel@tonic-gate  * prttime prints a time in hours and minutes or minutes and seconds.
654*7c478bd9Sstevel@tonic-gate  * The character string tail is printed at the end, obvious
655*7c478bd9Sstevel@tonic-gate  * strings to pass are "", " ", or "am".
656*7c478bd9Sstevel@tonic-gate  */
657*7c478bd9Sstevel@tonic-gate static void
658*7c478bd9Sstevel@tonic-gate prttime(time_t tim, char *tail)
659*7c478bd9Sstevel@tonic-gate {
660*7c478bd9Sstevel@tonic-gate 	if (tim >= 60) {
661*7c478bd9Sstevel@tonic-gate 		PRINTF((dcgettext(NULL, "%3d:%02d", LC_TIME),
662*7c478bd9Sstevel@tonic-gate 		    (int)tim/60, (int)tim%60));
663*7c478bd9Sstevel@tonic-gate 	} else if (tim > 0) {
664*7c478bd9Sstevel@tonic-gate 		PRINTF((dcgettext(NULL, "    %2d", LC_TIME), (int)tim));
665*7c478bd9Sstevel@tonic-gate 	} else {
666*7c478bd9Sstevel@tonic-gate 		PRINTF(("      "));
667*7c478bd9Sstevel@tonic-gate 	}
668*7c478bd9Sstevel@tonic-gate 	PRINTF(("%s", tail));
669*7c478bd9Sstevel@tonic-gate }
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate /*
672*7c478bd9Sstevel@tonic-gate  * prints a 12 hour time given a pointer to a time of day
673*7c478bd9Sstevel@tonic-gate  */
674*7c478bd9Sstevel@tonic-gate static void
675*7c478bd9Sstevel@tonic-gate prtat(time_t *time)
676*7c478bd9Sstevel@tonic-gate {
677*7c478bd9Sstevel@tonic-gate 	struct tm	*p;
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 	p = localtime(time);
680*7c478bd9Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
681*7c478bd9Sstevel@tonic-gate 		char timestr[50];
682*7c478bd9Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
683*7c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, "%l:%M""%p", LC_TIME), p);
684*7c478bd9Sstevel@tonic-gate 		checkampm(timestr);
685*7c478bd9Sstevel@tonic-gate 		PRINTF((" %s", timestr));
686*7c478bd9Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
687*7c478bd9Sstevel@tonic-gate 		char weekdaytime[20];
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
690*7c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, "%a%l%p", LC_TIME), p);
691*7c478bd9Sstevel@tonic-gate 		checkampm(weekdaytime);
692*7c478bd9Sstevel@tonic-gate 		PRINTF((" %s", weekdaytime));
693*7c478bd9Sstevel@tonic-gate 	} else {
694*7c478bd9Sstevel@tonic-gate 		char monthtime[20];
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
697*7c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, "%e%b%y", LC_TIME), p);
698*7c478bd9Sstevel@tonic-gate 		PRINTF((" %s", monthtime));
699*7c478bd9Sstevel@tonic-gate 	}
700*7c478bd9Sstevel@tonic-gate }
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate /*
703*7c478bd9Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
704*7c478bd9Sstevel@tonic-gate  */
705*7c478bd9Sstevel@tonic-gate static time_t
706*7c478bd9Sstevel@tonic-gate findidle(char *devname)
707*7c478bd9Sstevel@tonic-gate {
708*7c478bd9Sstevel@tonic-gate 	struct stat stbuf;
709*7c478bd9Sstevel@tonic-gate 	time_t lastaction, diff;
710*7c478bd9Sstevel@tonic-gate 	char ttyname[64];
711*7c478bd9Sstevel@tonic-gate 
712*7c478bd9Sstevel@tonic-gate 	(void) strcpy(ttyname, "/dev/");
713*7c478bd9Sstevel@tonic-gate 	(void) strcat(ttyname, devname);
714*7c478bd9Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
715*7c478bd9Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
716*7c478bd9Sstevel@tonic-gate 		diff = now - lastaction;
717*7c478bd9Sstevel@tonic-gate 		diff = DIV60(diff);
718*7c478bd9Sstevel@tonic-gate 		if (diff < 0)
719*7c478bd9Sstevel@tonic-gate 			diff = 0;
720*7c478bd9Sstevel@tonic-gate 	} else
721*7c478bd9Sstevel@tonic-gate 		diff = 0;
722*7c478bd9Sstevel@tonic-gate 	return (diff);
723*7c478bd9Sstevel@tonic-gate }
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate /*
726*7c478bd9Sstevel@tonic-gate  * given a pointer to the argument string get rid of unsavory characters.
727*7c478bd9Sstevel@tonic-gate  */
728*7c478bd9Sstevel@tonic-gate static void
729*7c478bd9Sstevel@tonic-gate clnarglist(char *arglist)
730*7c478bd9Sstevel@tonic-gate {
731*7c478bd9Sstevel@tonic-gate 	char	*c;
732*7c478bd9Sstevel@tonic-gate 	int 	err = 0;
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	/* get rid of unsavory characters */
735*7c478bd9Sstevel@tonic-gate 	for (c = arglist; *c != NULL; c++) {
736*7c478bd9Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
737*7c478bd9Sstevel@tonic-gate 			if (err++ > 5) {
738*7c478bd9Sstevel@tonic-gate 				*arglist = NULL;
739*7c478bd9Sstevel@tonic-gate 				break;
740*7c478bd9Sstevel@tonic-gate 			}
741*7c478bd9Sstevel@tonic-gate 			*c = '?';
742*7c478bd9Sstevel@tonic-gate 		}
743*7c478bd9Sstevel@tonic-gate 	}
744*7c478bd9Sstevel@tonic-gate }
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate /* replaces all occurences of AM/PM with am/pm */
747*7c478bd9Sstevel@tonic-gate static void
748*7c478bd9Sstevel@tonic-gate checkampm(char *str)
749*7c478bd9Sstevel@tonic-gate {
750*7c478bd9Sstevel@tonic-gate 	char *ampm;
751*7c478bd9Sstevel@tonic-gate 	while ((ampm = strstr(str, "AM")) != NULL ||
752*7c478bd9Sstevel@tonic-gate 	    (ampm = strstr(str, "PM")) != NULL) {
753*7c478bd9Sstevel@tonic-gate 		*ampm = tolower(*ampm);
754*7c478bd9Sstevel@tonic-gate 		*(ampm+1) = tolower(*(ampm+1));
755*7c478bd9Sstevel@tonic-gate 	}
756*7c478bd9Sstevel@tonic-gate }
757