-char *weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
-char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
-
-/* prtat prints a 12 hour time given a pointer to a time of day */
-prtat(time)
- long *time;
-{
- struct tm *p;
- register int hr, pm;
-
- p = localtime(time);
- hr = p->tm_hour;
- pm = (hr > 11);
- if (hr > 11)
- hr -= 12;
- if (hr == 0)
- hr = 12;
- if (now - *time <= 18 * HR)
- prttime(hr * 60 + p->tm_min, pm ? "pm" : "am");
- else if (now - *time <= 7 * DAY)
- printf(" %*s%d%s", hr < 10 ? 4 : 3, weekday[p->tm_wday], hr,
- pm ? "pm" : "am");
- else
- printf(" %2d%s%2d", p->tm_mday, month[p->tm_mon], p->tm_year);
-}
-
-/*
- * readpr finds and reads in the array pr, containing the interesting
- * parts of the proc and user tables for each live process.
- * We only accept procs whos controlling tty has a pgrp equal to the
- * pgrp of the proc. This accurately defines the notion of the current
- * process(s), but because of time skew, we always read in the tty struct
- * after reading the proc, even though the same tty struct may have been
- * read earlier on.
- */
-readpr()
-{
- int mf, addr, c;
- int szpt, pfnum, i;
- struct pte *Usrptma, *usrpt, *pte, apte;
- struct dblock db;
- struct kinfo_proc *kp;
- register struct proc *p;
- int nentries;
-
- Usrptma = (struct pte *) nl[X_USRPTMA].n_value;
- usrpt = (struct pte *) nl[X_USRPT].n_value;
- if((mem = open(_PATH_MEM, 0)) < 0) {
- fprintf(stderr, "w: no %s.\n", _PATH_MEM);
- exit(1);
- }
- if ((swap = open(_PATH_DRUM, 0)) < 0) {
- fprintf(stderr, "w: no %s\n", _PATH_DRUM);
- exit(1);
- }
- /*
- * read mem to find swap dev.
- */
- lseek(kmem, (long)nl[X_SWAPDEV].n_value, 0);
- read(kmem, &nl[X_SWAPDEV].n_value, sizeof(nl[X_SWAPDEV].n_value));
- /*
- * Find base of and parameters of swap
- */
- lseek(kmem, (long)nl[X_NSWAP].n_value, 0);
- read(kmem, &nswap, sizeof(nswap));
- lseek(kmem, (long)nl[X_DMMIN].n_value, 0);
- read(kmem, &dmmin, sizeof(dmmin));
- lseek(kmem, (long)nl[X_DMMAX].n_value, 0);
- read(kmem, &dmmax, sizeof(dmmax));
- /*
- * Read proc table.
- */
- np = 0;
- nentries = kvm_getkproc(&kp);
- pr = (struct pr *)calloc(nentries, sizeof (struct pr));
- for (i=0; i < nentries; i++, kp++) {
- p = &kp->kp_proc;
- /* decide if it's an interesting process */
- if (p->p_stat==0 || p->p_stat==SZOMB
- || p->p_stat==SSTOP)
- continue;
- /* find & read in the user structure */
- if ((p->p_flag & SLOAD) == 0) {
- /* not in memory - get from swap device */
- addr = dtob(p->p_swaddr);
- lseek(swap, (long)addr, 0);
- if (read(swap, &up, sizeof(up)) != sizeof(up)) {
- continue;
- }
- } else {
- int p0br, cc;
-#define INTPPG (NBPG / sizeof (int))
- struct pte pagetbl[NBPG / sizeof (struct pte)];
- /* loaded, get each page from memory separately */
- szpt = p->p_szpt;
- p0br = (int)p->p_p0br;
- pte = &Usrptma[btokmx(p->p_p0br) + szpt-1];
- lseek(kmem, (long)pte, 0);
- if (read(kmem, &apte, sizeof(apte)) != sizeof(apte))
- continue;
- lseek(mem, ctob(apte.pg_pfnum), 0);
- if (read(mem,pagetbl,sizeof(pagetbl)) != sizeof(pagetbl))
-cont:
- continue;
- for(cc=0; cc<UPAGES; cc++) { /* get u area */
- int upage = pagetbl[NPTEPG-UPAGES+cc].pg_pfnum;
- lseek(mem,ctob(upage),0);
- if (read(mem,((int *)&up)+INTPPG*cc,NBPG) != NBPG)
- goto cont;
- }
- szpt = up.u_pcb.pcb_szpt;
- pr[np].w_seekaddr = ctob(apte.pg_pfnum);
- }
- vstodb(0, CLSIZE, &up.u_smap, &db, 1);
- pr[np].w_lastpg = dtob(db.db_base);
- if (kp->kp_eproc.kp_tdev == NODEV)
- continue;
-
- /* only include a process whose tty has a pgrp which matchs its own */
- if (kp->kp_eproc.kp_pgid != kp->kp_eproc.kp_tpgid)
- continue;
-
- /* save the interesting parts */
- pr[np].w_pid = p->p_pid;
- pr[np].w_flag = p->p_flag;
- pr[np].w_size = p->p_dsize + p->p_ssize;
- 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);
- pr[np].w_time =
- up.u_ru.ru_utime.tv_sec + up.u_ru.ru_stime.tv_sec;
- pr[np].w_ctime =
- up.u_cru.ru_utime.tv_sec + up.u_cru.ru_stime.tv_sec;
- pr[np].w_tty = kp->kp_eproc.kp_tdev;
- pr[np].w_uid = p->p_uid;
- strcpy(pr[np].w_comm, p->p_comm, MAXCOMLEN+1);
- /*
- * Get args if there's a chance we'll print it.
- * Can't just save pointer: getargs returns static place.
- * Can't use strncpy, it blank pads.
- */
- pr[np].w_args[0] = 0;
- strncat(pr[np].w_args,getargs(&pr[np]),ARGWIDTH);
- if (pr[np].w_args[0]==0 || pr[np].w_args[0]=='-' && pr[np].w_args[1]<=' ' || pr[np].w_args[0] == '?') {
- strcat(pr[np].w_args, " (");
- strcat(pr[np].w_args, pr[np].w_comm);
- strcat(pr[np].w_args, ")");
- }
- np++;
- }
-}
-
-/*
- * getargs: given a pointer to a proc structure, this looks at the swap area
- * and tries to reconstruct the arguments. This is straight out of ps.
- */
-char *
-getargs(p)
- struct pr *p;
-{
- int c, addr, nbad;
- static int abuf[CLSIZE*NBPG/sizeof(int)];
- struct pte pagetbl[NPTEPG];
- register int *ip;
- register char *cp, *cp1;
-
- if ((p->w_flag & SLOAD) == 0) {
- lseek(swap, p->w_lastpg, 0);
- if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf))
- return(p->w_comm);
- } else {
- c = p->w_seekaddr;
- lseek(mem,c,0);
- if (read(mem,pagetbl,NBPG) != NBPG)
- return(p->w_comm);
- if (pagetbl[NPTEPG-CLSIZE-UPAGES].pg_fod==0 && pagetbl[NPTEPG-CLSIZE-UPAGES].pg_pfnum) {
- lseek(mem,ctob(pagetbl[NPTEPG-CLSIZE-UPAGES].pg_pfnum),0);
- if (read(mem,abuf,sizeof(abuf)) != sizeof(abuf))
- return(p->w_comm);
- } else {
- lseek(swap, p->w_lastpg, 0);
- if (read(swap, abuf, sizeof(abuf)) != sizeof(abuf))
- return(p->w_comm);
- }
- }
- abuf[sizeof(abuf)/sizeof(abuf[0])-1] = 0;
- for (ip = &abuf[sizeof(abuf)/sizeof(abuf[0])-2]; ip > abuf;) {
- /* Look from top for -1 or 0 as terminator flag. */
- if (*--ip == -1 || *ip == 0) {
- cp = (char *)(ip+1);
- if (*cp==0)
- cp++;
- nbad = 0; /* up to 5 funny chars as ?'s */
- for (cp1 = cp; cp1 < (char *)&abuf[sizeof(abuf)/sizeof(abuf[0])]; cp1++) {
- c = *cp1&0177;
- if (c==0) /* nulls between args => spaces */
- *cp1 = ' ';
- else if (c < ' ' || c > 0176) {
- if (++nbad >= 5) {
- *cp1++ = ' ';
- break;
- }
- *cp1 = '?';
- } else if (c=='=') { /* Oops - found an
- * environment var, back
- * over & erase it. */
- *cp1 = 0;
- while (cp1>cp && *--cp1!=' ')
- *cp1 = 0;
- break;
- }
- }
- while (*--cp1==' ') /* strip trailing spaces */
- *cp1 = 0;
- return(cp);
- }
- }
- return (p->w_comm);
-}