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 whodo 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 * Maintenance note:
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * Much of this code is replicated in w.c. If you're
470Sstevel@tonic-gate * fixing bugs here, then you should probably fix 'em there too.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <ctype.h>
540Sstevel@tonic-gate #include <fcntl.h>
550Sstevel@tonic-gate #include <time.h>
560Sstevel@tonic-gate #include <errno.h>
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <utmpx.h>
590Sstevel@tonic-gate #include <sys/utsname.h>
600Sstevel@tonic-gate #include <sys/stat.h>
610Sstevel@tonic-gate #include <sys/mkdev.h>
620Sstevel@tonic-gate #include <dirent.h>
630Sstevel@tonic-gate #include <procfs.h> /* /proc header file */
640Sstevel@tonic-gate #include <sys/wait.h>
650Sstevel@tonic-gate #include <locale.h>
660Sstevel@tonic-gate #include <unistd.h>
670Sstevel@tonic-gate #include <limits.h>
680Sstevel@tonic-gate #include <priv_utils.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * utmpx defines wider fields for user and line. For compatibility of output,
720Sstevel@tonic-gate * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
730Sstevel@tonic-gate * to use the full lengths.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate #ifndef UTMPX_NAMELEN
760Sstevel@tonic-gate /* XXX - utmp - fix name length */
770Sstevel@tonic-gate #define NMAX (_POSIX_LOGIN_NAME_MAX - 1)
780Sstevel@tonic-gate #define LMAX 12
790Sstevel@tonic-gate #else /* UTMPX_NAMELEN */
800Sstevel@tonic-gate static struct utmpx dummy;
810Sstevel@tonic-gate #define NMAX (sizeof (dummy.ut_user))
820Sstevel@tonic-gate #define LMAX (sizeof (dummy.ut_line))
830Sstevel@tonic-gate #endif /* UTMPX_NAMELEN */
840Sstevel@tonic-gate
850Sstevel@tonic-gate #define DIV60(t) ((t+30)/60) /* x/60 rounded */
860Sstevel@tonic-gate
870Sstevel@tonic-gate #ifdef ERR
880Sstevel@tonic-gate #undef ERR
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate #define ERR (-1)
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define DEVNAMELEN 14
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
1010Sstevel@tonic-gate static int ndevs; /* number of configured devices */
1020Sstevel@tonic-gate static int maxdev; /* slots for configured devices */
1030Sstevel@tonic-gate #define DNINCR 100
1040Sstevel@tonic-gate static struct devl { /* device list */
1050Sstevel@tonic-gate char dname[DEVNAMELEN]; /* device name */
1060Sstevel@tonic-gate dev_t ddev; /* device number */
1070Sstevel@tonic-gate } *devl;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate struct uproc {
1100Sstevel@tonic-gate pid_t p_upid; /* user process id */
1110Sstevel@tonic-gate char p_state; /* numeric value of process state */
1120Sstevel@tonic-gate dev_t p_ttyd; /* controlling tty of process */
1130Sstevel@tonic-gate time_t p_time; /* ticks of user & system time */
1140Sstevel@tonic-gate time_t p_ctime; /* ticks of child user & system time */
1150Sstevel@tonic-gate int p_igintr; /* 1=ignores SIGQUIT and SIGINT */
1160Sstevel@tonic-gate char p_comm[PRARGSZ+1]; /* command */
1170Sstevel@tonic-gate char p_args[PRARGSZ+1]; /* command line arguments */
1180Sstevel@tonic-gate struct uproc *p_child, /* first child pointer */
1190Sstevel@tonic-gate *p_sibling, /* sibling pointer */
1200Sstevel@tonic-gate *p_pgrplink, /* pgrp link */
1210Sstevel@tonic-gate *p_link; /* hash table chain pointer */
1220Sstevel@tonic-gate };
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * define hash table for struct uproc
1260Sstevel@tonic-gate * Hash function uses process id
1270Sstevel@tonic-gate * and the size of the hash table(HSIZE)
1280Sstevel@tonic-gate * to determine process index into the table.
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate static struct uproc pr_htbl[HSIZE];
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate static struct uproc *findhash(pid_t);
1330Sstevel@tonic-gate static time_t findidle(char *);
1340Sstevel@tonic-gate static void clnarglist(char *);
1350Sstevel@tonic-gate static void showproc(struct uproc *);
1360Sstevel@tonic-gate static void showtotals(struct uproc *);
1370Sstevel@tonic-gate static void calctotals(struct uproc *);
1380Sstevel@tonic-gate static char *getty(dev_t);
1390Sstevel@tonic-gate static void prttime(time_t, char *);
1400Sstevel@tonic-gate static void prtat(time_t *);
1410Sstevel@tonic-gate static void checkampm(char *);
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate static char *prog;
1440Sstevel@tonic-gate static int header = 1; /* true if -h flag: don't print heading */
1450Sstevel@tonic-gate static int lflag = 0; /* true if -l flag: w command format */
1460Sstevel@tonic-gate static char *sel_user; /* login of particular user selected */
1470Sstevel@tonic-gate static time_t now; /* current time of day */
1480Sstevel@tonic-gate static time_t uptime; /* time of last reboot & elapsed time since */
1490Sstevel@tonic-gate static int nusers; /* number of users logged in now */
1500Sstevel@tonic-gate static time_t idle; /* number of minutes user is idle */
1510Sstevel@tonic-gate static time_t jobtime; /* total cpu time visible */
1520Sstevel@tonic-gate static char doing[520]; /* process attached to terminal */
1530Sstevel@tonic-gate static time_t proctime; /* cpu time of process in doing */
1540Sstevel@tonic-gate static int empty;
1550Sstevel@tonic-gate static pid_t curpid;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate #if SIGQUIT > SIGINT
1580Sstevel@tonic-gate #define ACTSIZE SIGQUIT
1590Sstevel@tonic-gate #else
1600Sstevel@tonic-gate #define ACTSIZE SIGINT
1610Sstevel@tonic-gate #endif
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate int
main(int argc,char * argv[])1640Sstevel@tonic-gate main(int argc, char *argv[])
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate struct utmpx *ut;
1670Sstevel@tonic-gate struct utmpx *utmpbegin;
1680Sstevel@tonic-gate struct utmpx *utmpend;
1690Sstevel@tonic-gate struct utmpx *utp;
1700Sstevel@tonic-gate struct tm *tm;
1710Sstevel@tonic-gate struct uproc *up, *parent, *pgrp;
1720Sstevel@tonic-gate struct psinfo info;
1730Sstevel@tonic-gate struct sigaction actinfo[ACTSIZE];
1740Sstevel@tonic-gate struct pstatus statinfo;
1750Sstevel@tonic-gate size_t size;
1760Sstevel@tonic-gate struct stat sbuf;
1770Sstevel@tonic-gate struct utsname uts;
1780Sstevel@tonic-gate DIR *dirp;
1790Sstevel@tonic-gate struct dirent *dp;
1800Sstevel@tonic-gate char pname[64];
1810Sstevel@tonic-gate char *fname;
1820Sstevel@tonic-gate int procfd;
1830Sstevel@tonic-gate int i;
1840Sstevel@tonic-gate int days, hrs, mins;
1850Sstevel@tonic-gate int entries;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * This program needs the proc_owner privilege
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate (void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER,
1910Sstevel@tonic-gate (char *)NULL);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1940Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1950Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1960Sstevel@tonic-gate #endif
1970Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate prog = argv[0];
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate while (argc > 1) {
2020Sstevel@tonic-gate if (argv[1][0] == '-') {
2030Sstevel@tonic-gate for (i = 1; argv[1][i]; i++) {
2040Sstevel@tonic-gate switch (argv[1][i]) {
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate case 'h':
2070Sstevel@tonic-gate header = 0;
2080Sstevel@tonic-gate break;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate case 'l':
2110Sstevel@tonic-gate lflag++;
2120Sstevel@tonic-gate break;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate default:
2150Sstevel@tonic-gate (void) printf(gettext(
2160Sstevel@tonic-gate "usage: %s [ -hl ] [ user ]\n"),
2170Sstevel@tonic-gate prog);
2180Sstevel@tonic-gate exit(1);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate } else {
2220Sstevel@tonic-gate if (!isalnum(argv[1][0]) || argc > 2) {
2230Sstevel@tonic-gate (void) printf(gettext(
2240Sstevel@tonic-gate "usage: %s [ -hl ] [ user ]\n"), prog);
2250Sstevel@tonic-gate exit(1);
2260Sstevel@tonic-gate } else
2270Sstevel@tonic-gate sel_user = argv[1];
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate argc--; argv++;
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * read the UTMPX_FILE (contains information about
2340Sstevel@tonic-gate * each logged in user)
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate if (stat(UTMPX_FILE, &sbuf) == ERR) {
2370Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
238*10478SSumanth.Naropanth@Sun.COM prog, UTMPX_FILE, strerror(errno));
2390Sstevel@tonic-gate exit(1);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate entries = sbuf.st_size / sizeof (struct futmpx);
2420Sstevel@tonic-gate size = sizeof (struct utmpx) * entries;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if ((ut = malloc(size)) == NULL) {
2450Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
246*10478SSumanth.Naropanth@Sun.COM prog, UTMPX_FILE, strerror(errno));
2470Sstevel@tonic-gate exit(1);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate (void) utmpxname(UTMPX_FILE);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate utmpbegin = ut;
2530Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
2540Sstevel@tonic-gate utmpend = (struct utmpx *)((char *)utmpbegin + size);
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate setutxent();
257*10478SSumanth.Naropanth@Sun.COM while ((ut < utmpend) && ((utp = getutxent()) != NULL))
2580Sstevel@tonic-gate (void) memcpy(ut++, utp, sizeof (*ut));
2590Sstevel@tonic-gate endutxent();
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate (void) time(&now); /* get current time */
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate if (header) { /* print a header */
2640Sstevel@tonic-gate if (lflag) { /* w command format header */
2650Sstevel@tonic-gate prtat(&now);
2660Sstevel@tonic-gate for (ut = utmpbegin; ut < utmpend; ut++) {
2670Sstevel@tonic-gate if (ut->ut_type == USER_PROCESS) {
2680Sstevel@tonic-gate nusers++;
2690Sstevel@tonic-gate } else if (ut->ut_type == BOOT_TIME) {
2700Sstevel@tonic-gate uptime = now - ut->ut_xtime;
2710Sstevel@tonic-gate uptime += 30;
2720Sstevel@tonic-gate days = uptime / (60*60*24);
2730Sstevel@tonic-gate uptime %= (60*60*24);
2740Sstevel@tonic-gate hrs = uptime / (60*60);
2750Sstevel@tonic-gate uptime %= (60*60);
2760Sstevel@tonic-gate mins = uptime / 60;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate (void) printf(dcgettext(NULL,
2790Sstevel@tonic-gate " up %d day(s), %d hr(s), "
2800Sstevel@tonic-gate "%d min(s)", LC_TIME),
2810Sstevel@tonic-gate days, hrs, mins);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate ut = utmpbegin; /* rewind utmp data */
2860Sstevel@tonic-gate (void) printf(dcgettext(NULL,
2870Sstevel@tonic-gate " %d user(s)\n", LC_TIME), nusers);
2880Sstevel@tonic-gate (void) printf(dcgettext(NULL, "User tty "
2890Sstevel@tonic-gate "login@ idle JCPU PCPU what\n", LC_TIME));
2900Sstevel@tonic-gate } else { /* standard whodo header */
2910Sstevel@tonic-gate char date_buf[100];
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate * print current time and date
2950Sstevel@tonic-gate */
2960Sstevel@tonic-gate (void) strftime(date_buf, sizeof (date_buf),
2970Sstevel@tonic-gate dcgettext(NULL, "%C", LC_TIME), localtime(&now));
2980Sstevel@tonic-gate (void) printf("%s\n", date_buf);
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * print system name
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate (void) uname(&uts);
3040Sstevel@tonic-gate (void) printf("%s\n", uts.nodename);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * loop through /proc, reading info about each process
3100Sstevel@tonic-gate * and build the parent/child tree
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate if (!(dirp = opendir(PROCDIR))) {
3130Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: could not open %s: %s\n"),
314*10478SSumanth.Naropanth@Sun.COM prog, PROCDIR, strerror(errno));
3150Sstevel@tonic-gate exit(1);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
3190Sstevel@tonic-gate if (dp->d_name[0] == '.')
3200Sstevel@tonic-gate continue;
3210Sstevel@tonic-gate retry:
3220Sstevel@tonic-gate (void) snprintf(pname, sizeof (pname),
323*10478SSumanth.Naropanth@Sun.COM "%s/%s/", PROCDIR, dp->d_name);
3240Sstevel@tonic-gate fname = pname + strlen(pname);
3250Sstevel@tonic-gate (void) strcpy(fname, "psinfo");
3260Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0)
3270Sstevel@tonic-gate continue;
3280Sstevel@tonic-gate if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
3290Sstevel@tonic-gate int err = errno;
3300Sstevel@tonic-gate (void) close(procfd);
3310Sstevel@tonic-gate if (err == EAGAIN)
3320Sstevel@tonic-gate goto retry;
3330Sstevel@tonic-gate if (err != ENOENT)
3340Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3350Sstevel@tonic-gate "%s: read() failed on %s: %s\n"),
3360Sstevel@tonic-gate prog, pname, strerror(err));
3370Sstevel@tonic-gate continue;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate (void) close(procfd);
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate up = findhash(info.pr_pid);
3420Sstevel@tonic-gate up->p_ttyd = info.pr_ttydev;
3430Sstevel@tonic-gate up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
3440Sstevel@tonic-gate up->p_time = 0;
3450Sstevel@tonic-gate up->p_ctime = 0;
3460Sstevel@tonic-gate up->p_igintr = 0;
3470Sstevel@tonic-gate (void) strncpy(up->p_comm, info.pr_fname,
3480Sstevel@tonic-gate sizeof (info.pr_fname));
3490Sstevel@tonic-gate up->p_args[0] = 0;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if (up->p_state != NONE && up->p_state != ZOMBIE) {
3520Sstevel@tonic-gate (void) strcpy(fname, "status");
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /* now we need the proc_owner privilege */
3550Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON);
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate procfd = open(pname, O_RDONLY);
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /* drop proc_owner privilege after open */
3600Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF);
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate if (procfd < 0)
3630Sstevel@tonic-gate continue;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if (read(procfd, &statinfo, sizeof (statinfo))
3660Sstevel@tonic-gate != sizeof (statinfo)) {
3670Sstevel@tonic-gate int err = errno;
3680Sstevel@tonic-gate (void) close(procfd);
3690Sstevel@tonic-gate if (err == EAGAIN)
3700Sstevel@tonic-gate goto retry;
3710Sstevel@tonic-gate if (err != ENOENT)
3720Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3730Sstevel@tonic-gate "%s: read() failed on %s: %s \n"),
3740Sstevel@tonic-gate prog, pname, strerror(err));
3750Sstevel@tonic-gate continue;
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate (void) close(procfd);
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate up->p_time = statinfo.pr_utime.tv_sec +
3800Sstevel@tonic-gate statinfo.pr_stime.tv_sec;
3810Sstevel@tonic-gate up->p_ctime = statinfo.pr_cutime.tv_sec +
3820Sstevel@tonic-gate statinfo.pr_cstime.tv_sec;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate (void) strcpy(fname, "sigact");
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate /* now we need the proc_owner privilege */
3870Sstevel@tonic-gate (void) __priv_bracket(PRIV_ON);
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate procfd = open(pname, O_RDONLY);
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate /* drop proc_owner privilege after open */
3920Sstevel@tonic-gate (void) __priv_bracket(PRIV_OFF);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (procfd < 0)
3950Sstevel@tonic-gate continue;
3960Sstevel@tonic-gate if (read(procfd, actinfo, sizeof (actinfo))
3970Sstevel@tonic-gate != sizeof (actinfo)) {
3980Sstevel@tonic-gate int err = errno;
3990Sstevel@tonic-gate (void) close(procfd);
4000Sstevel@tonic-gate if (err == EAGAIN)
4010Sstevel@tonic-gate goto retry;
4020Sstevel@tonic-gate if (err != ENOENT)
4030Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4040Sstevel@tonic-gate "%s: read() failed on %s: %s \n"),
4050Sstevel@tonic-gate prog, pname, strerror(err));
4060Sstevel@tonic-gate continue;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate (void) close(procfd);
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate up->p_igintr =
411*10478SSumanth.Naropanth@Sun.COM actinfo[SIGINT-1].sa_handler == SIG_IGN &&
412*10478SSumanth.Naropanth@Sun.COM actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate up->p_args[0] = 0;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate * Process args if there's a chance we'll print it.
4180Sstevel@tonic-gate */
4190Sstevel@tonic-gate if (lflag) { /* w command needs args */
4200Sstevel@tonic-gate clnarglist(info.pr_psargs);
4210Sstevel@tonic-gate (void) strcpy(up->p_args, info.pr_psargs);
4220Sstevel@tonic-gate if (up->p_args[0] == 0 ||
4230Sstevel@tonic-gate up->p_args[0] == '-' &&
4240Sstevel@tonic-gate up->p_args[1] <= ' ' ||
4250Sstevel@tonic-gate up->p_args[0] == '?') {
4260Sstevel@tonic-gate (void) strcat(up->p_args, " (");
4270Sstevel@tonic-gate (void) strcat(up->p_args, up->p_comm);
4280Sstevel@tonic-gate (void) strcat(up->p_args, ")");
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate * link pgrp together in case parents go away
4360Sstevel@tonic-gate * Pgrp chain is a single linked list originating
4370Sstevel@tonic-gate * from the pgrp leader to its group member.
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate if (info.pr_pgid != info.pr_pid) { /* not pgrp leader */
4400Sstevel@tonic-gate pgrp = findhash(info.pr_pgid);
4410Sstevel@tonic-gate up->p_pgrplink = pgrp->p_pgrplink;
4420Sstevel@tonic-gate pgrp->p_pgrplink = up;
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate parent = findhash(info.pr_ppid);
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /* if this is the new member, link it in */
4470Sstevel@tonic-gate if (parent->p_upid != INITPROCESS) {
4480Sstevel@tonic-gate if (parent->p_child) {
4490Sstevel@tonic-gate up->p_sibling = parent->p_child;
4500Sstevel@tonic-gate up->p_child = 0;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate parent->p_child = up;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /* revert to non-privileged user */
4580Sstevel@tonic-gate (void) __priv_relinquish();
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate (void) closedir(dirp);
4610Sstevel@tonic-gate (void) time(&now); /* get current time */
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * loop through utmpx file, printing process info
4650Sstevel@tonic-gate * about each logged in user
4660Sstevel@tonic-gate */
4670Sstevel@tonic-gate for (ut = utmpbegin; ut < utmpend; ut++) {
4680Sstevel@tonic-gate time_t tim;
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate if (ut->ut_type != USER_PROCESS)
4710Sstevel@tonic-gate continue;
4720Sstevel@tonic-gate if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
4730Sstevel@tonic-gate continue; /* we're looking for somebody else */
4740Sstevel@tonic-gate if (lflag) { /* -l flag format (w command) */
4750Sstevel@tonic-gate /* print login name of the user */
4760Sstevel@tonic-gate (void) printf("%-*.*s ", NMAX, NMAX, ut->ut_name);
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate /* print tty user is on */
4790Sstevel@tonic-gate (void) printf("%-*.*s", LMAX, LMAX, ut->ut_line);
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate /* print when the user logged in */
4820Sstevel@tonic-gate tim = ut->ut_xtime;
4830Sstevel@tonic-gate (void) prtat(&tim);
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /* print idle time */
4860Sstevel@tonic-gate idle = findidle(ut->ut_line);
4870Sstevel@tonic-gate if (idle >= 36 * 60)
4880Sstevel@tonic-gate (void) printf(dcgettext(NULL, "%2ddays ",
4890Sstevel@tonic-gate LC_TIME), (idle + 12 * 60) / (24 * 60));
4900Sstevel@tonic-gate else
4910Sstevel@tonic-gate prttime(idle, " ");
4920Sstevel@tonic-gate showtotals(findhash((pid_t)ut->ut_pid));
4930Sstevel@tonic-gate } else { /* standard whodo format */
4940Sstevel@tonic-gate tim = ut->ut_xtime;
4950Sstevel@tonic-gate tm = localtime(&tim);
4960Sstevel@tonic-gate (void) printf("\n%-*.*s %-*.*s %2.1d:%2.2d\n",
4970Sstevel@tonic-gate LMAX, LMAX, ut->ut_line,
4980Sstevel@tonic-gate NMAX, NMAX, ut->ut_name, tm->tm_hour, tm->tm_min);
4990Sstevel@tonic-gate showproc(findhash((pid_t)ut->ut_pid));
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate return (0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate * Used for standard whodo format.
5080Sstevel@tonic-gate * This is the recursive routine descending the process
5090Sstevel@tonic-gate * tree starting from the given process pointer(up).
5100Sstevel@tonic-gate * It used depth-first search strategy and also marked
5110Sstevel@tonic-gate * each node as printed as it traversed down the tree.
5120Sstevel@tonic-gate */
5130Sstevel@tonic-gate static void
showproc(struct uproc * up)5140Sstevel@tonic-gate showproc(struct uproc *up)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate struct uproc *zp;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate if (up->p_state == VISITED) /* we already been here */
5190Sstevel@tonic-gate return;
5200Sstevel@tonic-gate /* print the data for this process */
5210Sstevel@tonic-gate if (up->p_state == ZOMBIE)
5220Sstevel@tonic-gate (void) printf(" %-*.*s %5d %4.1ld:%2.2ld %s\n",
5230Sstevel@tonic-gate LMAX, LMAX, " ?", (int)up->p_upid, 0L, 0L, "<defunct>");
5240Sstevel@tonic-gate else if (up->p_state != NONE) {
5250Sstevel@tonic-gate (void) printf(" %-*.*s %5d %4.1ld:%2.2ld %s\n",
5260Sstevel@tonic-gate LMAX, LMAX, getty(up->p_ttyd), (int)up->p_upid,
5270Sstevel@tonic-gate up->p_time / 60L, up->p_time % 60L,
5280Sstevel@tonic-gate up->p_comm);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate up->p_state = VISITED;
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /* descend for its children */
5330Sstevel@tonic-gate if (up->p_child) {
5340Sstevel@tonic-gate showproc(up->p_child);
5350Sstevel@tonic-gate for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling) {
5360Sstevel@tonic-gate showproc(zp);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /* print the pgrp relation */
5410Sstevel@tonic-gate if (up->p_pgrplink)
5420Sstevel@tonic-gate showproc(up->p_pgrplink);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate * Used for -l flag (w command) format.
5480Sstevel@tonic-gate * Prints the CPU time for all processes & children,
5490Sstevel@tonic-gate * and the cpu time for interesting process,
5500Sstevel@tonic-gate * and what the user is doing.
5510Sstevel@tonic-gate */
5520Sstevel@tonic-gate static void
showtotals(struct uproc * up)5530Sstevel@tonic-gate showtotals(struct uproc *up)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate jobtime = 0;
5560Sstevel@tonic-gate proctime = 0;
5570Sstevel@tonic-gate empty = 1;
5580Sstevel@tonic-gate curpid = -1;
5590Sstevel@tonic-gate (void) strcpy(doing, "-"); /* default act: normally never prints */
5600Sstevel@tonic-gate calctotals(up);
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate /* print CPU time for all processes & children */
5630Sstevel@tonic-gate /* and need to convert clock ticks to seconds first */
5640Sstevel@tonic-gate prttime((time_t)jobtime, " ");
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate /* print cpu time for interesting process */
5670Sstevel@tonic-gate /* and need to convert clock ticks to seconds first */
5680Sstevel@tonic-gate prttime((time_t)proctime, " ");
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate /* what user is doing, current process */
5710Sstevel@tonic-gate (void) printf(" %-.32s\n", doing);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate * Used for -l flag (w command) format.
5760Sstevel@tonic-gate * This recursive routine descends the process
5770Sstevel@tonic-gate * tree starting from the given process pointer(up).
5780Sstevel@tonic-gate * It used depth-first search strategy and also marked
5790Sstevel@tonic-gate * each node as visited as it traversed down the tree.
5800Sstevel@tonic-gate * It calculates the process time for all processes &
5810Sstevel@tonic-gate * children. It also finds the "interesting" process
5820Sstevel@tonic-gate * and determines its cpu time and command.
5830Sstevel@tonic-gate */
5840Sstevel@tonic-gate static void
calctotals(struct uproc * up)5850Sstevel@tonic-gate calctotals(struct uproc *up)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate struct uproc *zp;
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate if (up->p_state == VISITED)
5900Sstevel@tonic-gate return;
5910Sstevel@tonic-gate up->p_state = VISITED;
5920Sstevel@tonic-gate if (up->p_state == NONE || up->p_state == ZOMBIE)
5930Sstevel@tonic-gate return;
5940Sstevel@tonic-gate jobtime += up->p_time + up->p_ctime;
5950Sstevel@tonic-gate proctime += up->p_time;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate if (empty && !up->p_igintr) {
5980Sstevel@tonic-gate empty = 0;
5990Sstevel@tonic-gate curpid = -1;
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate
6020Sstevel@tonic-gate if (up->p_upid > curpid && (!up->p_igintr || empty)) {
6030Sstevel@tonic-gate curpid = up->p_upid;
6040Sstevel@tonic-gate (void) strcpy(doing, up->p_args);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate /* descend for its children */
6080Sstevel@tonic-gate if (up->p_child) {
6090Sstevel@tonic-gate calctotals(up->p_child);
6100Sstevel@tonic-gate for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
6110Sstevel@tonic-gate calctotals(zp);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate static char *
devadd(char * name,dev_t ddev)6160Sstevel@tonic-gate devadd(char *name, dev_t ddev)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate struct devl *dp;
6190Sstevel@tonic-gate int leng, start, i;
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate if (ndevs == maxdev) {
6220Sstevel@tonic-gate maxdev += DNINCR;
6230Sstevel@tonic-gate dp = realloc(devl, maxdev * sizeof (struct devl));
6240Sstevel@tonic-gate if (!dp) {
6250Sstevel@tonic-gate (void) fprintf(stderr,
626*10478SSumanth.Naropanth@Sun.COM gettext("%s: out of memory!: %s\n"),
627*10478SSumanth.Naropanth@Sun.COM prog, strerror(errno));
6280Sstevel@tonic-gate exit(1);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate devl = dp;
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate dp = &devl[ndevs++];
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate dp->ddev = ddev;
6350Sstevel@tonic-gate if (name == NULL) {
6360Sstevel@tonic-gate (void) strcpy(dp->dname, " ? ");
6370Sstevel@tonic-gate return (dp->dname);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate leng = strlen(name);
6410Sstevel@tonic-gate if (leng < DEVNAMELEN + 4) {
6420Sstevel@tonic-gate /* strip off "/dev/" */
6430Sstevel@tonic-gate (void) strcpy(dp->dname, &name[5]);
6440Sstevel@tonic-gate } else {
6450Sstevel@tonic-gate /* strip enough off the front to fit */
6460Sstevel@tonic-gate start = leng - DEVNAMELEN - 1;
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate for (i = start; i < leng && name[i] != '/'; i++)
6490Sstevel@tonic-gate ;
6500Sstevel@tonic-gate if (i == leng)
6510Sstevel@tonic-gate (void) strncpy(dp->dname, &name[start], DEVNAMELEN);
6520Sstevel@tonic-gate else
6530Sstevel@tonic-gate (void) strncpy(dp->dname, &name[i+1], DEVNAMELEN);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate return (dp->dname);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate static char *
devlookup(dev_t ddev)6590Sstevel@tonic-gate devlookup(dev_t ddev)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate struct devl *dp;
6620Sstevel@tonic-gate int i;
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate for (dp = devl, i = 0; i < ndevs; dp++, i++) {
6650Sstevel@tonic-gate if (dp->ddev == ddev)
6660Sstevel@tonic-gate return (dp->dname);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate return (NULL);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /*
6720Sstevel@tonic-gate * This routine gives back a corresponding device name
6730Sstevel@tonic-gate * from the device number given.
6740Sstevel@tonic-gate */
6750Sstevel@tonic-gate static char *
getty(dev_t dev)6760Sstevel@tonic-gate getty(dev_t dev)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate extern char *_ttyname_dev(dev_t, char *, size_t);
6790Sstevel@tonic-gate char devname[TTYNAME_MAX];
6800Sstevel@tonic-gate char *retval;
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate if (dev == PRNODEV)
6830Sstevel@tonic-gate return (" ? ");
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate if ((retval = devlookup(dev)) != NULL)
6860Sstevel@tonic-gate return (retval);
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate retval = _ttyname_dev(dev, devname, sizeof (devname));
6890Sstevel@tonic-gate return (devadd(retval, dev));
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * Findhash finds the appropriate entry in the process
6940Sstevel@tonic-gate * hash table (pr_htbl) for the given pid in case that
6950Sstevel@tonic-gate * pid exists on the hash chain. It returns back a pointer
6960Sstevel@tonic-gate * to that uproc structure. If this is a new pid, it allocates
6970Sstevel@tonic-gate * a new node, initializes it, links it into the chain (after
6980Sstevel@tonic-gate * head) and returns a structure pointer.
6990Sstevel@tonic-gate */
7000Sstevel@tonic-gate static struct uproc *
findhash(pid_t pid)7010Sstevel@tonic-gate findhash(pid_t pid)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate struct uproc *up, *tp;
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate tp = up = &pr_htbl[(int)pid % HSIZE];
7060Sstevel@tonic-gate if (up->p_upid == 0) { /* empty slot */
7070Sstevel@tonic-gate up->p_upid = pid;
7080Sstevel@tonic-gate up->p_state = NONE;
7090Sstevel@tonic-gate up->p_child = up->p_sibling = up->p_pgrplink = up->p_link = 0;
7100Sstevel@tonic-gate return (up);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate if (up->p_upid == pid) { /* found in hash table */
7130Sstevel@tonic-gate return (up);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate for (tp = up->p_link; tp; tp = tp->p_link) { /* follow chain */
7160Sstevel@tonic-gate if (tp->p_upid == pid) {
7170Sstevel@tonic-gate return (tp);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate tp = malloc(sizeof (*tp)); /* add new node */
7210Sstevel@tonic-gate if (!tp) {
7220Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: out of memory!: %s\n"),
723*10478SSumanth.Naropanth@Sun.COM prog, strerror(errno));
7240Sstevel@tonic-gate exit(1);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate (void) memset((char *)tp, 0, sizeof (*tp));
7270Sstevel@tonic-gate tp->p_upid = pid;
7280Sstevel@tonic-gate tp->p_state = NONE;
7290Sstevel@tonic-gate tp->p_child = tp->p_sibling = tp->p_pgrplink = (pid_t)0;
7300Sstevel@tonic-gate tp->p_link = up->p_link; /* insert after head */
7310Sstevel@tonic-gate up->p_link = tp;
7320Sstevel@tonic-gate return (tp);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate #define HR (60 * 60)
7360Sstevel@tonic-gate #define DAY (24 * HR)
7370Sstevel@tonic-gate #define MON (30 * DAY)
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate * prints a time in hours and minutes or minutes and seconds.
7410Sstevel@tonic-gate * The character string 'tail' is printed at the end, obvious
7420Sstevel@tonic-gate * strings to pass are "", " ", or "am".
7430Sstevel@tonic-gate */
7440Sstevel@tonic-gate static void
prttime(time_t tim,char * tail)7450Sstevel@tonic-gate prttime(time_t tim, char *tail)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate if (tim >= 60)
7480Sstevel@tonic-gate (void) printf(dcgettext(NULL, "%3d:%02d", LC_TIME),
7490Sstevel@tonic-gate (int)tim/60, (int)tim%60);
7500Sstevel@tonic-gate else if (tim > 0)
7510Sstevel@tonic-gate (void) printf(dcgettext(NULL, " %2d", LC_TIME), (int)tim);
7520Sstevel@tonic-gate else
7530Sstevel@tonic-gate (void) printf(" ");
7540Sstevel@tonic-gate (void) printf("%s", tail);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate * prints a 12 hour time given a pointer to a time of day
7600Sstevel@tonic-gate */
7610Sstevel@tonic-gate static void
prtat(time_t * time)7620Sstevel@tonic-gate prtat(time_t *time)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate struct tm *p;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate p = localtime(time);
7670Sstevel@tonic-gate if (now - *time <= 18 * HR) {
7680Sstevel@tonic-gate char timestr[50];
7690Sstevel@tonic-gate (void) strftime(timestr, sizeof (timestr),
7700Sstevel@tonic-gate dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
7710Sstevel@tonic-gate checkampm(timestr);
7720Sstevel@tonic-gate (void) printf("%s", timestr);
7730Sstevel@tonic-gate } else if (now - *time <= 7 * DAY) {
7740Sstevel@tonic-gate char weekdaytime[20];
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate (void) strftime(weekdaytime, sizeof (weekdaytime),
7770Sstevel@tonic-gate dcgettext(NULL, "%a%l%p", LC_TIME), p);
7780Sstevel@tonic-gate checkampm(weekdaytime);
7790Sstevel@tonic-gate (void) printf(" %s", weekdaytime);
7800Sstevel@tonic-gate } else {
7810Sstevel@tonic-gate char monthtime[20];
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate (void) strftime(monthtime, sizeof (monthtime),
7840Sstevel@tonic-gate dcgettext(NULL, "%e%b%y", LC_TIME), p);
7850Sstevel@tonic-gate (void) printf(" %s", monthtime);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate * find & return number of minutes current tty has been idle
7910Sstevel@tonic-gate */
7920Sstevel@tonic-gate static time_t
findidle(char * devname)7930Sstevel@tonic-gate findidle(char *devname)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate struct stat stbuf;
7960Sstevel@tonic-gate time_t lastaction, diff;
7970Sstevel@tonic-gate char ttyname[64];
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate (void) strcpy(ttyname, "/dev/");
8000Sstevel@tonic-gate (void) strcat(ttyname, devname);
8010Sstevel@tonic-gate if (stat(ttyname, &stbuf) != -1) {
8020Sstevel@tonic-gate lastaction = stbuf.st_atime;
8030Sstevel@tonic-gate diff = now - lastaction;
8040Sstevel@tonic-gate diff = DIV60(diff);
8050Sstevel@tonic-gate if (diff < 0)
8060Sstevel@tonic-gate diff = 0;
8070Sstevel@tonic-gate } else
8080Sstevel@tonic-gate diff = 0;
8090Sstevel@tonic-gate return (diff);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate /*
8130Sstevel@tonic-gate * given a pointer to the argument string clean out unsavory characters.
8140Sstevel@tonic-gate */
8150Sstevel@tonic-gate static void
clnarglist(char * arglist)8160Sstevel@tonic-gate clnarglist(char *arglist)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate char *c;
8190Sstevel@tonic-gate int err = 0;
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate /* get rid of unsavory characters */
8220Sstevel@tonic-gate for (c = arglist; *c == NULL; c++) {
8230Sstevel@tonic-gate if ((*c < ' ') || (*c > 0176)) {
8240Sstevel@tonic-gate if (err++ > 5) {
8250Sstevel@tonic-gate *arglist = NULL;
8260Sstevel@tonic-gate break;
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate *c = '?';
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /* replaces all occurences of AM/PM with am/pm */
8340Sstevel@tonic-gate static void
checkampm(char * str)8350Sstevel@tonic-gate checkampm(char *str)
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate char *ampm;
8380Sstevel@tonic-gate while ((ampm = strstr(str, "AM")) != NULL ||
8390Sstevel@tonic-gate (ampm = strstr(str, "PM")) != NULL) {
8400Sstevel@tonic-gate *ampm = tolower(*ampm);
8410Sstevel@tonic-gate *(ampm+1) = tolower(*(ampm+1));
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate }
844