xref: /onnv-gate/usr/src/cmd/w/w.c (revision 10478:35e03441c55c)
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
5*10478SSumanth.Naropanth@Sun.COM  * Common Development and Distribution License (the "License").
6*10478SSumanth.Naropanth@Sun.COM  * 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  */
210Sstevel@tonic-gate /*
22*10478SSumanth.Naropanth@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * This is the new w command which takes advantage of
410Sstevel@tonic-gate  * the /proc interface to gain access to the information
420Sstevel@tonic-gate  * of all the processes currently on the system.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * This program also implements 'uptime'.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Maintenance note:
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Much of this code is replicated in whodo.c.  If you're
490Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <stdio.h>
530Sstevel@tonic-gate #include <string.h>
540Sstevel@tonic-gate #include <stdarg.h>
550Sstevel@tonic-gate #include <stdlib.h>
560Sstevel@tonic-gate #include <ctype.h>
570Sstevel@tonic-gate #include <fcntl.h>
580Sstevel@tonic-gate #include <time.h>
590Sstevel@tonic-gate #include <errno.h>
600Sstevel@tonic-gate #include <sys/types.h>
610Sstevel@tonic-gate #include <utmpx.h>
620Sstevel@tonic-gate #include <sys/stat.h>
630Sstevel@tonic-gate #include <dirent.h>
640Sstevel@tonic-gate #include <procfs.h>		/* /proc header file */
650Sstevel@tonic-gate #include <locale.h>
660Sstevel@tonic-gate #include <unistd.h>
670Sstevel@tonic-gate #include <sys/loadavg.h>
680Sstevel@tonic-gate #include <limits.h>
690Sstevel@tonic-gate #include <priv_utils.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * utmpx defines wider fields for user and line.  For compatibility of output,
730Sstevel@tonic-gate  * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
740Sstevel@tonic-gate  * to use the full lengths.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate #ifndef UTMPX_NAMELEN
770Sstevel@tonic-gate /* XXX - utmp - fix name length */
780Sstevel@tonic-gate #define	NMAX		(_POSIX_LOGIN_NAME_MAX - 1)
790Sstevel@tonic-gate #define	LMAX		12
800Sstevel@tonic-gate #else	/* UTMPX_NAMELEN */
810Sstevel@tonic-gate static struct utmpx dummy;
820Sstevel@tonic-gate #define	NMAX		(sizeof (dummy.ut_user))
830Sstevel@tonic-gate #define	LMAX		(sizeof (dummy.ut_line))
840Sstevel@tonic-gate #endif /* UTMPX_NAMELEN */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)	/* x/60 rounded */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #ifdef ERR
890Sstevel@tonic-gate #undef ERR
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate #define	ERR		(-1)
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	HSIZE		256		/* size of process hash table 	*/
940Sstevel@tonic-gate #define	PROCDIR		"/proc"
950Sstevel@tonic-gate #define	INITPROCESS	(pid_t)1	/* init process pid */
960Sstevel@tonic-gate #define	NONE		'n'		/* no state */
970Sstevel@tonic-gate #define	RUNNING		'r'		/* runnable process */
980Sstevel@tonic-gate #define	ZOMBIE		'z'		/* zombie process */
990Sstevel@tonic-gate #define	VISITED		'v'		/* marked node as visited */
1000Sstevel@tonic-gate #define	PRINTF(a)	if (printf a < 0) { \
1010Sstevel@tonic-gate 		perror((gettext("%s: printf failed"), prog)); \
1020Sstevel@tonic-gate 		exit(1); }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct uproc {
1050Sstevel@tonic-gate 	pid_t	p_upid;			/* process id */
1060Sstevel@tonic-gate 	char	p_state;		/* numeric value of process state */
1070Sstevel@tonic-gate 	dev_t   p_ttyd;			/* controlling tty of process */
1080Sstevel@tonic-gate 	time_t  p_time;			/* seconds of user & system time */
1090Sstevel@tonic-gate 	time_t	p_ctime;		/* seconds of child user & sys time */
1100Sstevel@tonic-gate 	int	p_igintr;		/* 1 = ignores SIGQUIT and SIGINT */
1110Sstevel@tonic-gate 	char    p_comm[PRARGSZ+1];	/* command */
1120Sstevel@tonic-gate 	char    p_args[PRARGSZ+1];	/* command line arguments */
1130Sstevel@tonic-gate 	struct uproc	*p_child,	/* first child pointer */
1140Sstevel@tonic-gate 			*p_sibling,	/* sibling pointer */
1150Sstevel@tonic-gate 			*p_pgrpl,	/* pgrp link */
1160Sstevel@tonic-gate 			*p_link;	/* hash table chain pointer */
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  *  	define hash table for struct uproc
1210Sstevel@tonic-gate  *	Hash function uses process id
1220Sstevel@tonic-gate  *	and the size of the hash table(HSIZE)
1230Sstevel@tonic-gate  *	to determine process index into the table.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate static struct uproc	pr_htbl[HSIZE];
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static struct 	uproc	*findhash(pid_t);
1280Sstevel@tonic-gate static time_t  	findidle(char *);
1290Sstevel@tonic-gate static void	clnarglist(char *);
1300Sstevel@tonic-gate static void	showtotals(struct uproc *);
1310Sstevel@tonic-gate static void	calctotals(struct uproc *);
1320Sstevel@tonic-gate static void	prttime(time_t, char *);
1330Sstevel@tonic-gate static void	prtat(time_t *time);
1340Sstevel@tonic-gate static void	checkampm(char *str);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static char	*prog;		/* pointer to invocation name */
1370Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
1380Sstevel@tonic-gate static int	lflag = 1;	/* set if -l flag; 0 for -s flag: short form */
1390Sstevel@tonic-gate static char	*sel_user;	/* login of particular user selected */
1400Sstevel@tonic-gate static char 	firstchar;	/* first char of name of prog invoked as */
1410Sstevel@tonic-gate static int	login;		/* true if invoked as login shell */
1420Sstevel@tonic-gate static time_t	now;		/* current time of day */
1430Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
1440Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
1450Sstevel@tonic-gate static time_t	idle;		/* number of minutes user is idle */
1460Sstevel@tonic-gate static time_t	jobtime;	/* total cpu time visible */
1470Sstevel@tonic-gate static char	doing[520];	/* process attached to terminal */
1480Sstevel@tonic-gate static time_t	proctime;	/* cpu time of process in doing */
1490Sstevel@tonic-gate static pid_t	curpid, empty;
1500Sstevel@tonic-gate static int	add_times;	/* boolean: add the cpu times or not */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate #if SIGQUIT > SIGINT
1530Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
1540Sstevel@tonic-gate #else
1550Sstevel@tonic-gate #define	ACTSIZE	SIGINT
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate int
main(int argc,char * argv[])1590Sstevel@tonic-gate main(int argc, char *argv[])
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct utmpx	*ut;
1620Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
1630Sstevel@tonic-gate 	struct utmpx	*utmpend;
1640Sstevel@tonic-gate 	struct utmpx	*utp;
1650Sstevel@tonic-gate 	struct uproc	*up, *parent, *pgrp;
1660Sstevel@tonic-gate 	struct psinfo	info;
1670Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
1680Sstevel@tonic-gate 	struct pstatus	statinfo;
1690Sstevel@tonic-gate 	size_t		size;
1700Sstevel@tonic-gate 	struct stat	sbuf;
1710Sstevel@tonic-gate 	DIR   		*dirp;
1720Sstevel@tonic-gate 	struct	dirent	*dp;
1730Sstevel@tonic-gate 	char		pname[64];
1740Sstevel@tonic-gate 	char 		*fname;
1750Sstevel@tonic-gate 	int		procfd;
1760Sstevel@tonic-gate 	char		*cp;
1770Sstevel@tonic-gate 	int		i;
1780Sstevel@tonic-gate 	int		days, hrs, mins;
1790Sstevel@tonic-gate 	int		entries;
1800Sstevel@tonic-gate 	double		loadavg[3];
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * This program needs the proc_owner privilege
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER,
1860Sstevel@tonic-gate 	    (char *)NULL);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1890Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1900Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1910Sstevel@tonic-gate #endif
1920Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	login = (argv[0][0] == '-');
1950Sstevel@tonic-gate 	cp = strrchr(argv[0], '/');
1960Sstevel@tonic-gate 	firstchar = login ? argv[0][1] : (cp == 0) ? argv[0][0] : cp[1];
1970Sstevel@tonic-gate 	prog = argv[0];
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	while (argc > 1) {
2000Sstevel@tonic-gate 		if (argv[1][0] == '-') {
2010Sstevel@tonic-gate 			for (i = 1; argv[1][i]; i++) {
2020Sstevel@tonic-gate 				switch (argv[1][i]) {
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 				case 'h':
2050Sstevel@tonic-gate 					header = 0;
2060Sstevel@tonic-gate 					break;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 				case 'l':
2090Sstevel@tonic-gate 					lflag++;
2100Sstevel@tonic-gate 					break;
2110Sstevel@tonic-gate 				case 's':
2120Sstevel@tonic-gate 					lflag = 0;
2130Sstevel@tonic-gate 					break;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 				case 'u':
2160Sstevel@tonic-gate 				case 'w':
2170Sstevel@tonic-gate 					firstchar = argv[1][i];
2180Sstevel@tonic-gate 					break;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 				default:
2210Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2220Sstevel@tonic-gate 					    "%s: bad flag %s\n"),
2230Sstevel@tonic-gate 					    prog, argv[1]);
2240Sstevel@tonic-gate 					exit(1);
2250Sstevel@tonic-gate 				}
2260Sstevel@tonic-gate 			}
2270Sstevel@tonic-gate 		} else {
2280Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
2290Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2300Sstevel@tonic-gate 				    "usage: %s [ -hlsuw ] [ user ]\n"), prog);
2310Sstevel@tonic-gate 				exit(1);
2320Sstevel@tonic-gate 			} else
2330Sstevel@tonic-gate 				sel_user = argv[1];
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 		argc--; argv++;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * read the UTMP_FILE (contains information about each logged in user)
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	if (stat(UTMPX_FILE, &sbuf) == ERR) {
2420Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
243*10478SSumanth.Naropanth@Sun.COM 		    prog, UTMPX_FILE, strerror(errno));
2440Sstevel@tonic-gate 		exit(1);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
2470Sstevel@tonic-gate 	size = sizeof (struct utmpx) * entries;
2480Sstevel@tonic-gate 	if ((ut = malloc(size)) == NULL) {
2490Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
250*10478SSumanth.Naropanth@Sun.COM 		    prog, UTMPX_FILE, strerror(errno));
2510Sstevel@tonic-gate 		exit(1);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	utmpbegin = ut;
2570Sstevel@tonic-gate 	utmpend = (struct utmpx *)((char *)utmpbegin + size);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	setutxent();
260*10478SSumanth.Naropanth@Sun.COM 	while ((ut < utmpend) && ((utp = getutxent()) != NULL))
2610Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
2620Sstevel@tonic-gate 	endutxent();
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (header) {	/* print a header */
2670Sstevel@tonic-gate 		prtat(&now);
2680Sstevel@tonic-gate 		for (ut = utmpbegin; ut < utmpend; ut++) {
2690Sstevel@tonic-gate 			if (ut->ut_type == USER_PROCESS) {
2700Sstevel@tonic-gate 				if (!nonuser(*ut))
2710Sstevel@tonic-gate 					nusers++;
2720Sstevel@tonic-gate 			} else if (ut->ut_type == BOOT_TIME) {
2730Sstevel@tonic-gate 				uptime = now - ut->ut_xtime;
2740Sstevel@tonic-gate 				uptime += 30;
2750Sstevel@tonic-gate 				days = uptime / (60*60*24);
2760Sstevel@tonic-gate 				uptime %= (60*60*24);
2770Sstevel@tonic-gate 				hrs = uptime / (60*60);
2780Sstevel@tonic-gate 				uptime %= (60*60);
2790Sstevel@tonic-gate 				mins = uptime / 60;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 				PRINTF((gettext("  up")));
2820Sstevel@tonic-gate 				if (days > 0)
2830Sstevel@tonic-gate 					PRINTF((gettext(
2840Sstevel@tonic-gate 					    " %d day(s),"), days));
2850Sstevel@tonic-gate 				if (hrs > 0 && mins > 0) {
2860Sstevel@tonic-gate 					PRINTF((" %2d:%02d,", hrs, mins));
2870Sstevel@tonic-gate 				} else {
2880Sstevel@tonic-gate 					if (hrs > 0)
2890Sstevel@tonic-gate 						PRINTF((gettext(
2900Sstevel@tonic-gate 						    " %d hr(s),"), hrs));
2910Sstevel@tonic-gate 					if (mins > 0)
2920Sstevel@tonic-gate 						PRINTF((gettext(
2930Sstevel@tonic-gate 						    " %d min(s),"), mins));
2940Sstevel@tonic-gate 				}
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		ut = utmpbegin;	/* rewind utmp data */
2990Sstevel@tonic-gate 		PRINTF((((nusers == 1) ?
3000Sstevel@tonic-gate 		    gettext("  %d user") : gettext("  %d users")), nusers));
3010Sstevel@tonic-gate 		/*
3020Sstevel@tonic-gate 		 * Print 1, 5, and 15 minute load averages.
3030Sstevel@tonic-gate 		 */
3040Sstevel@tonic-gate 		(void) getloadavg(loadavg, 3);
3050Sstevel@tonic-gate 		PRINTF((gettext(",  load average: %.2f, %.2f, %.2f\n"),
3060Sstevel@tonic-gate 		    loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
3070Sstevel@tonic-gate 		    loadavg[LOADAVG_15MIN]));
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		if (firstchar == 'u')	/* uptime command */
3100Sstevel@tonic-gate 			exit(0);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		if (lflag) {
3130Sstevel@tonic-gate 			PRINTF((dcgettext(NULL, "User     tty           "
3140Sstevel@tonic-gate 			    "login@  idle   JCPU   PCPU  what\n", LC_TIME)));
3150Sstevel@tonic-gate 		} else {
3160Sstevel@tonic-gate 			PRINTF((dcgettext(NULL,
3170Sstevel@tonic-gate 			    "User     tty           idle   what\n", LC_TIME)));
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		if (fflush(stdout) == EOF) {
3210Sstevel@tonic-gate 			perror((gettext("%s: fflush failed\n"), prog));
3220Sstevel@tonic-gate 			exit(1);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * loop through /proc, reading info about each process
3280Sstevel@tonic-gate 	 * and build the parent/child tree
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	if (!(dirp = opendir(PROCDIR))) {
3310Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
332*10478SSumanth.Naropanth@Sun.COM 		    prog, PROCDIR, strerror(errno));
3330Sstevel@tonic-gate 		exit(1);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3370Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
3380Sstevel@tonic-gate 			continue;
3390Sstevel@tonic-gate retry:
3400Sstevel@tonic-gate 		(void) sprintf(pname, "%s/%s/", PROCDIR, dp->d_name);
3410Sstevel@tonic-gate 		fname = pname + strlen(pname);
3420Sstevel@tonic-gate 		(void) strcpy(fname, "psinfo");
3430Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0)
3440Sstevel@tonic-gate 			continue;
3450Sstevel@tonic-gate 		if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
3460Sstevel@tonic-gate 			int err = errno;
3470Sstevel@tonic-gate 			(void) close(procfd);
3480Sstevel@tonic-gate 			if (err == EAGAIN)
3490Sstevel@tonic-gate 				goto retry;
3500Sstevel@tonic-gate 			if (err != ENOENT)
3510Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
3520Sstevel@tonic-gate 				    "%s: read() failed on %s: %s \n"),
3530Sstevel@tonic-gate 				    prog, pname, strerror(err));
3540Sstevel@tonic-gate 			continue;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 		(void) close(procfd);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		up = findhash(info.pr_pid);
3590Sstevel@tonic-gate 		up->p_ttyd = info.pr_ttydev;
3600Sstevel@tonic-gate 		up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
3610Sstevel@tonic-gate 		up->p_time = 0;
3620Sstevel@tonic-gate 		up->p_ctime = 0;
3630Sstevel@tonic-gate 		up->p_igintr = 0;
3640Sstevel@tonic-gate 		(void) strncpy(up->p_comm, info.pr_fname,
3650Sstevel@tonic-gate 		    sizeof (info.pr_fname));
3660Sstevel@tonic-gate 		up->p_args[0] = 0;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		if (up->p_state != NONE && up->p_state != ZOMBIE) {
3690Sstevel@tonic-gate 			(void) strcpy(fname, "status");
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
3720Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
3770Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 			if (procfd < 0)
3800Sstevel@tonic-gate 				continue;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 			if (read(procfd, &statinfo, sizeof (statinfo))
3830Sstevel@tonic-gate 			    != sizeof (statinfo)) {
3840Sstevel@tonic-gate 				int err = errno;
3850Sstevel@tonic-gate 				(void) close(procfd);
3860Sstevel@tonic-gate 				if (err == EAGAIN)
3870Sstevel@tonic-gate 					goto retry;
3880Sstevel@tonic-gate 				if (err != ENOENT)
3890Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3900Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
3910Sstevel@tonic-gate 					    prog, pname, strerror(err));
3920Sstevel@tonic-gate 				continue;
3930Sstevel@tonic-gate 			}
3940Sstevel@tonic-gate 			(void) close(procfd);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 			up->p_time = statinfo.pr_utime.tv_sec +
3970Sstevel@tonic-gate 			    statinfo.pr_stime.tv_sec;	/* seconds */
3980Sstevel@tonic-gate 			up->p_ctime = statinfo.pr_cutime.tv_sec +
3990Sstevel@tonic-gate 			    statinfo.pr_cstime.tv_sec;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 			(void) strcpy(fname, "sigact");
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 			/* now we need the proc_owner privilege */
4040Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_ON);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 			procfd = open(pname, O_RDONLY);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 			/* drop proc_owner privilege after open */
4090Sstevel@tonic-gate 			(void) __priv_bracket(PRIV_OFF);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 			if (procfd < 0)
4120Sstevel@tonic-gate 				continue;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 			if (read(procfd, actinfo, sizeof (actinfo))
4150Sstevel@tonic-gate 			    != sizeof (actinfo)) {
4160Sstevel@tonic-gate 				int err = errno;
4170Sstevel@tonic-gate 				(void) close(procfd);
4180Sstevel@tonic-gate 				if (err == EAGAIN)
4190Sstevel@tonic-gate 					goto retry;
4200Sstevel@tonic-gate 				if (err != ENOENT)
4210Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4220Sstevel@tonic-gate 					    "%s: read() failed on %s: %s \n"),
4230Sstevel@tonic-gate 					    prog, pname, strerror(err));
4240Sstevel@tonic-gate 				continue;
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 			(void) close(procfd);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 			up->p_igintr =
429*10478SSumanth.Naropanth@Sun.COM 			    actinfo[SIGINT-1].sa_handler == SIG_IGN &&
430*10478SSumanth.Naropanth@Sun.COM 			    actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 			/*
4330Sstevel@tonic-gate 			 * Process args.
4340Sstevel@tonic-gate 			 */
4350Sstevel@tonic-gate 			up->p_args[0] = 0;
4360Sstevel@tonic-gate 			clnarglist(info.pr_psargs);
4370Sstevel@tonic-gate 			(void) strcat(up->p_args, info.pr_psargs);
4380Sstevel@tonic-gate 			if (up->p_args[0] == 0 ||
4390Sstevel@tonic-gate 			    up->p_args[0] == '-' && up->p_args[1] <= ' ' ||
4400Sstevel@tonic-gate 			    up->p_args[0] == '?') {
4410Sstevel@tonic-gate 				(void) strcat(up->p_args, " (");
4420Sstevel@tonic-gate 				(void) strcat(up->p_args, up->p_comm);
4430Sstevel@tonic-gate 				(void) strcat(up->p_args, ")");
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		/*
4480Sstevel@tonic-gate 		 * link pgrp together in case parents go away
4490Sstevel@tonic-gate 		 * Pgrp chain is a single linked list originating
4500Sstevel@tonic-gate 		 * from the pgrp leader to its group member.
4510Sstevel@tonic-gate 		 */
4520Sstevel@tonic-gate 		if (info.pr_pgid != info.pr_pid) {	/* not pgrp leader */
4530Sstevel@tonic-gate 			pgrp = findhash(info.pr_pgid);
4540Sstevel@tonic-gate 			up->p_pgrpl = pgrp->p_pgrpl;
4550Sstevel@tonic-gate 			pgrp->p_pgrpl = up;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		parent = findhash(info.pr_ppid);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		/* if this is the new member, link it in */
4600Sstevel@tonic-gate 		if (parent->p_upid != INITPROCESS) {
4610Sstevel@tonic-gate 			if (parent->p_child) {
4620Sstevel@tonic-gate 				up->p_sibling = parent->p_child;
4630Sstevel@tonic-gate 				up->p_child = 0;
4640Sstevel@tonic-gate 			}
4650Sstevel@tonic-gate 			parent->p_child = up;
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/* revert to non-privileged user after opening */
4700Sstevel@tonic-gate 	(void) __priv_relinquish();
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	(void) closedir(dirp);
4730Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
4770Sstevel@tonic-gate 	 * about each logged in user
4780Sstevel@tonic-gate 	 */
4790Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
4800Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
4810Sstevel@tonic-gate 			continue;
4820Sstevel@tonic-gate 		if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
4830Sstevel@tonic-gate 			continue;	/* we're looking for somebody else */
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		/* print login name of the user */
4860Sstevel@tonic-gate 		PRINTF(("%-*.*s ", NMAX, NMAX, ut->ut_name));
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		/* print tty user is on */
4890Sstevel@tonic-gate 		if (lflag) {
4900Sstevel@tonic-gate 			PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));
4910Sstevel@tonic-gate 		} else {
4920Sstevel@tonic-gate 			if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
4930Sstevel@tonic-gate 			    ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
4940Sstevel@tonic-gate 				PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
4950Sstevel@tonic-gate 			} else {
4960Sstevel@tonic-gate 				PRINTF(("%-*.*s", LMAX, LMAX, ut->ut_line));
4970Sstevel@tonic-gate 			}
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		/* print when the user logged in */
5010Sstevel@tonic-gate 		if (lflag) {
5020Sstevel@tonic-gate 			time_t tim = ut->ut_xtime;
5030Sstevel@tonic-gate 			prtat(&tim);
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		/* print idle time */
5070Sstevel@tonic-gate 		idle = findidle(ut->ut_line);
5080Sstevel@tonic-gate 		if (idle >= 36 * 60) {
5090Sstevel@tonic-gate 			PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
5100Sstevel@tonic-gate 			    (idle + 12 * 60) / (24 * 60)));
5110Sstevel@tonic-gate 		} else
5120Sstevel@tonic-gate 			prttime(idle, " ");
5130Sstevel@tonic-gate 		showtotals(findhash(ut->ut_pid));
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 	if (fclose(stdout) == EOF) {
5160Sstevel@tonic-gate 		perror((gettext("%s: fclose failed"), prog));
5170Sstevel@tonic-gate 		exit(1);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	return (0);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  *  Prints the CPU time for all processes & children,
5240Sstevel@tonic-gate  *  and the cpu time for interesting process,
5250Sstevel@tonic-gate  *  and what the user is doing.
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate static void
showtotals(struct uproc * up)5280Sstevel@tonic-gate showtotals(struct uproc *up)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	jobtime = 0;
5310Sstevel@tonic-gate 	proctime = 0;
5320Sstevel@tonic-gate 	empty = 1;
5330Sstevel@tonic-gate 	curpid = -1;
5340Sstevel@tonic-gate 	add_times = 1;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	calctotals(up);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (lflag) {
5390Sstevel@tonic-gate 		/* print CPU time for all processes & children */
5400Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
5410Sstevel@tonic-gate 		prttime((time_t)jobtime, " ");
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 		/* print cpu time for interesting process */
5440Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
5450Sstevel@tonic-gate 		prttime((time_t)proctime, " ");
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	/* what user is doing, current process */
5480Sstevel@tonic-gate 	PRINTF((" %-.32s\n", doing));
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  *  This recursive routine descends the process
5530Sstevel@tonic-gate  *  tree starting from the given process pointer(up).
5540Sstevel@tonic-gate  *  It used depth-first search strategy and also marked
5550Sstevel@tonic-gate  *  each node as visited as it traversed down the tree.
5560Sstevel@tonic-gate  *  It calculates the process time for all processes &
5570Sstevel@tonic-gate  *  children.  It also finds the interesting process
5580Sstevel@tonic-gate  *  and determines its cpu time and command.
5590Sstevel@tonic-gate  */
5600Sstevel@tonic-gate static void
calctotals(struct uproc * up)5610Sstevel@tonic-gate calctotals(struct uproc *up)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	struct uproc   *zp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Once a node has been visited, stop adding cpu times
5670Sstevel@tonic-gate 	 * for its children so they don't get totalled twice.
5680Sstevel@tonic-gate 	 * Still look for the interesting job for this utmp
5690Sstevel@tonic-gate 	 * entry, however.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	if (up->p_state == VISITED)
5720Sstevel@tonic-gate 		add_times = 0;
5730Sstevel@tonic-gate 	up->p_state = VISITED;
5740Sstevel@tonic-gate 	if (up->p_state == NONE || up->p_state == ZOMBIE)
5750Sstevel@tonic-gate 		return;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (empty && !up->p_igintr) {
5780Sstevel@tonic-gate 		empty = 0;
5790Sstevel@tonic-gate 		curpid = -1;
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if (up->p_upid > curpid && (!up->p_igintr || empty)) {
5830Sstevel@tonic-gate 		curpid = up->p_upid;
5840Sstevel@tonic-gate 		if (lflag)
5850Sstevel@tonic-gate 			(void) strcpy(doing, up->p_args);
5860Sstevel@tonic-gate 		else
5870Sstevel@tonic-gate 			(void) strcpy(doing, up->p_comm);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (add_times == 1) {
5910Sstevel@tonic-gate 		jobtime += up->p_time + up->p_ctime;
5920Sstevel@tonic-gate 		proctime += up->p_time;
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/* descend for its children */
5960Sstevel@tonic-gate 	if (up->p_child) {
5970Sstevel@tonic-gate 		calctotals(up->p_child);
5980Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
5990Sstevel@tonic-gate 			calctotals(zp);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  *   Findhash  finds the appropriate entry in the process
6050Sstevel@tonic-gate  *   hash table (pr_htbl) for the given pid in case that
6060Sstevel@tonic-gate  *   pid exists on the hash chain. It returns back a pointer
6070Sstevel@tonic-gate  *   to that uproc structure. If this is a new pid, it allocates
6080Sstevel@tonic-gate  *   a new node, initializes it, links it into the chain (after
6090Sstevel@tonic-gate  *   head) and returns a structure pointer.
6100Sstevel@tonic-gate  */
6110Sstevel@tonic-gate static struct uproc *
findhash(pid_t pid)6120Sstevel@tonic-gate findhash(pid_t pid)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	struct uproc *up, *tp;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	tp = up = &pr_htbl[pid % HSIZE];
6170Sstevel@tonic-gate 	if (up->p_upid == 0) {			/* empty slot */
6180Sstevel@tonic-gate 		up->p_upid = pid;
6190Sstevel@tonic-gate 		up->p_state = NONE;
6200Sstevel@tonic-gate 		up->p_child = up->p_sibling = up->p_pgrpl = up->p_link = 0;
6210Sstevel@tonic-gate 		return (up);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	if (up->p_upid == pid) {		/* found in hash table */
6240Sstevel@tonic-gate 		return (up);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	for (tp = up->p_link; tp; tp = tp->p_link) {	/* follow chain */
6270Sstevel@tonic-gate 		if (tp->p_upid == pid)
6280Sstevel@tonic-gate 			return (tp);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	tp = malloc(sizeof (*tp));		/* add new node */
6310Sstevel@tonic-gate 	if (!tp) {
6320Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of memory!: %s\n"),
633*10478SSumanth.Naropanth@Sun.COM 		    prog, strerror(errno));
6340Sstevel@tonic-gate 		exit(1);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 	(void) memset(tp, 0, sizeof (*tp));
6370Sstevel@tonic-gate 	tp->p_upid = pid;
6380Sstevel@tonic-gate 	tp->p_state = NONE;
6390Sstevel@tonic-gate 	tp->p_child = tp->p_sibling = tp->p_pgrpl = 0;
6400Sstevel@tonic-gate 	tp->p_link = up->p_link;		/* insert after head */
6410Sstevel@tonic-gate 	up->p_link = tp;
6420Sstevel@tonic-gate 	return (tp);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate #define	HR	(60 * 60)
6460Sstevel@tonic-gate #define	DAY	(24 * HR)
6470Sstevel@tonic-gate #define	MON	(30 * DAY)
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * prttime prints a time in hours and minutes or minutes and seconds.
6510Sstevel@tonic-gate  * The character string tail is printed at the end, obvious
6520Sstevel@tonic-gate  * strings to pass are "", " ", or "am".
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate static void
prttime(time_t tim,char * tail)6550Sstevel@tonic-gate prttime(time_t tim, char *tail)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	if (tim >= 60) {
6580Sstevel@tonic-gate 		PRINTF((dcgettext(NULL, "%3d:%02d", LC_TIME),
6590Sstevel@tonic-gate 		    (int)tim/60, (int)tim%60));
6600Sstevel@tonic-gate 	} else if (tim > 0) {
6610Sstevel@tonic-gate 		PRINTF((dcgettext(NULL, "    %2d", LC_TIME), (int)tim));
6620Sstevel@tonic-gate 	} else {
6630Sstevel@tonic-gate 		PRINTF(("      "));
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 	PRINTF(("%s", tail));
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * prints a 12 hour time given a pointer to a time of day
6700Sstevel@tonic-gate  */
6710Sstevel@tonic-gate static void
prtat(time_t * time)6720Sstevel@tonic-gate prtat(time_t *time)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	struct tm	*p;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	p = localtime(time);
6770Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
6780Sstevel@tonic-gate 		char timestr[50];
6790Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
6800Sstevel@tonic-gate 		    dcgettext(NULL, "%l:%M""%p", LC_TIME), p);
6810Sstevel@tonic-gate 		checkampm(timestr);
6820Sstevel@tonic-gate 		PRINTF((" %s", timestr));
6830Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
6840Sstevel@tonic-gate 		char weekdaytime[20];
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
6870Sstevel@tonic-gate 		    dcgettext(NULL, "%a%l%p", LC_TIME), p);
6880Sstevel@tonic-gate 		checkampm(weekdaytime);
6890Sstevel@tonic-gate 		PRINTF((" %s", weekdaytime));
6900Sstevel@tonic-gate 	} else {
6910Sstevel@tonic-gate 		char monthtime[20];
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
6940Sstevel@tonic-gate 		    dcgettext(NULL, "%e%b%y", LC_TIME), p);
6950Sstevel@tonic-gate 		PRINTF((" %s", monthtime));
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
7010Sstevel@tonic-gate  */
7020Sstevel@tonic-gate static time_t
findidle(char * devname)7030Sstevel@tonic-gate findidle(char *devname)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	struct stat stbuf;
7060Sstevel@tonic-gate 	time_t lastaction, diff;
7070Sstevel@tonic-gate 	char ttyname[64];
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	(void) strcpy(ttyname, "/dev/");
7100Sstevel@tonic-gate 	(void) strcat(ttyname, devname);
7110Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
7120Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
7130Sstevel@tonic-gate 		diff = now - lastaction;
7140Sstevel@tonic-gate 		diff = DIV60(diff);
7150Sstevel@tonic-gate 		if (diff < 0)
7160Sstevel@tonic-gate 			diff = 0;
7170Sstevel@tonic-gate 	} else
7180Sstevel@tonic-gate 		diff = 0;
7190Sstevel@tonic-gate 	return (diff);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate /*
7230Sstevel@tonic-gate  * given a pointer to the argument string get rid of unsavory characters.
7240Sstevel@tonic-gate  */
7250Sstevel@tonic-gate static void
clnarglist(char * arglist)7260Sstevel@tonic-gate clnarglist(char *arglist)
7270Sstevel@tonic-gate {
7280Sstevel@tonic-gate 	char	*c;
7290Sstevel@tonic-gate 	int 	err = 0;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/* get rid of unsavory characters */
7320Sstevel@tonic-gate 	for (c = arglist; *c != NULL; c++) {
7330Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
7340Sstevel@tonic-gate 			if (err++ > 5) {
7350Sstevel@tonic-gate 				*arglist = NULL;
7360Sstevel@tonic-gate 				break;
7370Sstevel@tonic-gate 			}
7380Sstevel@tonic-gate 			*c = '?';
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /* replaces all occurences of AM/PM with am/pm */
7440Sstevel@tonic-gate static void
checkampm(char * str)7450Sstevel@tonic-gate checkampm(char *str)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate 	char *ampm;
7480Sstevel@tonic-gate 	while ((ampm = strstr(str, "AM")) != NULL ||
7490Sstevel@tonic-gate 	    (ampm = strstr(str, "PM")) != NULL) {
7500Sstevel@tonic-gate 		*ampm = tolower(*ampm);
7510Sstevel@tonic-gate 		*(ampm+1) = tolower(*(ampm+1));
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate }
754