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