Versions of usr.bin/w/w.c analyzed:
- 1980-10-01
check-in 2685ec94a9
artifact 401524b93d
Origin for each line in
usr.bin/w/w.c
from check-in 2685ec94a9:
2685ec94a9 1980-10-01 bill: static char *sccsid = "@(#)w.c 4.1 (Berkeley) 10/01/80";
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * w - print system status (who and what)
2685ec94a9 1980-10-01 bill: *
2685ec94a9 1980-10-01 bill: * This program is similar to the systat command on Tenex/Tops 10/20
2685ec94a9 1980-10-01 bill: * It needs read permission on /dev/mem, /dev/kmem, and /dev/drum.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: #include <sys/param.h>
2685ec94a9 1980-10-01 bill: #include <nlist.h>
2685ec94a9 1980-10-01 bill: #include <stdio.h>
2685ec94a9 1980-10-01 bill: #include <ctype.h>
2685ec94a9 1980-10-01 bill: #include <utmp.h>
2685ec94a9 1980-10-01 bill: #include <time.h>
2685ec94a9 1980-10-01 bill: #include <sys/stat.h>
2685ec94a9 1980-10-01 bill: #include <sys/dir.h>
2685ec94a9 1980-10-01 bill: #include <sys/user.h>
2685ec94a9 1980-10-01 bill: #include <sys/proc.h>
2685ec94a9 1980-10-01 bill: #include <sys/pte.h>
2685ec94a9 1980-10-01 bill: #include <sys/vm.h>
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: #define NMAX sizeof(utmp.ut_name)
2685ec94a9 1980-10-01 bill: #define LMAX sizeof(utmp.ut_line)
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: #define ARGWIDTH 33 /* # chars left on 80 col crt for args */
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: struct smproc {
2685ec94a9 1980-10-01 bill: short w_pid; /* proc.p_pid */
2685ec94a9 1980-10-01 bill: char w_flag; /* proc.p_flag */
2685ec94a9 1980-10-01 bill: short w_size; /* proc.p_size */
2685ec94a9 1980-10-01 bill: long w_seekaddr; /* where to find args */
2685ec94a9 1980-10-01 bill: long w_lastpg; /* disk address of stack */
2685ec94a9 1980-10-01 bill: int w_igintr; /* INTR+3*QUIT, 0=die, 1=ign, 2=catch */
2685ec94a9 1980-10-01 bill: time_t w_time; /* CPU time used by this process */
2685ec94a9 1980-10-01 bill: time_t w_ctime; /* CPU time used by children */
2685ec94a9 1980-10-01 bill: dev_t w_tty; /* tty device of process */
2685ec94a9 1980-10-01 bill: char w_comm[15]; /* user.u_comm, null terminated */
2685ec94a9 1980-10-01 bill: char w_args[ARGWIDTH+1]; /* args if interesting process */
2685ec94a9 1980-10-01 bill: } pr[NPROC];
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: struct nlist nl[] = {
2685ec94a9 1980-10-01 bill: { "_proc" },
2685ec94a9 1980-10-01 bill: #define X_PROC 0
2685ec94a9 1980-10-01 bill: { "_swapdev" },
2685ec94a9 1980-10-01 bill: #define X_SWAPDEV 1
2685ec94a9 1980-10-01 bill: { "_Usrptmap" },
2685ec94a9 1980-10-01 bill: #define X_USRPTMA 2
2685ec94a9 1980-10-01 bill: { "_usrpt" },
2685ec94a9 1980-10-01 bill: #define X_USRPT 3
2685ec94a9 1980-10-01 bill: { "_nswap" },
2685ec94a9 1980-10-01 bill: #define X_NSWAP 4
2685ec94a9 1980-10-01 bill: { "_avenrun" },
2685ec94a9 1980-10-01 bill: #define X_AVENRUN 5
2685ec94a9 1980-10-01 bill: { "_bootime" },
2685ec94a9 1980-10-01 bill: #define X_BOOTIME 6
2685ec94a9 1980-10-01 bill: { 0 },
2685ec94a9 1980-10-01 bill: };
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: FILE *ps;
2685ec94a9 1980-10-01 bill: FILE *ut;
2685ec94a9 1980-10-01 bill: FILE *bootfd;
2685ec94a9 1980-10-01 bill: int kmem;
2685ec94a9 1980-10-01 bill: int mem;
2685ec94a9 1980-10-01 bill: int swap; /* /dev/kmem, mem, and swap */
2685ec94a9 1980-10-01 bill: int nswap;
2685ec94a9 1980-10-01 bill: dev_t tty;
2685ec94a9 1980-10-01 bill: char doing[520]; /* process attached to terminal */
2685ec94a9 1980-10-01 bill: time_t proctime; /* cpu time of process in doing */
2685ec94a9 1980-10-01 bill: double avenrun[3];
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: #define DIV60(t) ((t+30)/60) /* x/60 rounded */
2685ec94a9 1980-10-01 bill: #define TTYEQ (tty == pr[i].w_tty)
2685ec94a9 1980-10-01 bill: #define IGINT (1+3*1) /* ignoring both SIGINT & SIGQUIT */
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: char *getargs();
2685ec94a9 1980-10-01 bill: char *fread();
2685ec94a9 1980-10-01 bill: char *ctime();
2685ec94a9 1980-10-01 bill: char *rindex();
2685ec94a9 1980-10-01 bill: FILE *popen();
2685ec94a9 1980-10-01 bill: struct tm *localtime();
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: int debug; /* true if -d flag: debugging output */
2685ec94a9 1980-10-01 bill: int header = 1; /* true if -h flag: don't print heading */
2685ec94a9 1980-10-01 bill: int lflag = 1; /* true if -l flag: long style output */
2685ec94a9 1980-10-01 bill: int login; /* true if invoked as login shell */
2685ec94a9 1980-10-01 bill: int idle; /* number of minutes user is idle */
2685ec94a9 1980-10-01 bill: int nusers; /* number of users logged in now */
2685ec94a9 1980-10-01 bill: char * sel_user; /* login of particular user selected */
2685ec94a9 1980-10-01 bill: char firstchar; /* first char of name of prog invoked as */
2685ec94a9 1980-10-01 bill: time_t jobtime; /* total cpu time visible */
2685ec94a9 1980-10-01 bill: time_t now; /* the current time of day */
2685ec94a9 1980-10-01 bill: struct tm *nowt; /* current time as time struct */
2685ec94a9 1980-10-01 bill: time_t bootime, uptime; /* time of last reboot & elapsed time since */
2685ec94a9 1980-10-01 bill: int np; /* number of processes currently active */
2685ec94a9 1980-10-01 bill: struct utmp utmp;
2685ec94a9 1980-10-01 bill: struct proc mproc;
2685ec94a9 1980-10-01 bill: struct user up;
2685ec94a9 1980-10-01 bill: char fill[512];
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: main(argc, argv)
2685ec94a9 1980-10-01 bill: char **argv;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: int days, hrs, mins;
2685ec94a9 1980-10-01 bill: register int i, j;
2685ec94a9 1980-10-01 bill: char *cp;
2685ec94a9 1980-10-01 bill: register int curpid, empty;
2685ec94a9 1980-10-01 bill: char obuf[BUFSIZ];
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: setbuf(stdout, obuf);
2685ec94a9 1980-10-01 bill: login = (argv[0][0] == '-');
2685ec94a9 1980-10-01 bill: cp = rindex(argv[0], '/');
2685ec94a9 1980-10-01 bill: firstchar = login ? argv[0][1] : (cp==0) ? argv[0][0] : cp[1];
2685ec94a9 1980-10-01 bill: cp = argv[0]; /* for Usage */
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: while (argc > 1) {
2685ec94a9 1980-10-01 bill: if (argv[1][0] == '-') {
2685ec94a9 1980-10-01 bill: for (i=1; argv[1][i]; i++) {
2685ec94a9 1980-10-01 bill: switch(argv[1][i]) {
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: case 'd':
2685ec94a9 1980-10-01 bill: debug++;
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: case 'h':
2685ec94a9 1980-10-01 bill: header = 0;
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: case 'l':
2685ec94a9 1980-10-01 bill: lflag++;
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: case 's':
2685ec94a9 1980-10-01 bill: lflag = 0;
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: case 'u':
2685ec94a9 1980-10-01 bill: case 'w':
2685ec94a9 1980-10-01 bill: firstchar = argv[1][1];
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: default:
2685ec94a9 1980-10-01 bill: printf("Bad flag %s\n", argv[1]);
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: if (!isalnum(argv[1][0]) || argc > 2) {
2685ec94a9 1980-10-01 bill: printf("Usage: %s [ -hlsuw ] [ user ]\n", cp);
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: } else
2685ec94a9 1980-10-01 bill: sel_user = argv[1];
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: argc--; argv++;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if ((kmem = open("/dev/kmem", 0)) < 0) {
2685ec94a9 1980-10-01 bill: fprintf(stderr, "No kmem\n");
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: nlist("/vmunix", nl);
2685ec94a9 1980-10-01 bill: if (nl[0].n_type==0) {
2685ec94a9 1980-10-01 bill: fprintf(stderr, "No namelist\n");
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if (firstchar != 'u')
2685ec94a9 1980-10-01 bill: readpr();
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: ut = fopen("/etc/utmp","r");
2685ec94a9 1980-10-01 bill: if (header) {
2685ec94a9 1980-10-01 bill: /* Print time of day */
2685ec94a9 1980-10-01 bill: time(&now);
2685ec94a9 1980-10-01 bill: nowt = localtime(&now);
2685ec94a9 1980-10-01 bill: prtat(nowt);
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Print how long system has been up.
2685ec94a9 1980-10-01 bill: * (Found by looking for "bootime" in kernel)
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)nl[X_BOOTIME].n_value, 0);
2685ec94a9 1980-10-01 bill: read(kmem, &bootime, sizeof (bootime));
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: uptime = now - bootime;
2685ec94a9 1980-10-01 bill: days = uptime / (60*60*24);
2685ec94a9 1980-10-01 bill: uptime %= (60*60*24);
2685ec94a9 1980-10-01 bill: hrs = uptime / (60*60);
2685ec94a9 1980-10-01 bill: uptime %= (60*60);
2685ec94a9 1980-10-01 bill: mins = DIV60(uptime);
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: printf(" up");
2685ec94a9 1980-10-01 bill: if (days > 0)
2685ec94a9 1980-10-01 bill: printf(" %d day%s,", days, days>1?"s":"");
2685ec94a9 1980-10-01 bill: if (hrs > 0 && mins > 0) {
2685ec94a9 1980-10-01 bill: printf(" %2d:%02d,", hrs, mins);
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: if (hrs > 0)
2685ec94a9 1980-10-01 bill: printf(" %d hr%s,", hrs, hrs>1?"s":"");
2685ec94a9 1980-10-01 bill: if (mins > 0)
2685ec94a9 1980-10-01 bill: printf(" %d min%s,", mins, mins>1?"s":"");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* Print number of users logged in to system */
2685ec94a9 1980-10-01 bill: while (fread(&utmp, sizeof(utmp), 1, ut)) {
2685ec94a9 1980-10-01 bill: if (utmp.ut_name[0] != '\0')
2685ec94a9 1980-10-01 bill: nusers++;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: rewind(ut);
2685ec94a9 1980-10-01 bill: printf(" %d users", nusers);
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Print 1, 5, and 15 minute load averages.
2685ec94a9 1980-10-01 bill: * (Found by looking in kernel for avenrun).
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: printf(", load average:");
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)nl[X_AVENRUN].n_value, 0);
2685ec94a9 1980-10-01 bill: read(kmem, avenrun, sizeof(avenrun));
2685ec94a9 1980-10-01 bill: for (i = 0; i < (sizeof(avenrun)/sizeof(avenrun[0])); i++) {
2685ec94a9 1980-10-01 bill: if (i > 0)
2685ec94a9 1980-10-01 bill: printf(",");
2685ec94a9 1980-10-01 bill: printf(" %.2f", avenrun[i]);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: printf("\n");
2685ec94a9 1980-10-01 bill: if (firstchar == 'u')
2685ec94a9 1980-10-01 bill: exit(0);
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* Headers for rest of output */
2685ec94a9 1980-10-01 bill: if (lflag)
2685ec94a9 1980-10-01 bill: printf("User tty login@ idle JCPU PCPU what\n");
2685ec94a9 1980-10-01 bill: else
2685ec94a9 1980-10-01 bill: printf("User tty idle what\n");
2685ec94a9 1980-10-01 bill: fflush(stdout);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: for (;;) { /* for each entry in utmp */
2685ec94a9 1980-10-01 bill: if (fread(&utmp, sizeof(utmp), 1, ut) == NULL) {
2685ec94a9 1980-10-01 bill: fclose(ut);
2685ec94a9 1980-10-01 bill: exit(0);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: if (utmp.ut_name[0] == '\0')
2685ec94a9 1980-10-01 bill: continue; /* that tty is free */
2685ec94a9 1980-10-01 bill: if (sel_user && strcmpn(utmp.ut_name, sel_user, NMAX) != 0)
2685ec94a9 1980-10-01 bill: continue; /* we wanted only somebody else */
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: gettty();
2685ec94a9 1980-10-01 bill: jobtime = 0;
2685ec94a9 1980-10-01 bill: proctime = 0;
2685ec94a9 1980-10-01 bill: strcpy(doing, "-"); /* default act: normally never prints */
2685ec94a9 1980-10-01 bill: empty = 1;
2685ec94a9 1980-10-01 bill: curpid = -1;
2685ec94a9 1980-10-01 bill: idle = findidle();
2685ec94a9 1980-10-01 bill: for (i=0; i<np; i++) { /* for each process on this tty */
2685ec94a9 1980-10-01 bill: if (!(TTYEQ))
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: jobtime += pr[i].w_time + pr[i].w_ctime;
2685ec94a9 1980-10-01 bill: proctime += pr[i].w_time;
2685ec94a9 1980-10-01 bill: if (debug) {
2685ec94a9 1980-10-01 bill: printf("\t\t%d\t%s", pr[i].w_pid, pr[i].w_args);
2685ec94a9 1980-10-01 bill: if ((j=pr[i].w_igintr) > 0)
2685ec94a9 1980-10-01 bill: if (j==IGINT)
2685ec94a9 1980-10-01 bill: printf(" &");
2685ec94a9 1980-10-01 bill: else
2685ec94a9 1980-10-01 bill: printf(" & %d %d", j%3, j/3);
2685ec94a9 1980-10-01 bill: printf("\n");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: if (empty && pr[i].w_igintr!=IGINT) {
2685ec94a9 1980-10-01 bill: empty = 0;
2685ec94a9 1980-10-01 bill: curpid = -1;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: if(pr[i].w_pid>curpid && (pr[i].w_igintr!=IGINT || empty)){
2685ec94a9 1980-10-01 bill: curpid = pr[i].w_pid;
2685ec94a9 1980-10-01 bill: strcpy(doing, lflag ? pr[i].w_args : pr[i].w_comm);
2685ec94a9 1980-10-01 bill: #ifdef notdef
2685ec94a9 1980-10-01 bill: if (doing[0]==0 || doing[0]=='-' && doing[1]<=' ' || doing[0] == '?') {
2685ec94a9 1980-10-01 bill: strcat(doing, " (");
2685ec94a9 1980-10-01 bill: strcat(doing, pr[i].w_comm);
2685ec94a9 1980-10-01 bill: strcat(doing, ")");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: #endif
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: putline();
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* figure out the major/minor device # pair for this tty */
2685ec94a9 1980-10-01 bill: gettty()
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: char ttybuf[20];
2685ec94a9 1980-10-01 bill: struct stat statbuf;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: ttybuf[0] = 0;
2685ec94a9 1980-10-01 bill: strcpy(ttybuf, "/dev/");
2685ec94a9 1980-10-01 bill: strcat(ttybuf, utmp.ut_line);
2685ec94a9 1980-10-01 bill: stat(ttybuf, &statbuf);
2685ec94a9 1980-10-01 bill: tty = statbuf.st_rdev;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * putline: print out the accumulated line of info about one user.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: putline()
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: register int tm;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* print login name of the user */
2685ec94a9 1980-10-01 bill: printf("%-*.*s ", NMAX, NMAX, utmp.ut_name);
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* print tty user is on */
2685ec94a9 1980-10-01 bill: if (lflag)
2685ec94a9 1980-10-01 bill: /* long form: all (up to) LMAX chars */
2685ec94a9 1980-10-01 bill: printf("%-*.*s", LMAX, LMAX, utmp.ut_line);
2685ec94a9 1980-10-01 bill: else {
2685ec94a9 1980-10-01 bill: /* short form: 2 chars, skipping 'tty' if there */
2685ec94a9 1980-10-01 bill: if (utmp.ut_line[0]=='t' && utmp.ut_line[1]=='t' && utmp.ut_line[2]=='y')
2685ec94a9 1980-10-01 bill: printf("%-2.2s", &utmp.ut_line[3]);
2685ec94a9 1980-10-01 bill: else
2685ec94a9 1980-10-01 bill: printf("%-2.2s", utmp.ut_line);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if (lflag)
2685ec94a9 1980-10-01 bill: /* print when the user logged in */
2685ec94a9 1980-10-01 bill: prtat(localtime(&utmp.ut_time));
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* print idle time */
2685ec94a9 1980-10-01 bill: prttime(idle," ");
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if (lflag) {
2685ec94a9 1980-10-01 bill: /* print CPU time for all processes & children */
2685ec94a9 1980-10-01 bill: prttime(DIV60(jobtime)," ");
2685ec94a9 1980-10-01 bill: /* print cpu time for interesting process */
2685ec94a9 1980-10-01 bill: prttime(DIV60(proctime)," ");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* what user is doing, either command tail or args */
2685ec94a9 1980-10-01 bill: printf(" %-.32s\n",doing);
2685ec94a9 1980-10-01 bill: fflush(stdout);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* find & return number of minutes current tty has been idle */
2685ec94a9 1980-10-01 bill: findidle()
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: struct stat stbuf;
2685ec94a9 1980-10-01 bill: long lastaction, diff;
2685ec94a9 1980-10-01 bill: char ttyname[20];
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: strcpy(ttyname, "/dev/");
2685ec94a9 1980-10-01 bill: strcatn(ttyname, utmp.ut_line, LMAX);
2685ec94a9 1980-10-01 bill: stat(ttyname, &stbuf);
2685ec94a9 1980-10-01 bill: time(&now);
2685ec94a9 1980-10-01 bill: lastaction = stbuf.st_atime;
2685ec94a9 1980-10-01 bill: diff = now - lastaction;
2685ec94a9 1980-10-01 bill: diff = DIV60(diff);
2685ec94a9 1980-10-01 bill: if (diff < 0) diff = 0;
2685ec94a9 1980-10-01 bill: return(diff);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * prttime prints a time in hours and minutes.
2685ec94a9 1980-10-01 bill: * The character string tail is printed at the end, obvious
2685ec94a9 1980-10-01 bill: * strings to pass are "", " ", or "am".
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: prttime(tim, tail)
2685ec94a9 1980-10-01 bill: time_t tim;
2685ec94a9 1980-10-01 bill: char *tail;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: register int didhrs = 0;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if (tim >= 60) {
2685ec94a9 1980-10-01 bill: printf("%3d:", tim/60);
2685ec94a9 1980-10-01 bill: didhrs++;
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: printf(" ");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: tim %= 60;
2685ec94a9 1980-10-01 bill: if (tim > 0 || didhrs) {
2685ec94a9 1980-10-01 bill: printf(didhrs&&tim<10 ? "%02d" : "%2d", tim);
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: printf(" ");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: printf("%s", tail);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* prtat prints a 12 hour time given a pointer to a time of day */
2685ec94a9 1980-10-01 bill: prtat(p)
2685ec94a9 1980-10-01 bill: struct tm *p;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: register int t, pm;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: t = p -> tm_hour;
2685ec94a9 1980-10-01 bill: pm = (t > 11);
2685ec94a9 1980-10-01 bill: if (t > 11)
2685ec94a9 1980-10-01 bill: t -= 12;
2685ec94a9 1980-10-01 bill: if (t == 0)
2685ec94a9 1980-10-01 bill: t = 12;
2685ec94a9 1980-10-01 bill: prttime(t*60 + p->tm_min, pm ? "pm" : "am");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * readpr finds and reads in the array pr, containing the interesting
2685ec94a9 1980-10-01 bill: * parts of the proc and user tables for each live process.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: readpr()
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: int pn, mf, addr, c;
2685ec94a9 1980-10-01 bill: int szpt, pfnum, i;
2685ec94a9 1980-10-01 bill: struct pte *Usrptma, *usrpt, *pte, apte;
2685ec94a9 1980-10-01 bill: struct dblock db;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: Usrptma = (struct pte *) nl[X_USRPTMA].n_value;
2685ec94a9 1980-10-01 bill: usrpt = (struct pte *) nl[X_USRPT].n_value;
2685ec94a9 1980-10-01 bill: if((mem = open("/dev/mem", 0)) < 0) {
2685ec94a9 1980-10-01 bill: fprintf(stderr, "No mem\n");
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: if ((swap = open("/dev/drum", 0)) < 0) {
2685ec94a9 1980-10-01 bill: fprintf(stderr, "No drum\n");
2685ec94a9 1980-10-01 bill: exit(1);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * read mem to find swap dev.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)nl[X_SWAPDEV].n_value, 0);
2685ec94a9 1980-10-01 bill: read(kmem, &nl[X_SWAPDEV].n_value, sizeof(nl[X_SWAPDEV].n_value));
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Find base of swap
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)nl[X_NSWAP].n_value, 0);
2685ec94a9 1980-10-01 bill: read(kmem, &nswap, sizeof(nswap));
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Locate proc table
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: np = 0;
2685ec94a9 1980-10-01 bill: for (pn=0; pn<NPROC; pn++) {
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)(nl[X_PROC].n_value + pn*(sizeof mproc)), 0);
2685ec94a9 1980-10-01 bill: read(kmem, &mproc, sizeof mproc);
2685ec94a9 1980-10-01 bill: /* decide if it's an interesting process */
2685ec94a9 1980-10-01 bill: if (mproc.p_stat==0 || mproc.p_pgrp==0)
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: if (mproc.p_flag&SDETACH)
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: #ifdef notdef
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * The following speeds up w on systems with lots of ttys
2685ec94a9 1980-10-01 bill: * by ignoring inits and gettys, but loses on root login shells.
2685ec94a9 1980-10-01 bill: * On Ernie it reduced user and system time by .3 seconds,
2685ec94a9 1980-10-01 bill: * an insignificant amount. It is commented out since it
2685ec94a9 1980-10-01 bill: * will lose when root logs in.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: if (mproc.p_uid == 0 & mproc.p_ppid == 1)
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: #endif
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* find & read in the user structure */
2685ec94a9 1980-10-01 bill: if ((mproc.p_flag & SLOAD) == 0) {
2685ec94a9 1980-10-01 bill: /* not in memory - get from swap device */
2685ec94a9 1980-10-01 bill: addr = mproc.p_swaddr<<9;
2685ec94a9 1980-10-01 bill: lseek(swap, (long)addr, 0);
2685ec94a9 1980-10-01 bill: if (read(swap, &up, sizeof(up)) != sizeof(up)) {
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: int p0br, cc;
2685ec94a9 1980-10-01 bill: #define INTPPG (NBPG / sizeof (int))
2685ec94a9 1980-10-01 bill: struct pte pagetbl[NBPG / sizeof (struct pte)];
2685ec94a9 1980-10-01 bill: /* loaded, get each page from memory separately */
2685ec94a9 1980-10-01 bill: szpt = mproc.p_szpt;
2685ec94a9 1980-10-01 bill: p0br = (int)mproc.p_p0br;
2685ec94a9 1980-10-01 bill: pte = &Usrptma[btokmx(mproc.p_p0br) + szpt-1];
2685ec94a9 1980-10-01 bill: lseek(kmem, (long)pte, 0);
2685ec94a9 1980-10-01 bill: if (read(kmem, &apte, sizeof(apte)) != sizeof(apte))
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: lseek(mem, ctob(apte.pg_pfnum), 0);
2685ec94a9 1980-10-01 bill: if (read(mem,pagetbl,sizeof(pagetbl)) != sizeof(pagetbl))
2685ec94a9 1980-10-01 bill: cont:
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill: for(cc=0; cc<UPAGES; cc++) { /* get u area */
2685ec94a9 1980-10-01 bill: int upage = pagetbl[NPTEPG-UPAGES+cc].pg_pfnum;
2685ec94a9 1980-10-01 bill: lseek(mem,ctob(upage),0);
2685ec94a9 1980-10-01 bill: if (read(mem,((int *)&up)+INTPPG*cc,NBPG) != NBPG)
2685ec94a9 1980-10-01 bill: goto cont;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: szpt = up.u_pcb.pcb_szpt;
2685ec94a9 1980-10-01 bill: pr[np].w_seekaddr = ctob(apte.pg_pfnum);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: vstodb(0, 1, &up.u_smap, &db, 1);
2685ec94a9 1980-10-01 bill: pr[np].w_lastpg = ctob(db.db_base);
2685ec94a9 1980-10-01 bill: if (up.u_ttyp == NULL)
2685ec94a9 1980-10-01 bill: continue;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* save the interesting parts */
2685ec94a9 1980-10-01 bill: pr[np].w_pid = mproc.p_pid;
2685ec94a9 1980-10-01 bill: pr[np].w_flag = mproc.p_flag;
2685ec94a9 1980-10-01 bill: pr[np].w_size = mproc.p_dsize + mproc.p_ssize;
2685ec94a9 1980-10-01 bill: pr[np].w_igintr = (((int)up.u_signal[2]==1) + 2*((int)up.u_signal[2]>1) + 3*((int)up.u_signal[3]==1)) + 6*((int)up.u_signal[3]>1);
2685ec94a9 1980-10-01 bill: pr[np].w_time = up.u_vm.vm_utime + up.u_vm.vm_stime;
2685ec94a9 1980-10-01 bill: pr[np].w_ctime = up.u_cvm.vm_utime + up.u_cvm.vm_stime;
2685ec94a9 1980-10-01 bill: pr[np].w_tty = up.u_ttyd;
2685ec94a9 1980-10-01 bill: up.u_comm[14] = 0; /* Bug: This bombs next field. */
2685ec94a9 1980-10-01 bill: strcpy(pr[np].w_comm, up.u_comm);
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Get args if there's a chance we'll print it.
2685ec94a9 1980-10-01 bill: * Cant just save pointer: getargs returns static place.
2685ec94a9 1980-10-01 bill: * Cant use strcpyn: that crock blank pads.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: pr[np].w_args[0] = 0;
2685ec94a9 1980-10-01 bill: strcatn(pr[np].w_args,getargs(&pr[np]),ARGWIDTH);
2685ec94a9 1980-10-01 bill: if (pr[np].w_args[0]==0 || pr[np].w_args[0]=='-' && pr[np].w_args[1]<=' ' || pr[np].w_args[0] == '?') {
2685ec94a9 1980-10-01 bill: strcat(pr[np].w_args, " (");
2685ec94a9 1980-10-01 bill: strcat(pr[np].w_args, pr[np].w_comm);
2685ec94a9 1980-10-01 bill: strcat(pr[np].w_args, ")");
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: np++;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * getargs: given a pointer to a proc structure, this looks at the swap area
2685ec94a9 1980-10-01 bill: * and tries to reconstruct the arguments. This is straight out of ps.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: char *
2685ec94a9 1980-10-01 bill: getargs(p)
2685ec94a9 1980-10-01 bill: struct smproc *p;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: int c, addr, nbad;
2685ec94a9 1980-10-01 bill: static int abuf[512/sizeof(int)];
2685ec94a9 1980-10-01 bill: struct pte pagetbl[NPTEPG];
2685ec94a9 1980-10-01 bill: register int *ip;
2685ec94a9 1980-10-01 bill: register char *cp, *cp1;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if ((p->w_flag & SLOAD) == 0) {
2685ec94a9 1980-10-01 bill: lseek(swap, p->w_lastpg, 0);
2685ec94a9 1980-10-01 bill: if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf))
2685ec94a9 1980-10-01 bill: return(p->w_comm);
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: c = p->w_seekaddr;
2685ec94a9 1980-10-01 bill: lseek(mem,c,0);
2685ec94a9 1980-10-01 bill: if (read(mem,pagetbl,NBPG) != NBPG)
2685ec94a9 1980-10-01 bill: return(p->w_comm);
2685ec94a9 1980-10-01 bill: if (pagetbl[NPTEPG-1-UPAGES].pg_fod==0 && pagetbl[NPTEPG-1-UPAGES].pg_pfnum) {
2685ec94a9 1980-10-01 bill: lseek(mem,ctob(pagetbl[NPTEPG-1-UPAGES].pg_pfnum),0);
2685ec94a9 1980-10-01 bill: if (read(mem,abuf,sizeof(abuf)) != sizeof(abuf))
2685ec94a9 1980-10-01 bill: return(p->w_comm);
2685ec94a9 1980-10-01 bill: } else {
2685ec94a9 1980-10-01 bill: lseek(swap, p->w_lastpg, 0);
2685ec94a9 1980-10-01 bill: if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf))
2685ec94a9 1980-10-01 bill: return(p->w_comm);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: abuf[127] = 0;
2685ec94a9 1980-10-01 bill: for (ip = &abuf[126]; ip > abuf;) {
2685ec94a9 1980-10-01 bill: /* Look from top for -1 or 0 as terminator flag. */
2685ec94a9 1980-10-01 bill: if (*--ip == -1 || *ip == 0) {
2685ec94a9 1980-10-01 bill: cp = (char *)(ip+1);
2685ec94a9 1980-10-01 bill: if (*cp==0)
2685ec94a9 1980-10-01 bill: cp++;
2685ec94a9 1980-10-01 bill: nbad = 0; /* up to 5 funny chars as ?'s */
2685ec94a9 1980-10-01 bill: for (cp1 = cp; cp1 < (char *)&abuf[128]; cp1++) {
2685ec94a9 1980-10-01 bill: c = *cp1&0177;
2685ec94a9 1980-10-01 bill: if (c==0) /* nulls between args => spaces */
2685ec94a9 1980-10-01 bill: *cp1 = ' ';
2685ec94a9 1980-10-01 bill: else if (c < ' ' || c > 0176) {
2685ec94a9 1980-10-01 bill: if (++nbad >= 5) {
2685ec94a9 1980-10-01 bill: *cp1++ = ' ';
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: *cp1 = '?';
2685ec94a9 1980-10-01 bill: } else if (c=='=') { /* Oops - found an
2685ec94a9 1980-10-01 bill: * environment var, back
2685ec94a9 1980-10-01 bill: * over & erase it. */
2685ec94a9 1980-10-01 bill: *cp1 = 0;
2685ec94a9 1980-10-01 bill: while (cp1>cp && *--cp1!=' ')
2685ec94a9 1980-10-01 bill: *cp1 = 0;
2685ec94a9 1980-10-01 bill: break;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: while (*--cp1==' ') /* strip trailing spaces */
2685ec94a9 1980-10-01 bill: *cp1 = 0;
2685ec94a9 1980-10-01 bill: return(cp);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: return (p->w_comm);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /*
2685ec94a9 1980-10-01 bill: * Given a base/size pair in virtual swap area,
2685ec94a9 1980-10-01 bill: * return a physical base/size pair which is the
2685ec94a9 1980-10-01 bill: * (largest) initial, physically contiguous block.
2685ec94a9 1980-10-01 bill: */
2685ec94a9 1980-10-01 bill: vstodb(vsbase, vssize, dmp, dbp, rev)
2685ec94a9 1980-10-01 bill: register int vsbase;
2685ec94a9 1980-10-01 bill: int vssize;
2685ec94a9 1980-10-01 bill: struct dmap *dmp;
2685ec94a9 1980-10-01 bill: register struct dblock *dbp;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill: register int blk = DMMIN;
2685ec94a9 1980-10-01 bill: register swblk_t *ip = dmp->dm_map;
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
2685ec94a9 1980-10-01 bill: panic("vstodb");
2685ec94a9 1980-10-01 bill: while (vsbase >= blk) {
2685ec94a9 1980-10-01 bill: vsbase -= blk;
2685ec94a9 1980-10-01 bill: if (blk < DMMAX)
2685ec94a9 1980-10-01 bill: blk *= 2;
2685ec94a9 1980-10-01 bill: ip++;
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill: if (*ip <= 0 || *ip + blk > nswap)
2685ec94a9 1980-10-01 bill: panic("vstodb *ip");
2685ec94a9 1980-10-01 bill: dbp->db_size = min(vssize, blk - vsbase);
2685ec94a9 1980-10-01 bill: dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: panic(cp)
2685ec94a9 1980-10-01 bill: char *cp;
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: /* printf("%s\n", cp); */
2685ec94a9 1980-10-01 bill: }
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: min(a, b)
2685ec94a9 1980-10-01 bill: {
2685ec94a9 1980-10-01 bill:
2685ec94a9 1980-10-01 bill: return (a < b ? a : b);
2685ec94a9 1980-10-01 bill: }