summaryrefslogtreecommitdiffstats
path: root/usr.bin/time
diff options
context:
space:
mode:
authorphk <phk@FreeBSD.org>1998-07-24 07:19:29 +0000
committerphk <phk@FreeBSD.org>1998-07-24 07:19:29 +0000
commit42571197008f6a841e0dca0d38cde8c8798984bb (patch)
treeb0f004a5680bc8974ef464480a14466ba686e4ab /usr.bin/time
parent2e8d660a6e06bae5d9351ab209a17e5bdb4803fa (diff)
downloadFreeBSD-src-42571197008f6a841e0dca0d38cde8c8798984bb.zip
FreeBSD-src-42571197008f6a841e0dca0d38cde8c8798984bb.tar.gz
By default, /usr/bin/time writes its output to stderr. Two options
have been added to time(1) to write output to an alternative destination. Option "-f filename" will write to filename, and filename can be - to write to stdout. Option "-a filename" will append the output to filename. Time(1) man page has been updated to reflect the change. PR: 7368 Submitted by: Steven G. Kargl <kargl@troutmask.apl.washington.edu>
Diffstat (limited to 'usr.bin/time')
-rw-r--r--usr.bin/time/time.124
-rw-r--r--usr.bin/time/time.c64
2 files changed, 69 insertions, 19 deletions
diff --git a/usr.bin/time/time.1 b/usr.bin/time/time.1
index 3033096..b095281 100644
--- a/usr.bin/time/time.1
+++ b/usr.bin/time/time.1
@@ -39,6 +39,8 @@
.Nd time command execution
.Sh SYNOPSIS
.Nm
+.Op Fl a Ar file
+.Op Fl f Ar file
.Op Fl l
.Ar command
.Sh DESCRIPTION
@@ -66,6 +68,28 @@ process.
.Pp
Available options:
.Bl -tag -width Ds
+.It Fl a Ar file
+Append the output of
+.Nm
+to
+.Ar file
+instead of writing to stderr.
+.It Fl f Ar file
+Write the output to
+.Ar file
+instead of stderr. If
+.Ar file
+exists, then
+.Nm
+will overwrite the file if premissions permit such an operation.
+The output can be sent to stdout by giving
+a file name
+.Do
+-
+.Dc
+to the
+.Fl f
+option.
.It Fl l
The contents of the
.Em rusage
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c
index fea1e5a..ff703fd 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$";
+ "$Id: time.c,v 1.6 1997/08/14 06:48:59 charnier Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -56,6 +56,7 @@ static const char rcsid[] =
#include <err.h>
#include <stdio.h>
#include <unistd.h>
+#include <string.h>
static int getstathz __P((void));
static void usage __P((void));
@@ -65,14 +66,36 @@ main(argc, argv)
int argc;
char **argv;
{
+ extern char *optarg;
+ extern int optind;
+
register int pid;
int ch, status, lflag;
struct timeval before, after;
struct rusage ru;
+ FILE *out = NULL;
lflag = 0;
- while ((ch = getopt(argc, argv, "l")) != -1)
+ while ((ch = getopt(argc, argv, "a:f:l")) != -1)
switch((char)ch) {
+ case 'a':
+ if (out)
+ err(1, optarg);
+ out = fopen(optarg, "a");
+ if (!out)
+ err(1, optarg);
+ break;
+ case 'f':
+ if (out)
+ err(1, optarg);
+ if (strcmp(optarg, "-") == 0)
+ out = stdout;
+ else {
+ out = fopen(optarg, "w");
+ if (!out)
+ err(1, optarg);
+ }
+ break;
case 'l':
lflag = 1;
break;
@@ -85,6 +108,9 @@ main(argc, argv)
exit(0);
argv += optind;
+ if (!out)
+ out = stderr;
+
gettimeofday(&before, (struct timezone *)NULL);
switch(pid = vfork()) {
case -1: /* error */
@@ -107,10 +133,10 @@ main(argc, argv)
after.tv_usec -= before.tv_usec;
if (after.tv_usec < 0)
after.tv_sec--, after.tv_usec += 1000000;
- fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
- fprintf(stderr, "%9ld.%02ld user ",
+ 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(stderr, "%9ld.%02ld sys\n",
+ fprintf(out, "%9ld.%02ld sys\n",
ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
if (lflag) {
int hz = getstathz();
@@ -126,33 +152,33 @@ main(argc, argv)
if (ticks == 0)
ticks = 1;
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_maxrss, "maximum resident set size");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_ixrss / ticks, "average shared memory size");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_idrss / ticks, "average unshared data size");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_isrss / ticks, "average unshared stack size");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_minflt, "page reclaims");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_majflt, "page faults");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_nswap, "swaps");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_inblock, "block input operations");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_oublock, "block output operations");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_msgsnd, "messages sent");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_msgrcv, "messages received");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_nsignals, "signals received");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_nvcsw, "voluntary context switches");
- fprintf(stderr, "%10ld %s\n",
+ fprintf(out, "%10ld %s\n",
ru.ru_nivcsw, "involuntary context switches");
}
exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
OpenPOWER on IntegriCloud