|
|
1.1 ! root 1: /* ! 2: * Time command ! 3: */ ! 4: ! 5: #include <stdio.h> ! 6: #include <sys/timeb.h> ! 7: #include <sys/times.h> ! 8: #include <signal.h> ! 9: #include <sys/machine.h> ! 10: ! 11: #define MILLI 1000 /* Parts of a second */ ! 12: ! 13: extern char *signame[]; ! 14: ! 15: main(argc, argv) ! 16: char *argv[]; ! 17: { ! 18: if (argc <= 1) { ! 19: exit(0); ! 20: } ! 21: dotime(argc-1, argv+1); ! 22: exit(0); ! 23: } ! 24: ! 25: dotime(argc, argv) ! 26: char *argv[]; ! 27: { ! 28: int pid, status; ! 29: register n; ! 30: struct timeb before, after; ! 31: struct tbuffer tb; ! 32: long tdiff; ! 33: int tpart; ! 34: char *command = argv[0]; ! 35: ! 36: ftime(&before); ! 37: if ((pid = fork()) < 0) { ! 38: eprint("Try again.\n"); ! 39: exit(1); ! 40: } ! 41: if (pid) { ! 42: signal(SIGHUP, SIG_IGN); ! 43: signal(SIGINT, SIG_IGN); ! 44: signal(SIGQUIT, SIG_IGN); ! 45: signal(SIGTERM, SIG_IGN); ! 46: while (wait(&status) >= 0) ! 47: if (n = status&0177) { ! 48: if (n <= NSIG) ! 49: eprint("%s", signame[n]); ! 50: else ! 51: eprint("sig. %d", n); ! 52: if (status & 0200) ! 53: eprint(" -- core dumped"); ! 54: fprintf(stderr ,"\n"); ! 55: } ! 56: ftime(&after); ! 57: times(&tb); ! 58: tdiff = after.time-before.time; ! 59: tpart = after.millitm - before.millitm; ! 60: if (tpart < 0) { ! 61: tpart += MILLI; ! 62: tdiff--; ! 63: } ! 64: eprint("\n"); ! 65: output("Real:", tdiff, (tpart+MILLI/20)/(MILLI/10)); ! 66: tpart = tb.tb_cutime%HZ; ! 67: output("User:", tb.tb_cutime/HZ, (tpart+HZ/20)/(HZ/10)); ! 68: tpart = tb.tb_cstime%HZ; ! 69: output("Sys: ", tb.tb_cstime/HZ, (tpart+HZ/20)/(HZ/10)); ! 70: } else { ! 71: execvp(command, argv); ! 72: eprint("%s: not found\n", command); ! 73: exit(1); ! 74: } ! 75: exit(n); ! 76: } ! 77: ! 78: /* ! 79: * Format the output for a time entry. ! 80: */ ! 81: output(name, tsec, tenths) ! 82: char *name; ! 83: long tsec; ! 84: { ! 85: long hours; ! 86: int mins; ! 87: int secs; ! 88: ! 89: /* ! 90: * Fudge for sloppy rounding above ! 91: */ ! 92: if (tenths > 9) { ! 93: tenths = 0; ! 94: tsec++; ! 95: } ! 96: hours = tsec/(60*60); ! 97: tsec %= (60*60); ! 98: mins = tsec/60; ! 99: secs = tsec%60; ! 100: eprint("%s", name); ! 101: if (hours != 0) ! 102: eprint("%5d:%02d:%02d", (int)hours, mins, secs); ! 103: else { ! 104: eprint(" "); ! 105: if (mins) ! 106: eprint("%2d:%02d", mins, secs); ! 107: else ! 108: eprint(" %2d", secs); ! 109: } ! 110: eprint(".%d\n", tenths); ! 111: } ! 112: ! 113: /* VARARGS */ ! 114: eprint(x) ! 115: { ! 116: fprintf(stderr, "%r", &x); ! 117: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.