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