1 static char *sccsid = "@(#)time.c 4.1 (Berkeley) 10/01/80"; 2 /* time command */ 3 4 #include <stdio.h> 5 #include <signal.h> 6 #include <sys/types.h> 7 #include <sys/times.h> 8 9 extern int errno; 10 extern char *sys_errlist[]; 11 12 main(argc, argv) 13 char **argv; 14 { 15 struct tms buffer, obuffer; 16 int status; 17 register p; 18 time_t before, after; 19 20 if(argc<=1) 21 exit(0); 22 time(&before); 23 p = fork(); 24 if(p == -1) { 25 fprintf(stderr, "Try again.\n"); 26 exit(1); 27 } 28 if(p == 0) { 29 execvp(argv[1], &argv[1]); 30 fprintf(stderr, "%s: %s\n", argv[1], sys_errlist[errno]); 31 exit(1); 32 } 33 signal(SIGINT, SIG_IGN); 34 signal(SIGQUIT, SIG_IGN); 35 times(&obuffer); 36 while(wait(&status) != p) 37 times(&obuffer); 38 time(&after); 39 if((status&0377) != 0) 40 fprintf(stderr,"Command terminated abnormally.\n"); 41 times(&buffer); 42 fprintf(stderr,"\n"); 43 printt("real", (after-before) * 60); 44 printt("user", buffer.tms_cutime - obuffer.tms_cutime); 45 printt("sys ", buffer.tms_cstime - obuffer.tms_cstime); 46 exit(status>>8); 47 } 48 49 char quant[] = { 6, 10, 10, 6, 10, 6, 10, 10, 10 }; 50 char *pad = "000 "; 51 char *sep = "\0\0.\0:\0:\0\0"; 52 char *nsep = "\0\0.\0 \0 \0\0"; 53 54 printt(s, a) 55 char *s; 56 long a; 57 { 58 char digit[9]; 59 register i; 60 char c; 61 int nonzero; 62 63 for(i=0; i<9; i++) { 64 digit[i] = a % quant[i]; 65 a /= quant[i]; 66 } 67 fprintf(stderr,s); 68 nonzero = 0; 69 while(--i>0) { 70 c = digit[i]!=0 ? digit[i]+'0': 71 nonzero ? '0': 72 pad[i]; 73 if (c) 74 fprintf(stderr,"%c",c); 75 nonzero |= digit[i]; 76 c = nonzero?sep[i]:nsep[i]; 77 if (c) 78 fprintf(stderr,"%c",c); 79 } 80 fprintf(stderr,"\n"); 81 } 82