summaryrefslogtreecommitdiffstats
path: root/usr.bin/time
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2000-10-28 21:48:53 +0000
committerobrien <obrien@FreeBSD.org>2000-10-28 21:48:53 +0000
commit20c43a6b76cab5f9f12fe61efd856953cf317341 (patch)
treeca14afd5303f233118bdf31a4e86b5bf41451803 /usr.bin/time
parentf8252177682b60816e68ec8c9e9ba416abb1762a (diff)
downloadFreeBSD-src-20c43a6b76cab5f9f12fe61efd856953cf317341.zip
FreeBSD-src-20c43a6b76cab5f9f12fe61efd856953cf317341.tar.gz
Add a new "-h" Human-friendly h/m/s output format.
Reviewed by: bde
Diffstat (limited to 'usr.bin/time')
-rw-r--r--usr.bin/time/time.16
-rw-r--r--usr.bin/time/time.c44
2 files changed, 45 insertions, 5 deletions
diff --git a/usr.bin/time/time.1 b/usr.bin/time/time.1
index 48cd78d..e896f1c 100644
--- a/usr.bin/time/time.1
+++ b/usr.bin/time/time.1
@@ -40,7 +40,8 @@
.Nd time command execution
.Sh SYNOPSIS
.Nm
-.Op Fl alp
+.Op Fl al
+.Op Fl h | Fl p
.Op Fl o Ar file
.Ar command
.Sh DESCRIPTION
@@ -73,6 +74,9 @@ If the
flag is used, append to the specified file rather than overwriting
it.
Otherwise, this option has no effect.
+.It Fl h
+Print times in a human friendly format. Times are printed in minutes, hours,
+etc. as appropiate.
.It Fl l
The contents of the
.Em rusage
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c
index e802e4d..5a35f68 100644
--- a/usr.bin/time/time.c
+++ b/usr.bin/time/time.c
@@ -61,6 +61,7 @@ static const char rcsid[] =
#include <signal.h>
static int getstathz __P((void));
+static void humantime __P((FILE *, long, long));
static void usage __P((void));
int
@@ -69,19 +70,22 @@ main(argc, argv)
char **argv;
{
register int pid;
- int aflag, ch, lflag, status, pflag;
+ int aflag, ch, hflag, lflag, status, pflag;
struct timeval before, after;
struct rusage ru;
FILE *out = stderr;
char *ofn = NULL;
int exitonsig = 0; /* Die with same signal as child */
- aflag = lflag = pflag = 0;
- while ((ch = getopt(argc, argv, "alo:p")) != -1)
+ aflag = hflag = lflag = pflag = 0;
+ while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
switch((char)ch) {
case 'a':
aflag = 1;
break;
+ case 'h':
+ hflag = 1;
+ break;
case 'l':
lflag = 1;
break;
@@ -144,6 +148,13 @@ main(argc, argv)
ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
fprintf(out, "sys %ld.%02ld\n",
ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
+ } else if (hflag) {
+ humantime(out, after.tv_sec, after.tv_usec/10000);
+ fprintf(out, " real%c", hflag ? '\t' : ' ');
+ humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
+ fprintf(out, " user%c", hflag ? '\t' : ' ');
+ humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
+ fprintf(out, " sys\n");
} else {
fprintf(out, "%9ld.%02ld real ",
after.tv_sec, after.tv_usec/10000);
@@ -207,7 +218,7 @@ main(argc, argv)
static void
usage()
{
- fprintf(stderr, "usage: time [-alp] [-o file] command\n");
+ fprintf(stderr, "usage: time [-al] [-h|-p] [-o file] command\n");
exit(1);
}
@@ -228,3 +239,28 @@ getstathz()
err(1, "sysctl kern.clockrate");
return clockrate.stathz;
}
+
+static void
+humantime(out, sec, usec)
+ FILE *out;
+ long sec;
+ long usec;
+{
+ long days, hrs, mins;
+
+ days = sec / (60L * 60 * 24);
+ sec %= (60L * 60 * 24);
+ hrs = sec / (60L * 60);
+ sec %= (60L * 60);
+ mins = sec / 60;
+ sec %= 60;
+
+ fprintf(out, "\t");
+ if (days)
+ fprintf(out, "%ldd", days);
+ if (hrs)
+ fprintf(out, "%ldh", hrs);
+ if (mins)
+ fprintf(out, "%ldm", mins);
+ fprintf(out, "%ld.%02lds", sec, usec);
+}
OpenPOWER on IntegriCloud