aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2013-01-31 01:56:57 -0600
committerRob Landley <rob@landley.net>2013-01-31 01:56:57 -0600
commit9d6e08806fcb8db77339ccd0e7ac22b38f3dc92f (patch)
tree94c2a4e1bc23f3a991d74b04f2f9a9c0ca0896a8 /toys
parent4e998e5dcaca307f56211192593c4267d513eb30 (diff)
downloadtoybox-9d6e08806fcb8db77339ccd0e7ac22b38f3dc92f.tar.gz
Add time command (that only does posix mode).
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/time.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/toys/posix/time.c b/toys/posix/time.c
new file mode 100644
index 00000000..0ccba19c
--- /dev/null
+++ b/toys/posix/time.c
@@ -0,0 +1,48 @@
+/* time.c - time a simple command
+ *
+ * Copyright 2013 Rob Landley <rob@landley.net>
+ *
+ * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html
+
+USE_TIME(NEWTOY(time, "<1^p", TOYFLAG_USR|TOYFLAG_BIN))
+
+config TIME
+ bool "time"
+ default y
+ help
+ usage: time [-p] COMMAND [ARGS...]
+
+ Run command line and report real, user, and system time elapsed in seconds.
+ (real = clock on the wall, user = cpu used by command's code,
+ system = cpu used by OS on behalf of command.)
+
+ -p posix mode (ignored)
+*/
+
+#include "toys.h"
+
+void time_main(void)
+{
+ pid_t pid;
+ struct timeval tv, tv2;
+
+ gettimeofday(&tv, NULL);
+ if (!(pid = fork())) xexec(toys.optargs);
+ else {
+ int stat;
+ struct rusage ru;
+ float r, u, s;
+
+ wait4(pid, &stat, 0, &ru);
+ gettimeofday(&tv2, NULL);
+ if (tv.tv_usec > tv2.tv_usec) {
+ tv2.tv_usec += 1000000;
+ tv2.tv_sec--;
+ }
+ r = (tv2.tv_sec-tv.tv_sec)+((tv2.tv_usec-tv.tv_usec)/1000000.0);
+ u = ru.ru_utime.tv_sec+(ru.ru_utime.tv_usec/1000000.0);
+ s = ru.ru_stime.tv_sec+(ru.ru_stime.tv_usec/1000000.0);
+ fprintf(stderr, "real %f\nuser %f\nsys %f\n", r, u, s);
+ toys.exitval = WIFEXITED(stat) ? WEXITSTATUS(stat) : WTERMSIG(stat);
+ }
+}