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.
This commit is contained in:
Stefan Farfeleder 2005-12-04 18:44:21 +00:00
parent 5df9daab27
commit 1974986a82
4 changed files with 34 additions and 1 deletions

View File

@ -78,6 +78,7 @@ returncmd return
setcmd set
setvarcmd setvar
shiftcmd shift
timescmd times
trapcmd trap
truecmd : true
typecmd type

View File

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

View File

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

View File

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