summaryrefslogtreecommitdiff
path: root/src/pr.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2005-03-19 06:18:19 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2005-03-19 06:18:19 +0000
commit2bad486665c60ac3ad25466f6fcaed1cb92e9150 (patch)
treeae1539ded4bf9848c20a30d7f60c2925b82b3bbd /src/pr.c
parentbd73d9d8b643b0fd433d2e8b234afacb6b373d80 (diff)
downloadcoreutils-2bad486665c60ac3ad25466f6fcaed1cb92e9150.tar.xz
Include strftime.h, timespec.h.
(init_header): Obtain and format nanosecond part of time stamp.
Diffstat (limited to 'src/pr.c')
-rw-r--r--src/pr.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/pr.c b/src/pr.c
index a5882aa41..8a84ac841 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -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)