Add '-t' option for tcsh-style time output.

This commit is contained in:
simonb 2020-04-23 07:54:53 +00:00
parent 17fe51dabd
commit ccc205ceda
5 changed files with 40 additions and 24 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.31 2019/01/05 16:54:00 christos Exp $ */
/* $NetBSD: extern.h,v 1.32 2020/04/23 07:54:53 simonb Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -297,7 +297,7 @@ void plist(struct varent *);
*/
void donice(Char **, struct command *);
void dotime(Char **, struct command *);
void prusage1(FILE *, const char *, struct rusage *, struct rusage *,
void prusage1(FILE *, const char *, int, struct rusage *, struct rusage *,
struct timespec *, struct timespec *);
void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *,
struct timespec *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: time.c,v 1.21 2017/07/15 14:35:55 christos Exp $ */
/* $NetBSD: time.c,v 1.22 2020/04/23 07:54:53 simonb Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: time.c,v 1.21 2017/07/15 14:35:55 christos Exp $");
__RCSID("$NetBSD: time.c,v 1.22 2020/04/23 07:54:53 simonb Exp $");
#endif
#endif /* not lint */
@ -49,7 +49,7 @@ __RCSID("$NetBSD: time.c,v 1.21 2017/07/15 14:35:55 christos Exp $");
/*
* C Shell - routines handling process timing and niceing
*/
static void pdeltat(FILE *, struct timeval *, struct timeval *);
static void pdeltat(FILE *, int, struct timeval *, struct timeval *);
static void pcsecs(FILE *, long);
#ifndef NOT_CSH
@ -138,12 +138,13 @@ prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timespec *e,
cp = short2str(vp->vec[1]);
else
cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
prusage1(fp, cp, r0, r1, e, b);
prusage1(fp, cp, 1, r0, r1, e, b);
}
#endif
void
prusage1(FILE *fp, const char *cp, struct rusage *r0, struct rusage *r1,
prusage1(FILE *fp, const char *cp, int prec,
struct rusage *r0, struct rusage *r1,
struct timespec *e, struct timespec *b)
{
long i;
@ -201,10 +202,10 @@ prusage1(FILE *fp, const char *cp, struct rusage *r0, struct rusage *r1,
(void)fprintf(fp, "%ld", r1->ru_minflt - r0->ru_minflt);
break;
case 'S': /* system CPU time used */
pdeltat(fp, &r1->ru_stime, &r0->ru_stime);
pdeltat(fp, prec, &r1->ru_stime, &r0->ru_stime);
break;
case 'U': /* user CPU time used */
pdeltat(fp, &r1->ru_utime, &r0->ru_utime);
pdeltat(fp, prec, &r1->ru_utime, &r0->ru_utime);
break;
case 'W': /* number of swaps */
i = r1->ru_nswap - r0->ru_nswap;
@ -234,13 +235,13 @@ prusage1(FILE *fp, const char *cp, struct rusage *r0, struct rusage *r1,
}
static void
pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0)
pdeltat(FILE *fp, int prec, struct timeval *t1, struct timeval *t0)
{
struct timeval td;
timersub(t1, t0, &td);
(void)fprintf(fp, "%ld.%01ld", (long)td.tv_sec,
(long)(td.tv_usec / 100000));
(void)fprintf(fp, "%ld.%0*ld", (long)td.tv_sec,
prec, (long)(td.tv_usec / 100000));
}
#define P2DIG(fp, i) (void)fprintf(fp, "%ld%ld", (i) / 10, (i) % 10)

View File

@ -1,5 +1,5 @@
/* $NetBSD: ext.h,v 1.3 2017/07/15 14:34:08 christos Exp $ */
/* $NetBSD: ext.h,v 1.4 2020/04/23 07:54:53 simonb Exp $ */
/* borrowed from ../../bin/csh/extern.h */
void prusage1(FILE *, const char *fmt, struct rusage *, struct rusage *,
void prusage1(FILE *, const char *fmt, int, struct rusage *, struct rusage *,
struct timespec *, struct timespec *);

View File

@ -1,4 +1,4 @@
.\" $NetBSD: time.1,v 1.28 2017/07/15 14:40:36 wiz Exp $
.\" $NetBSD: time.1,v 1.29 2020/04/23 07:54:53 simonb Exp $
.\"
.\" Copyright (c) 1980, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)time.1 8.1 (Berkeley) 6/6/93
.\"
.Dd July 15, 2017
.Dd April 23, 2020
.Dt TIME 1
.Os
.Sh NAME
@ -124,6 +124,13 @@ structure are printed; see below.
.It Fl p
The output is formatted as specified by
.St -p1003.2-92 .
.It Fl t
Displays information in the format used by default the
.Nm
builtin of
.Xr tcsh 1
uses (%Uu %Ss %E %P\\t%X+%Dk %I+%Oio %Fpf+%Ww) with
three decimal places for time values.
.El
.Pp
Some shells, such as

View File

@ -1,4 +1,4 @@
/* $NetBSD: time.c,v 1.23 2017/07/15 14:34:08 christos Exp $ */
/* $NetBSD: time.c,v 1.24 2020/04/23 07:54:53 simonb Exp $ */
/*
* Copyright (c) 1987, 1988, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\
#if 0
static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: time.c,v 1.23 2017/07/15 14:34:08 christos Exp $");
__RCSID("$NetBSD: time.c,v 1.24 2020/04/23 07:54:53 simonb Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -67,7 +67,7 @@ int
main(int argc, char ** volatile argv)
{
int pid;
int ch, status;
int ch, status, prec;
int volatile portableflag;
int volatile lflag;
const char *decpt;
@ -79,20 +79,21 @@ main(int argc, char ** volatile argv)
(void)setlocale(LC_ALL, "");
lflag = portableflag = 0;
prec = 1;
fmt = NULL;
while ((ch = getopt(argc, argv, "cf:lp")) != -1) {
while ((ch = getopt(argc, argv, "cf:lpt")) != -1) {
switch (ch) {
case 'f':
fmt = optarg;
portableflag = 0;
lflag = 0;
break;
case 'c':
case 'c': /* csh format */
fmt = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
portableflag = 0;
lflag = 0;
break;
case 'p':
case 'p': /* POSIX.2 format */
portableflag = 1;
fmt = NULL;
lflag = 0;
@ -102,6 +103,12 @@ main(int argc, char ** volatile argv)
portableflag = 0;
fmt = NULL;
break;
case 't': /* tcsh format */
fmt = "%Uu %Ss %E %P\t%X+%Dk %I+%Oio %Fpf+%Ww";
prec = 3;
portableflag = 0;
lflag = 0;
break;
case '?':
default:
usage();
@ -143,7 +150,7 @@ main(int argc, char ** volatile argv)
static struct rusage null_ru;
before.tv_sec = 0;
before.tv_nsec = 0;
prusage1(stderr, fmt, &null_ru, &ru, &after, &before);
prusage1(stderr, fmt, prec, &null_ru, &ru, &after, &before);
} else if (portableflag) {
prts("real ", decpt, &after, "\n");
prtv("user ", decpt, &ru.ru_utime, "\n");
@ -184,7 +191,8 @@ static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s [-clp] [-f <fmt>] utility [argument ...]\n",
(void)fprintf(stderr,
"Usage: %s [-clpt] [-f <fmt>] utility [argument ...]\n",
getprogname());
exit(EXIT_FAILURE);
}