Add a new "-h" Human-friendly h/m/s output format.

Reviewed by:	bde
This commit is contained in:
obrien 2000-10-28 21:48:53 +00:00
parent f825217768
commit 20c43a6b76
2 changed files with 45 additions and 5 deletions

View File

@ -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

View File

@ -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);
}