bug-coreutils
[Top][All Lists]
Advanced

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

"pr -D FORMAT" fixes, to match "date +FORMAT"


From: Paul Eggert
Subject: "pr -D FORMAT" fixes, to match "date +FORMAT"
Date: Fri, 18 Mar 2005 22:25:22 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

The command pr -D FORMAT is documented to use the same format strings
that date +FORMAT does, but this isn't currently true because the
former uses strftime and the latter uses nstrftime.  For example, pr
-D%N doesn't work.  I installed this patch to fix things.  This also
has the effect of removing a portability assumption when strftime
returns zero.

2005-03-18  Paul Eggert  <address@hidden>

        * NEWS: pr -D "FORMAT" now accepts the same formats that
        date +"FORMAT" does.
        * src/pr.c: Include strftime.h, timespec.h.
        (init_header): Obtain and format nanosecond part of time stamp.

Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.274
retrieving revision 1.275
diff -p -u -r1.274 -r1.275
--- NEWS        19 Mar 2005 00:45:08 -0000      1.274
+++ NEWS        19 Mar 2005 06:20:01 -0000      1.275
@@ -36,6 +36,8 @@ GNU coreutils NEWS                      
   ls now refuses to generate time stamps containing more than 1000 bytes, to
   foil potential denial-of-service attacks on hosts with very large stacks.
 
+  "pr -D FORMAT" now accepts the same formats that "date +FORMAT" does.
+
   test now detects integer overflow when evaluating large integers,
   rather than silently wrapping around.
 
Index: src/pr.c
===================================================================
RCS file: /fetish/cu/src/pr.c,v
retrieving revision 1.130
retrieving revision 1.131
diff -p -u -r1.130 -r1.131
--- src/pr.c    15 Mar 2005 18:05:13 -0000      1.130
+++ src/pr.c    19 Mar 2005 06:18:19 -0000      1.131
@@ -320,6 +320,8 @@
 #include "inttostr.h"
 #include "mbswidth.h"
 #include "posixver.h"
+#include "strftime.h"
+#include "timespec.h"
 #include "xstrtol.h"
 
 #if ! (HAVE_DECL_STRTOUMAX || defined strtoumax)
@@ -1665,30 +1667,39 @@ init_header (char *filename, int desc)
 {
   char *buf = NULL;
   struct stat st;
+  time_t s;
+  int ns;
   struct tm *tm;
 
   /* If parallel files or standard input, use current date. */
   if (STREQ (filename, "-"))
     desc = -1;
-  if (desc < 0 || fstat (desc, &st) != 0)
-    st.st_mtime = time (NULL);
+  if (0 <= desc && fstat (desc, &st) == 0)
+    {
+      s = st.st_mtime;
+      ns = TIMESPEC_NS (st.st_mtim);
+    }
+  else
+    {
+      static struct timespec timespec;
+      if (! timespec.tv_sec)
+       gettime (&timespec);
+      s = timespec.tv_sec;
+      ns = timespec.tv_nsec;
+    }
 
-  tm = localtime (&st.st_mtime);
+  tm = localtime (&s);
   if (tm == NULL)
     {
-      buf = xmalloc (INT_BUFSIZE_BOUND (long int));
-      sprintf (buf, "%ld", (long int) st.st_mtime);
+      buf = xmalloc (INT_BUFSIZE_BOUND (long int)
+                    + MAX (10, INT_BUFSIZE_BOUND (int)));
+      sprintf (buf, "%ld.9d", (long int) s, ns);
     }
   else
     {
-      size_t bufsize = 0;
-      for (;;)
-       {
-         buf = x2nrealloc (buf, &bufsize, sizeof *buf);
-         *buf = '\1';
-         if (strftime (buf, bufsize, date_format, tm) || *buf == '\0')
-           break;
-       }
+      size_t bufsize = nstrftime (NULL, SIZE_MAX, date_format, tm, 0, ns) + 1;
+      buf = xmalloc (bufsize);
+      nstrftime (buf, bufsize, date_format, tm, 0, ns);
     }
 
   if (date_text)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]