1: /* @(#)sh.vmwait.c 2.1 SCCS id keyword */ 2: /* Copyright (c) 1980 Regents of the University of California */ 3: #include "sh.h" 4: 5: /* 6: * C Shell 7: */ 8: 9: static struct tbuffer { 10: #ifdef V6 11: int put, pst; 12: #else 13: time_t put, pst; 14: #endif 15: time_t cut, cst; 16: } times0; 17: time_t time0; 18: 19: long 20: secs(bef, aft) 21: struct tbuffer *bef, *aft; 22: { 23: 24: return ((aft->cut - bef->cut + aft->cst - bef->cst)); 25: } 26: 27: settimes() 28: { 29: 30: time(&time0); 31: times(×0); 32: } 33: 34: dotime(v, kp) 35: register char **v; 36: struct command *kp; 37: { 38: struct tbuffer timeszer, timesdol, *tzp; 39: time_t timezer, timedol; 40: 41: if (v[1] != 0) { 42: time(&timezer), times(×zer); 43: lshift(v, 1); 44: if (func(kp) == 0) { 45: timflg = 1; 46: return (0); 47: } 48: tzp = ×zer; 49: } else 50: timezer = time0, tzp = ×0; 51: time(&timedol); 52: times(×dol); 53: ptimes(timedol - timezer, tzp, ×dol); 54: return (1); 55: } 56: 57: donice(v) 58: register char **v; 59: { 60: register char *cp; 61: 62: v++, cp = *v++; 63: if (cp == 0) { 64: #ifndef V6 65: nice(20); 66: nice(-10); 67: #endif 68: nice(4); 69: return (1); 70: } 71: if (*v == 0 && any(cp[0], "+-")) { 72: #ifndef V6 73: nice(20); 74: nice(-10); 75: #endif 76: nice(getn(cp)); 77: return (1); 78: } 79: return (0); 80: } 81: 82: struct tbuffer bef, aft; 83: time_t btim, atim; 84: 85: pwait(i) 86: register int i; 87: { 88: register int p, e; 89: char *name; 90: int s; 91: 92: if (i == 0) 93: return; 94: time(&btim); 95: do { 96: times(&bef); 97: p = wait(&s); 98: if (p == -1) 99: return; 100: times(&aft); 101: if (p == getn(value("child"))) 102: unsetv("child"); 103: time(&atim); 104: e = s & TRIM; 105: if (e > 0 && (e > 15 || mesg[e])) { 106: if (p != i) 107: printf("%d: ", p); 108: if (name = cname(p)) 109: printf("%s: ", name); 110: if (e <= 15) 111: printf(mesg[e]); 112: else 113: printf("Sig %d", e); 114: if (s & 0200) 115: printf(" -- Core dumped"); 116: printf("\n"); 117: } 118: if (e != 0 && i == p) { 119: cdone(p); 120: if (e == SIGINT && setintr && (!gointr || !eq(gointr, "-"))) 121: pintr(); 122: error(0); 123: } 124: if (i == p) { 125: set("status", putn(e ? e | QUOTE : (s >> 8) & 0377)); 126: if (exiterr && !eq(value("status"), "0")) { 127: if (e == 0) { 128: if (name = cname(p)) 129: printf("%s: ", name); 130: printf("Exit status %s\n", value("status")); 131: } 132: exitstat(); 133: } 134: cdone(p); 135: break; 136: } 137: cdone(p); 138: } while (i != p); 139: if (timflg || (!child && adrof("time") && secs(&bef, &aft) / 60 >= getn(value("time")))) { 140: timflg = 0; 141: ptimes(atim - btim, &bef, &aft); 142: } 143: } 144: 145: ptimes(sec, bef, aft) 146: time_t sec; 147: register struct tbuffer *bef, *aft; 148: { 149: 150: p60ths(aft->cut - bef->cut); 151: printf("u "); 152: p60ths(aft->cst - bef->cst); 153: printf("s "); 154: psecs(sec); 155: printf(" %d%%\n", (int) ((100 * secs(bef, aft)) / (60 * (sec ? sec : 1)))); 156: } 157: 158: endwait() 159: { 160: 161: signal(SIGINT, SIG_IGN); 162: cleft(); 163: bferr("Interrupted"); 164: } 165: 166: await() 167: { 168: if (setintr) 169: signal(SIGINT, endwait); 170: pwait(-1); 171: if (setintr) 172: signal(SIGINT, SIG_IGN); 173: } 174: 175: struct achild { 176: int pid; 177: char *cname; 178: struct achild *cnext; 179: } children; 180: 181: char * 182: cname(pid) 183: int pid; 184: { 185: register struct achild *cp; 186: 187: for (cp = children.cnext; cp; cp = cp->cnext) 188: if (cp->pid == pid) 189: return (cp->cname); 190: return (NOSTR); 191: } 192: 193: cadd(pid, cname) 194: int pid; 195: char *cname; 196: { 197: register struct achild *cp = (struct achild *) calloc(1, sizeof (struct achild)); 198: 199: cp->pid = pid; 200: cp->cname = savestr(cname); 201: cp->cnext = children.cnext; 202: children.cnext = cp; 203: } 204: 205: cdone(pid) 206: int pid; 207: { 208: register struct achild *cpp, *cp; 209: 210: cpp = &children; 211: for (cp = cpp->cnext; cp; cp = cp->cnext) { 212: if (cp->pid == pid) { 213: xfree(cp->cname); 214: cpp->cnext = cp->cnext; 215: xfree(cp); 216: return; 217: } 218: cpp = cp; 219: } 220: } 221: 222: cleft() 223: { 224: 225: register struct achild *cp; 226: 227: for (cp = children.cnext; cp; cp = cp->cnext) 228: printf("%6d %s\n", cp->pid, cp->cname); 229: }