home *** CD-ROM | disk | FTP | other *** search
- /*
- * %W% %E% %U% ncoast!bsa %Z%
- * %Z% Copyright (C) 1985 by Brandon S. Allbery, All Rights Reserved %Z%
- *
- * 8-Jul-88 John F. Haugh II (jfh@rpp386)
- * Major hacks to force to work on SCO Xenix 386
- */
-
- #ifndef lint
- static char _SccsId[] = "%W% %E% %U% ncoast!bsa %Z%";
- static char _CopyRt[] = "%Z% Copyright (C) 1985 by Brandon S. Allbery %Z%";
- #endif lint
-
- #include <stdio.h>
- #include <time.h>
- #include <signal.h>
- #include <a.out.h>
- #include <sys/types.h>
- #include <sys/page.h>
- #include <sys/seg.h>
- #include <sys/param.h>
- #include <sys/var.h>
- #include <sys/proc.h>
- #include <sys/dir.h>
- #include <sys/user.h>
- #include <sys/stat.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sysmacros.h>
- #include <utmp.h>
-
- #define UTMP "/etc/utmp"
- #ifndef KERNEL
- #define KERNEL "/xenix"
- #endif
- #define KMEM "/dev/kmem"
- #define PMEM "/dev/mem"
- #define SMEM "/dev/swap"
-
- #define MIN (60)
- #define HOUR (MIN * 60)
- #define DAY (HOUR * 24)
- #define WEEK (DAY * 7)
- #define MONTH (DAY * 30)
-
- FILE *kfd;
- FILE *mfd;
- FILE *sfd;
- FILE *utmp;
- long nproc;
- char _SObuf[BUFSIZ];
- daddr_t swplo;
- short mypid;
-
- struct xlist kernel[] = {
- {0, 0, 0, "_v"},
- {0, 0, 0, "_proc"},
- {0, 0, 0, "_swplo"},
- {0, 0, 0, "_lbolt"},
- {0, 0, 0, (char *) 0},
- };
-
- char *months[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
- };
-
- struct tm *localtime();
- char *vtime();
- char *uptime();
- char *itoa();
-
- main() {
- struct utmp user;
- short ucnt;
- long now;
- struct var vars;
-
- setbuf(stdout, _SObuf);
- mypid = getpid();
- time(&now);
- if (xlist(KERNEL, kernel) == -1) {
- perror(KERNEL);
- exit(2);
- }
- if ((utmp = fopen(UTMP, "r")) == NULL) {
- perror(UTMP);
- exit(1);
- }
- ucnt = 0;
- while (fread(&user, sizeof user, 1, utmp) > 0) {
- if (user.ut_type != USER_PROCESS)
- continue;
- ucnt++;
- }
- if (kfd == NULL) {
- if ((kfd = fopen(KMEM, "r")) == NULL) {
- perror(KMEM);
- exit(3);
- }
- }
- fseek(kfd, kernel[0].xl_value, 0);
- fread(&vars, sizeof vars, 1, kfd);
- nproc = vars.v_proc;
- fseek(kfd, kernel[2].xl_value, 0);
- fread(&swplo, sizeof swplo, 1, kfd);
- if (mfd == NULL) {
- if ((mfd = fopen(PMEM, "r")) == NULL) {
- perror(PMEM);
- exit(4);
- }
- }
- if (sfd == NULL) {
- if ((sfd = fopen(SMEM, "r")) == NULL) {
- perror(SMEM);
- exit(5);
- }
- }
- printf(" %s up%s, %d users\nUser tty login@ idle JCPU PCPU what\n", vtime(&now), uptime(), ucnt);
- rewind(utmp);
- while (fread(&user, sizeof user, 1, utmp) > 0) {
- if (user.ut_type != USER_PROCESS)
- continue;
- show(&user);
- }
- fclose(utmp);
- exit(0);
- }
-
- char *uptime() {
- static char timebuf[128];
- struct proc swapper;
- struct user bootproc;
- long oldpos, now;
- short cnt, ocnt;
- long boottime = 0L;
-
- oldpos = fseek(kfd, 0L, 1);
- fseek(kfd, kernel[3].xl_value, 0);
- fread(&boottime, sizeof boottime, 1, kfd);
- now = boottime / HZ;
-
- if (now < 0L)
- return " with strange clock time";
- timebuf[0] = '\0';
- ocnt = 0;
- cnt = 0;
- while (now >= MONTH) {
- cnt++;
- now -= MONTH;
- }
- if (cnt > 0) {
- strcat(timebuf, itoa(cnt));
- strcat(timebuf, " mon");
- if (cnt > 1)
- strcat(timebuf, "s");
- if (++ocnt == 2)
- return timebuf;
- }
- cnt = 0;
- while (now >= WEEK) {
- cnt++;
- now -= WEEK;
- }
- if (cnt > 0) {
- strcat(timebuf, itoa(cnt));
- strcat(timebuf, " wk");
- if (cnt > 1)
- strcat(timebuf, "s");
- if (++ocnt == 2)
- return timebuf;
- }
- cnt = 0;
- while (now >= DAY) {
- cnt++;
- now -= DAY;
- }
- if (cnt > 0) {
- strcat(timebuf, itoa(cnt));
- strcat(timebuf, " day");
- if (cnt > 1)
- strcat(timebuf, "s");
- if (++ocnt == 2)
- return timebuf;
- }
- cnt = 0;
- while (now >= HOUR) {
- cnt++;
- now -= HOUR;
- }
- if (cnt > 0) {
- strcat(timebuf, itoa(cnt));
- strcat(timebuf, " hr");
- if (cnt > 1)
- strcat(timebuf, "s");
- }
- return timebuf;
- }
-
- char *itoa(n)
- int n; {
- static char buf[20];
-
- sprintf(buf, " %d", n);
- return buf;
- }
-
- findu (proc, slot, user)
- struct proc *proc;
- int slot;
- struct user *user;
- {
- struct proc *procs = (struct proc *) kernel[1].xl_value;
- long swapaddr;
- int i;
-
- if ((proc->p_flag & (SSWAP|SSPART)) || ! (proc->p_flag & SLOAD)) {
- swapaddr = proc->p_addr[0].te_frameno * NBPC;
- if (fseek (sfd, swapaddr, 0) == -1L)
- fprintf (stderr, "error in lseek\n");
- fread (user, sizeof *user, 1, sfd);
- } else {
- if (fseek (mfd, proc->p_addr[0].te_frameno * NBPC, 0) == -1L)
- fprintf (stderr, "error in lseek\n");
- fread (user, sizeof *user, 1, mfd);
- }
- if (user->u_procp - procs == slot)
- return (1);
- else
- return (0);
- }
-
- show(uinfo)
- struct utmp *uinfo;
- {
- struct stat sbuf;
- struct proc proc;
- struct user prog;
- char ttydev[16];
- long now, cnt, offset, jcpu;
- short mpid, isswap;
- FILE *ufd;
-
- strcpy(ttydev, "/dev/");
- strncpy(&ttydev[5], uinfo->ut_line, 8);
- ttydev[13] = '\0';
- if (stat(ttydev, &sbuf) != 0) {
- perror(ttydev);
- return;
- }
- time(&now);
- now -= sbuf.st_atime;
- printf("%-8.8s %-8.8s %7s ", uinfo->ut_name, uinfo->ut_line, vtime(&uinfo->ut_time));
- if (now > DAY)
- printf("%4dd", now / DAY);
- else if (now > HOUR)
- printf("%2d:%02d", now / HOUR, (now % HOUR) / 60);
- else if (now > MIN)
- printf("%5d", now / MIN);
- else
- printf(" ");
- putchar(' ');
- mpid = -1;
- jcpu = 0;
- for (cnt = 0; cnt < nproc; cnt++) {
- fseek (kfd, kernel[1].xl_value + (cnt * sizeof proc), 0);
- fread(&proc, sizeof proc, 1, kfd);
- if (proc.p_stat == 0 || proc.p_stat == SZOMB || proc.p_stat == SWAIT)
- continue;
-
- if (findu (&proc, cnt, &prog) == 0)
- continue;
-
- if (prog.u_ttyp == NULL)
- continue;
- if (sbuf.st_rdev != prog.u_ttyd)
- continue;
- jcpu += prog.u_utime + prog.u_stime +
- prog.u_cutime + prog.u_cstime;
- if (proc.p_pid == mypid)
- continue;
- if (proc.p_pid > mpid)
- mpid = proc.p_pid;
- }
- if (mpid == -1)
- printf("[can't stat]");
- else {
- fseek(kfd, kernel[1].xl_value, 0);
- for (cnt = 0; cnt < nproc; cnt++) {
- fread(&proc, sizeof proc, 1, kfd);
- if (proc.p_pid == mpid)
- break;
- }
- if (proc.p_pid != mpid)
- printf("[interstice]");
- else {
- if (findu (&proc, cnt, &prog) == 0)
- fprintf (stderr, "can't find parent\n");
-
- printf("%3d:%02d %3d:%02d ",
- (jcpu / HZ) / MIN, (jcpu / HZ) % MIN,
- ((prog.u_utime + prog.u_stime) / HZ) / MIN,
- ((prog.u_utime + prog.u_stime) / HZ) % MIN);
- prog.u_procp = &proc;
- pcmd(&prog);
- }
- }
- putchar('\n');
- }
-
- char *vtime(when)
- long *when; {
- struct tm then, now;
- static char buf[20];
- short hour, min, ampm;
- long clock;
-
- time(&clock);
- now = *localtime(&clock);
- then = *localtime(when);
- if (then.tm_mon != now.tm_mon || then.tm_mday != now.tm_mday) {
- sprintf(buf, "%s %2d", months[then.tm_mon], then.tm_mday);
- return buf;
- }
- min = then.tm_min;
- if (then.tm_hour == 0) {
- ampm = 'a';
- hour = 12;
- }
- else if (then.tm_hour > 0 && then.tm_hour < 12) {
- ampm = 'a';
- hour = then.tm_hour;
- }
- else if (then.tm_hour == 12) {
- ampm = 'p';
- hour = 12;
- }
- else {
- ampm = 'p';
- hour = then.tm_hour - 12;
- }
- sprintf(buf, "%d:%02d%cm", hour, min, ampm);
- return buf;
- }
-
- pcmd(uinfo)
- struct user *uinfo;
- {
- /* someday look up the user's command line */
- printf("%-.14s", uinfo->u_comm);
- }
-