- 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, ")");
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_BOOTTIME;
+ size = sizeof(boottime);
+ if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
+ boottime.tv_sec != 0) {
+ uptime = now - boottime.tv_sec;
+ uptime += 30;
+ days = uptime / SECSPERDAY;
+ uptime %= SECSPERDAY;
+ hrs = uptime / SECSPERHOUR;
+ uptime %= SECSPERHOUR;
+ mins = uptime / SECSPERMIN;
+ (void)printf(" up");
+ if (days > 0)
+ (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
+ if (hrs > 0 && mins > 0)
+ (void)printf(" %2d:%02d,", hrs, mins);
+ else {
+ if (hrs > 0)
+ (void)printf(" %d hr%s,",
+ hrs, hrs > 1 ? "s" : "");
+ if (mins > 0)
+ (void)printf(" %d min%s,",
+ mins, mins > 1 ? "s" : "");