summaryrefslogtreecommitdiffstats
path: root/usr.bin/time
diff options
context:
space:
mode:
authorroberto <roberto@FreeBSD.org>1999-03-10 17:22:12 +0000
committerroberto <roberto@FreeBSD.org>1999-03-10 17:22:12 +0000
commitc346790f67fead19172ab7bb3c8de293882079a3 (patch)
treebc004ef6da7e466cc3f93ee3a6e22b93bcee0621 /usr.bin/time
parent6559ff40470c06ff38dedb173bbc3b2a7233a294 (diff)
downloadFreeBSD-src-c346790f67fead19172ab7bb3c8de293882079a3.zip
FreeBSD-src-c346790f67fead19172ab7bb3c8de293882079a3.tar.gz
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>
Diffstat (limited to 'usr.bin/time')
-rw-r--r--usr.bin/time/time.141
-rw-r--r--usr.bin/time/time.c43
2 files changed, 63 insertions, 21 deletions
diff --git a/usr.bin/time/time.1 b/usr.bin/time/time.1
index f0fd00a..0d51613 100644
--- a/usr.bin/time/time.1
+++ b/usr.bin/time/time.1
@@ -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
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c
index 8d3d8a0..1400a97 100644
--- a/usr.bin/time/time.c
+++ b/usr.bin/time/time.c
@@ -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);
}
OpenPOWER on IntegriCloud