From schweikh@obsidian.noc.dfn.de Tue Mar  9 13:39:41 1999
Return-Path: <schweikh@obsidian.noc.dfn.de>
Received: from obsidian.noc.dfn.de (obsidian.noc.dfn.de [193.174.247.193])
	by hub.freebsd.org (Postfix) with ESMTP id 7A02614DB7
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  9 Mar 1999 13:39:30 -0800 (PST)
	(envelope-from schweikh@obsidian.noc.dfn.de)
Received: (from schweikh@localhost)
	by obsidian.noc.dfn.de (8.8.7/8.8.7) id WAA16301
	for FreeBSD-gnats-submit@freebsd.org; Tue, 9 Mar 1999 22:39:12 +0100 (MET)
Message-Id: <199903092139.WAA16301@obsidian.noc.dfn.de>
Date: Tue, 9 Mar 1999 22:39:12 +0100 (MET)
From: Jens Schweikhardt <schweikh@noc.dfn.de>
Sender: schweikh@obsidian.noc.dfn.de
To: FreeBSD-gnats-submit@freebsd.org
Subject: time(1) not POSIX.2 compliant (patch included)

>Number:         10515
>Category:       bin
>Synopsis:       time(1) not POSIX.2 compliant (patch included)
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar  9 13:40:00 PST 1999
>Closed-Date:    Wed Mar 10 09:23:29 PST 1999
>Last-Modified:  Wed Mar 10 09:30:01 PST 1999
>Originator:     Jens Schweikhardt
>Release:        FreeBSD 2.2.7-RELEASE i386
>Organization:
DFN
>Environment:

	2.2.7-RELEASE off of the CD ROM.

>Description:

	POSIX.2 requires time(1) to support the -p option which must
	produce output that looks like "real %f\nuser %f\nsys %f\n".
	The current format is all on one line and numbers first and
	-p is not supported.

>How-To-Repeat:

	/usr/bin/time -p
	[barfs: no -p]

>Fix:

This fix simply adds the missing -p option to time(1). It also makes
time's exit status conform to POSIX.2, which should not harm anyone
(haha, famous last words). I've also updated the man page time.1 to
reflect all changes (DIAGNOSTICS and STANDARDS sections plus more).

A small step towards POSIX compliance but a giant leap for my
application, which *needs* it :-)

	Jens Schweikhardt


diff -u against the 2.2.7R files:

--- time.c.old	Tue Mar  9 22:20:52 1999
+++ time.c	Tue Mar  9 22:20:29 1999
@@ -55,6 +55,7 @@
 
 #include <err.h>
 #include <stdio.h>
+#include <errno.h>
 #include <unistd.h>
 
 static int getstathz __P((void));
@@ -66,16 +67,19 @@
 	char **argv;
 {
 	register int pid;
-	int ch, status, lflag;
+	int ch, status, lflag, pflag;
 	struct timeval before, after;
 	struct rusage ru;
 
-	lflag = 0;
-	while ((ch = getopt(argc, argv, "l")) != -1)
+	lflag = pflag = 0;
+	while ((ch = getopt(argc, argv, "lp")) != -1)
 		switch((char)ch) {
 		case 'l':
 			lflag = 1;
 			break;
+		case 'p':
+			pflag = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -91,10 +95,16 @@
 		err(1, "time");
 		/* NOTREACHED */
 	case 0:				/* child */
+		errno = 0;
 		execvp(*argv, argv);
-		warn("%s", *argv);
-		_exit(1);
 		/* NOTREACHED */
+		if (errno == ENOENT) {
+			warn("%s", *argv);
+			_exit(127); /* POSIX: utility could not be found */
+		} else {
+			warn("%s", *argv);
+			_exit(126); /* POSIX: utility could not be invoked */
+		}
 	}
 	/* parent */
 	(void)signal(SIGINT, SIG_IGN);
@@ -107,11 +117,22 @@
 	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 ",
-	    ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
-	fprintf(stderr, "%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(stderr, "real %ld.%02ld\n", after.tv_sec, after.tv_usec/10000);
+		fprintf(stderr, "user %ld.%02ld\n",
+			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
+		fprintf(stderr, "sys %ld.%02ld\n",
+			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
+	} else {
+		fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
+		fprintf(stderr, "%9ld.%02ld user ",
+			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
+		fprintf(stderr, "%9ld.%02ld sys\n",
+			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
+	}
 	if (lflag) {
 		int hz = getstathz();
 		u_long ticks;
@@ -161,7 +182,7 @@
 static void
 usage()
 {
-	fprintf(stderr, "usage: time [-l] command\n");
+	fprintf(stderr, "usage: time [-l] [-p] command\n");
 	exit(1);
 }
 
--- time.1.old	Tue Mar  9 21:19:10 1999
+++ time.1	Tue Mar  9 22:14:05 1999
@@ -40,6 +40,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl l
+.Op Fl p
 .Ar command
 .Sh DESCRIPTION
 The
@@ -59,10 +60,9 @@
 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
@@ -71,25 +71,44 @@
 .Em rusage
 structure are printed as well.
 .El
+.Bl -tag -width Ds
+.It Fl p
+Use the POSIX output format, "real %f\\nuser %f\\nsys %f\\n".
+.El
 .Pp
-The
-.Xr csh 1
-has its own and syntactically different builtin version of
+Most shells 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
+including.
 .Sh BUGS
 The granularity of seconds on micro processors is crude and
 can result in times being reported for CPU usage which are too large by
 a second.
 .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

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: roberto 
State-Changed-When: Wed Mar 10 09:23:29 PST 1999 
State-Changed-Why:  
I rewrote the patch for 4.0-CURRENT and committed it. 
Thanks. 

From: Ollivier Robert <roberto@keltia.freenix.fr>
To: Jens Schweikhardt <schweikh@noc.dfn.de>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/10515: time(1) not POSIX.2 compliant (patch included)
Date: Wed, 10 Mar 1999 18:10:07 +0100

 According to Jens Schweikhardt:
 > >Number:         10515
 > >Category:       bin
 > >Synopsis:       time(1) not POSIX.2 compliant (patch included)
 
 I've rewritten the patch for 4.0-CURRENT and will commit it. I've kept the
 reference to csh(1) though.
 -- 
 Ollivier ROBERT -=- FreeBSD: The Power to Serve! -=- roberto@keltia.freenix.fr
 FreeBSD keltia.freenix.fr 4.0-CURRENT #70: Sat Feb 27 09:43:08 CET 1999
 
 
>Unformatted:
 To: FreeBSD-gnats-submit@freebsd.org
 Subject: time(1) not POSIX.2 compliant (patch included)
 From: schweikh@noc.dfn.de
 Reply-To: schweikh@noc.dfn.de
 X-send-pr-version: 3.2
 
 
