diff options
author | stefanf <stefanf@FreeBSD.org> | 2005-12-04 18:44:21 +0000 |
---|---|---|
committer | stefanf <stefanf@FreeBSD.org> | 2005-12-04 18:44:21 +0000 |
commit | 5c1966823e42873a2101b2690142669884e27d4b (patch) | |
tree | 2a17fd66522f5f32763b7ad240adeb0c635b360e /bin/sh/eval.c | |
parent | 097d1b86b3a4c615f8b2519a13fcbf9a625cff4e (diff) | |
download | FreeBSD-src-5c1966823e42873a2101b2690142669884e27d4b.zip FreeBSD-src-5c1966823e42873a2101b2690142669884e27d4b.tar.gz |
Add the times builtin. It reports the user and system time for the shell
itself and its children. Instead of calling times() (as implied by POSIX) this
implementation directly calls getrusage() to get the times because this is more
convenient.
Diffstat (limited to 'bin/sh/eval.c')
-rw-r--r-- | bin/sh/eval.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/bin/sh/eval.c b/bin/sh/eval.c index fecc4de..031cab3 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include <signal.h> #include <stdlib.h> #include <unistd.h> +#include <sys/resource.h> #include <sys/wait.h> /* For WIFSIGNALED(status) */ #include <errno.h> @@ -1078,3 +1079,28 @@ execcmd(int argc, char **argv) } return 0; } + + +int +timescmd(int argc __unused, char **argv __unused) +{ + struct rusage ru; + long shumins, shsmins, chumins, chsmins; + double shusecs, shssecs, chusecs, chssecs; + + if (getrusage(RUSAGE_SELF, &ru) < 0) + return 1; + shumins = ru.ru_utime.tv_sec / 60; + shusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; + shsmins = ru.ru_stime.tv_sec / 60; + shssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; + if (getrusage(RUSAGE_CHILDREN, &ru) < 0) + return 1; + chumins = ru.ru_utime.tv_sec / 60; + chusecs = ru.ru_utime.tv_sec % 60 + ru.ru_utime.tv_usec / 1000000.; + chsmins = ru.ru_stime.tv_sec / 60; + chssecs = ru.ru_stime.tv_sec % 60 + ru.ru_stime.tv_usec / 1000000.; + out1fmt("%ldm%.3fs %ldm%.3fs\n%ldm%.3fs %ldm%.3fs\n", shumins, + shusecs, shsmins, shssecs, chumins, chusecs, chsmins, chssecs); + return 0; +} |