summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorstefanf <stefanf@FreeBSD.org>2005-12-04 18:44:21 +0000
committerstefanf <stefanf@FreeBSD.org>2005-12-04 18:44:21 +0000
commit5c1966823e42873a2101b2690142669884e27d4b (patch)
tree2a17fd66522f5f32763b7ad240adeb0c635b360e /bin
parent097d1b86b3a4c615f8b2519a13fcbf9a625cff4e (diff)
downloadFreeBSD-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')
-rw-r--r--bin/sh/builtins.def1
-rw-r--r--bin/sh/eval.c26
-rw-r--r--bin/sh/eval.h1
-rw-r--r--bin/sh/sh.17
4 files changed, 34 insertions, 1 deletions
diff --git a/bin/sh/builtins.def b/bin/sh/builtins.def
index 7bfe5d5..4dbd837 100644
--- a/bin/sh/builtins.def
+++ b/bin/sh/builtins.def
@@ -78,6 +78,7 @@ returncmd return
setcmd set
setvarcmd setvar
shiftcmd shift
+timescmd times
trapcmd trap
truecmd : true
typecmd type
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;
+}
diff --git a/bin/sh/eval.h b/bin/sh/eval.h
index 2f8471e..01c914a 100644
--- a/bin/sh/eval.h
+++ b/bin/sh/eval.h
@@ -56,6 +56,7 @@ int returncmd(int, char **);
int falsecmd(int, char **);
int truecmd(int, char **);
int execcmd(int, char **);
+int timescmd(int, char **);
int commandcmd(int, char **);
/* in_function returns nonzero if we are currently evaluating a function */
diff --git a/bin/sh/sh.1 b/bin/sh/sh.1
index 6fe7795..ff0cf81 100644
--- a/bin/sh/sh.1
+++ b/bin/sh/sh.1
@@ -32,7 +32,7 @@
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$
.\"
-.Dd October 29, 2005
+.Dd December 4, 2005
.Dt SH 1
.Os
.Sh NAME
@@ -1952,6 +1952,11 @@ A shift sets the value of $1 to the value of $2,
the value of $2 to the value of $3, and so on,
decreasing the value of $# by one.
If there are zero positional parameters, shifting does not do anything.
+.It Ic times
+Print the amount of time spent executing the shell and its children.
+The first output line shows the user and system times for the shell
+itself, the second one contains the user and system times for the
+children.
.It Ic trap Oo Ar action Oc Ar signal ...
Cause the shell to parse and execute
.Ar action
OpenPOWER on IntegriCloud