Adds the '-p' option to make time(1) output POSIX.2 compliant.

Updates the manpage as well.

I've rewritten the patch as it was for 2.2.7. It can probably be put
into 3.1-STABLE as well.

PR:		bin/10515
Submitted by:	Jens Schweikhardt <schweikh@noc.dfn.de>
This commit is contained in:
Ollivier Robert 1999-03-10 17:22:12 +00:00
parent 6fe7bbc584
commit 844338c286
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=44640
2 changed files with 63 additions and 21 deletions

View File

@ -39,7 +39,7 @@
.Nd time command execution
.Sh SYNOPSIS
.Nm
.Op Fl al
.Op Fl alp
.Op Fl o Ar file
.Ar command
.Sh DESCRIPTION
@ -60,10 +60,9 @@ finishes,
writes to the standard error stream,
(in seconds):
the total time elapsed,
time consumed by system overhead,
and the time used to execute the
the time used to execute the
.Ar command
process.
process and the time consumed by system overhead.
.Pp
Available options:
.Bl -tag -width Ds
@ -84,22 +83,44 @@ instead of stderr. If
exists and the
.Fl a
flag is not specified, the file will be overwritten.
.It Fl p
Makes
.Nm
output POSIX.2 compliant (each time is printed on its own line).
.El
.Pp
The
.Xr csh 1
has its own and syntactically different builtin version of
Most shells (including
.Xr csh 1 )
have their own and syntactically different builtin version of
.Nm time .
The command described here
is available as
.Pa /usr/bin/time
to
.Xr csh
users.
to users of those shells.
.Sh DIAGNOSTICS
If
.Ar command
could be timed successfully, its exit status is returned. In case
.Ar command
terminated abnormally, a warning message is output to stderr.
If the
.Ar command
was found but could not be run, the exit status is 126.
If no
.Ar command
could be found at all, the exit status is 127.
If
.Nm
encounters any other error, the exit status is between 1 and 125
included.
.Sh SEE ALSO
.Xr csh 1 ,
.Xr getrusage 2 ,
.Xr wait 2
.Sh STANDARDS
The
.Nm
utility is expected to conform to ISO/IEC 9945-2:1993 (``POSIX'').
.Sh HISTORY
A
.Nm

View File

@ -42,7 +42,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
"$Id: time.c,v 1.11 1998/08/24 10:16:59 cracauer Exp $";
"$Id: time.c,v 1.12 1998/10/13 14:52:32 des Exp $";
#endif /* not lint */
#include <sys/types.h>
@ -55,6 +55,7 @@ static const char rcsid[] =
#include <err.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
@ -71,15 +72,15 @@ main(argc, argv)
extern int optind;
register int pid;
int aflag, ch, lflag, status;
int aflag, ch, 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 = 0;
while ((ch = getopt(argc, argv, "alo:")) != -1)
aflag = lflag = pflag = 0;
while ((ch = getopt(argc, argv, "alo:p")) != -1)
switch((char)ch) {
case 'a':
aflag = 1;
@ -90,6 +91,9 @@ main(argc, argv)
case 'o':
ofn = optarg;
break;
case 'p':
pflag = 1;
break;
case '?':
default:
usage();
@ -111,9 +115,13 @@ main(argc, argv)
err(1, "time");
/* NOTREACHED */
case 0: /* child */
errno = 0;
execvp(*argv, argv);
warn("%s", *argv);
_exit(1);
if (errno == ENOENT)
_exit(127); /* POSIX: utility could not be found */
else
_exit(126); /* POSIX: utility could not be invoked */
/* NOTREACHED */
}
/* parent */
@ -129,11 +137,24 @@ main(argc, argv)
after.tv_usec -= before.tv_usec;
if (after.tv_usec < 0)
after.tv_sec--, after.tv_usec += 1000000;
fprintf(out, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
fprintf(out, "%9ld.%02ld user ",
ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
fprintf(out, "%9ld.%02ld sys\n",
ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
if (pflag) {
/* POSIX wants output that must look like
"real %f\nuser %f\nsys %f\n" and requires
at least two digits after the radix. */
fprintf(out, "real %ld.%02ld\n",
after.tv_sec, after.tv_usec/10000);
fprintf(out, "user %ld.%02ld\n",
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 {
fprintf(out, "%9ld.%02ld real ",
after.tv_sec, after.tv_usec/10000);
fprintf(out, "%9ld.%02ld user ",
ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
fprintf(out, "%9ld.%02ld sys\n",
ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
}
if (lflag) {
int hz = getstathz();
u_long ticks;
@ -189,7 +210,7 @@ main(argc, argv)
static void
usage()
{
fprintf(stderr, "usage: time [-al] [-o file] command\n");
fprintf(stderr, "usage: time [-alp] [-o file] command\n");
exit(1);
}