Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add '-t' option for tcsh-style time output.



details:   https://anonhg.NetBSD.org/src/rev/7e5e3c176c90
branches:  trunk
changeset: 1009438:7e5e3c176c90
user:      simonb <simonb%NetBSD.org@localhost>
date:      Thu Apr 23 07:54:53 2020 +0000

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

diffstat:

 bin/csh/extern.h    |   4 ++--
 bin/csh/time.c      |  21 +++++++++++----------
 usr.bin/time/ext.h  |   4 ++--
 usr.bin/time/time.1 |  11 +++++++++--
 usr.bin/time/time.c |  24 ++++++++++++++++--------
 5 files changed, 40 insertions(+), 24 deletions(-)

diffs (218 lines):

diff -r 11c97284ef0a -r 7e5e3c176c90 bin/csh/extern.h
--- a/bin/csh/extern.h  Thu Apr 23 07:39:07 2020 +0000
+++ b/bin/csh/extern.h  Thu Apr 23 07:54:53 2020 +0000
@@ -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 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 *);
diff -r 11c97284ef0a -r 7e5e3c176c90 bin/csh/time.c
--- a/bin/csh/time.c    Thu Apr 23 07:39:07 2020 +0000
+++ b/bin/csh/time.c    Thu Apr 23 07:54:53 2020 +0000
@@ -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 @@
 /*
  * 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 @@
        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 @@
                (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 @@
 }
 
 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)
diff -r 11c97284ef0a -r 7e5e3c176c90 usr.bin/time/ext.h
--- a/usr.bin/time/ext.h        Thu Apr 23 07:39:07 2020 +0000
+++ b/usr.bin/time/ext.h        Thu Apr 23 07:54:53 2020 +0000
@@ -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 *);
diff -r 11c97284ef0a -r 7e5e3c176c90 usr.bin/time/time.1
--- a/usr.bin/time/time.1       Thu Apr 23 07:39:07 2020 +0000
+++ b/usr.bin/time/time.1       Thu Apr 23 07:54:53 2020 +0000
@@ -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 @@
 .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
diff -r 11c97284ef0a -r 7e5e3c176c90 usr.bin/time/time.c
--- a/usr.bin/time/time.c       Thu Apr 23 07:39:07 2020 +0000
+++ b/usr.bin/time/time.c       Thu Apr 23 07:54:53 2020 +0000
@@ -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 @@
 #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 @@
 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 @@
        (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 @@
                        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 @@
                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 @@
 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);
 }



Home | Main Index | Thread Index | Old Index