Commit | Line | Data |
---|---|---|
5cb2ff69 SL |
1 | #ifndef lint |
2 | static char *sccsid = "@(#)time.c 4.4 (Berkeley) %G%"; | |
3 | #endif | |
63a84c73 | 4 | |
b1198826 SL |
5 | /* |
6 | * time | |
7 | */ | |
63a84c73 BJ |
8 | #include <stdio.h> |
9 | #include <signal.h> | |
10 | #include <sys/types.h> | |
b1198826 SL |
11 | #include <time.h> |
12 | #include <resource.h> | |
63a84c73 BJ |
13 | |
14 | main(argc, argv) | |
b1198826 SL |
15 | int argc; |
16 | char **argv; | |
63a84c73 | 17 | { |
63a84c73 | 18 | int status; |
b1198826 SL |
19 | register int p; |
20 | struct timeval before, after; | |
21 | struct rusage ru; | |
63a84c73 | 22 | |
b1198826 | 23 | if (argc<=1) |
63a84c73 | 24 | exit(0); |
b1198826 | 25 | gettimeofday(&before, 0); |
63a84c73 | 26 | p = fork(); |
b1198826 SL |
27 | if (p < 0) { |
28 | perror("time"); | |
63a84c73 BJ |
29 | exit(1); |
30 | } | |
b1198826 | 31 | if (p == 0) { |
63a84c73 | 32 | execvp(argv[1], &argv[1]); |
b1198826 | 33 | perror(argv[1]); |
63a84c73 BJ |
34 | exit(1); |
35 | } | |
36 | signal(SIGINT, SIG_IGN); | |
37 | signal(SIGQUIT, SIG_IGN); | |
b1198826 SL |
38 | while (wait3(&status, 0, &ru) != p) |
39 | ; | |
40 | gettimeofday(&after, 0); | |
41 | if ((status&0377) != 0) | |
42 | fprintf(stderr, "Command terminated abnormally.\n"); | |
43 | after.tv_sec -= before.tv_sec; | |
44 | after.tv_usec -= before.tv_usec; | |
45 | if (after.tv_usec < 0) | |
46 | after.tv_sec--, after.tv_usec += 1000000; | |
47 | printt("real", &after); | |
48 | printt("user", &ru.ru_utime); | |
49 | printt("sys ", &ru.ru_stime); | |
064b89c9 | 50 | fprintf(stderr, "\n"); |
b1198826 | 51 | exit (status>>8); |
63a84c73 BJ |
52 | } |
53 | ||
b1198826 SL |
54 | printt(s, tv) |
55 | char *s; | |
56 | struct timeval *tv; | |
63a84c73 | 57 | { |
63a84c73 | 58 | |
b1198826 | 59 | fprintf(stderr, "%9d.%01d %s ", tv->tv_sec, tv->tv_usec/100000, s); |
63a84c73 | 60 | } |