From 158d982669d3d05079da2b9cc8d8fcd8adf35a4b Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 16 Sep 2005 07:50:33 +0000 Subject: [PATCH] Include stat-time.h, and use its functions instead of the obsolete TIMESPEC_NS macro. --- src/copy.c | 7 +++---- src/cp.c | 7 +++---- src/date.c | 6 +++--- src/du.c | 48 ++++++++++++++++++++---------------------------- src/install.c | 7 +++---- src/ls.c | 27 ++++++++++++++++----------- src/pr.c | 16 +++++++--------- src/stat.c | 20 +++++++++----------- src/tail.c | 14 ++++++-------- src/touch.c | 29 +++++++++-------------------- 10 files changed, 79 insertions(+), 102 deletions(-) diff --git a/src/copy.c b/src/copy.c index b19a8d8..ba6ce48 100644 --- a/src/copy.c +++ b/src/copy.c @@ -45,6 +45,7 @@ #include "quote.h" #include "same.h" #include "savedir.h" +#include "stat-time.h" #include "utimecmp.h" #include "utimens.h" #include "xreadlink.h" @@ -1567,10 +1568,8 @@ copy_internal (char const *src_name, char const *dst_name, { struct timespec timespec[2]; - timespec[0].tv_sec = src_sb.st_atime; - timespec[0].tv_nsec = TIMESPEC_NS (src_sb.st_atim); - timespec[1].tv_sec = src_sb.st_mtime; - timespec[1].tv_nsec = TIMESPEC_NS (src_sb.st_mtim); + timespec[0] = get_stat_atime (&src_sb); + timespec[1] = get_stat_mtime (&src_sb); if (utimens (dst_name, timespec) != 0) { diff --git a/src/cp.c b/src/cp.c index 6605ebc..4e08612 100644 --- a/src/cp.c +++ b/src/cp.c @@ -33,6 +33,7 @@ #include "filenamecat.h" #include "quote.h" #include "quotearg.h" +#include "stat-time.h" #include "utimens.h" #define ASSIGN_BASENAME_STRDUPA(Dest, File_name) \ @@ -304,10 +305,8 @@ re_protect (char const *const_dst_name, size_t src_offset, { struct timespec timespec[2]; - timespec[0].tv_sec = src_sb.st_atime; - timespec[0].tv_nsec = TIMESPEC_NS (src_sb.st_atim); - timespec[1].tv_sec = src_sb.st_mtime; - timespec[1].tv_nsec = TIMESPEC_NS (src_sb.st_mtim); + timespec[0] = get_stat_atime (&src_sb); + timespec[1] = get_stat_mtime (&src_sb); if (utimens (dst_name, timespec)) { diff --git a/src/date.c b/src/date.c index ebe63ac..af9485b 100644 --- a/src/date.c +++ b/src/date.c @@ -33,6 +33,7 @@ #include "inttostr.h" #include "posixtm.h" #include "quote.h" +#include "stat-time.h" #include "fprintftime.h" /* The official name of this program (e.g., no `g' prefix). */ @@ -493,10 +494,9 @@ main (int argc, char **argv) /* (option_specified_date || set_date) */ if (reference != NULL) { - if (stat (reference, &refstats)) + if (stat (reference, &refstats) != 0) error (EXIT_FAILURE, errno, "%s", reference); - when.tv_sec = refstats.st_mtime; - when.tv_nsec = TIMESPEC_NS (refstats.st_mtim); + when = get_stat_mtime (&refstats); } else { diff --git a/src/du.c b/src/du.c index 08d29d2..84c9b4a 100644 --- a/src/du.c +++ b/src/du.c @@ -41,6 +41,7 @@ #include "quotearg.h" #include "readtokens0.h" #include "same.h" +#include "stat-time.h" #include "strftime.h" #include "xanstrftime.h" #include "xfts.h" @@ -84,10 +85,9 @@ struct duinfo /* Size of files in directory. */ uintmax_t size; - /* Latest time stamp found. If dmax == TYPE_MINIMUM (time_t) && nsec < 0, - no time stamp has been found. */ - time_t dmax; - int nsec; + /* Latest time stamp found. If tmax.tv_sec == TYPE_MINIMUM (time_t) + && tmax.tv_nsec < 0, no time stamp has been found. */ + struct timespec tmax; }; /* Initialize directory data. */ @@ -95,17 +95,16 @@ static inline void duinfo_init (struct duinfo *a) { a->size = 0; - a->dmax = TYPE_MINIMUM (time_t); - a->nsec = -1; + a->tmax.tv_sec = TYPE_MINIMUM (time_t); + a->tmax.tv_nsec = -1; } /* Set directory data. */ static inline void -duinfo_set (struct duinfo *a, uintmax_t size, time_t dmax, int nsec) +duinfo_set (struct duinfo *a, uintmax_t size, struct timespec tmax) { a->size = size; - a->dmax = dmax; - a->nsec = nsec; + a->tmax = tmax; } /* Accumulate directory data. */ @@ -113,12 +112,8 @@ static inline void duinfo_add (struct duinfo *a, struct duinfo const *b) { a->size += b->size; - if (a->dmax < b->dmax - || (a->dmax == b->dmax && a->nsec < b->nsec)) - { - a->dmax = b->dmax; - a->nsec = b->nsec; - } + if (timespec_cmp (a->tmax, b->tmax) < 0) + a->tmax = b->tmax; } /* A structure for per-directory level information. */ @@ -406,27 +401,27 @@ hash_init (void) } /* FIXME: this code is nearly identical to code in date.c */ -/* Display the date and time in WHEN/NSEC according to the format specified +/* Display the date and time in WHEN according to the format specified in TIME_FORMAT. If TIME_FORMAT is NULL, use the standard output format. Return zero if successful. */ static void -show_date (const char *format, time_t when, int nsec) +show_date (const char *format, struct timespec when) { char *out; - struct tm *tm = localtime (&when); + struct tm *tm = localtime (&when.tv_sec); if (! tm) { char buf[INT_BUFSIZE_BOUND (intmax_t)]; error (0, 0, _("time %s is out of range"), (TYPE_SIGNED (time_t) - ? imaxtostr (when, buf) - : umaxtostr (when, buf))); + ? imaxtostr (when.tv_sec, buf) + : umaxtostr (when.tv_sec, buf))); fputs (buf, stdout); return; } - out = xanstrftime (format, tm, 0, nsec); + out = xanstrftime (format, tm, 0, when.tv_nsec); fputs (out, stdout); free (out); } @@ -450,7 +445,7 @@ print_size (const struct duinfo *pdui, const char *string) if (opt_time) { putchar ('\t'); - show_date (time_format, pdui->dmax, pdui->nsec); + show_date (time_format, pdui->tmax); } printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n'); fflush (stdout); @@ -539,12 +534,9 @@ process_file (FTS *fts, FTSENT *ent) (apparent_size ? sb->st_size : ST_NBLOCKS (*sb) * ST_NBLOCKSIZE), - (time_type == time_ctime ? sb->st_ctime - : time_type == time_atime ? sb->st_atime - : sb->st_mtime), - (time_type == time_ctime ? TIMESPEC_NS (sb->st_ctim) - : time_type == time_atime ? TIMESPEC_NS (sb->st_atim) - : TIMESPEC_NS (sb->st_mtim))); + (time_type == time_mtime ? get_stat_mtime (sb) + : time_type == time_atime ? get_stat_atime (sb) + : get_stat_ctime (sb))); } level = ent->fts_level; diff --git a/src/install.c b/src/install.c index 49cc8d0..4925dc6 100644 --- a/src/install.c +++ b/src/install.c @@ -35,6 +35,7 @@ #include "mkdir-p.h" #include "modechange.h" #include "quote.h" +#include "stat-time.h" #include "utimens.h" #include "xstrtol.h" @@ -536,10 +537,8 @@ change_timestamps (const char *from, const char *to) return false; } - timespec[0].tv_sec = stb.st_atime; - timespec[0].tv_nsec = TIMESPEC_NS (stb.st_atim); - timespec[1].tv_sec = stb.st_mtime; - timespec[1].tv_nsec = TIMESPEC_NS (stb.st_mtim); + timespec[0] = get_stat_atime (&stb); + timespec[1] = get_stat_mtime (&stb); if (utimens (to, timespec)) { error (0, errno, _("cannot set time stamps for %s"), quote (to)); diff --git a/src/ls.c b/src/ls.c index bd648dc..298cf3d 100644 --- a/src/ls.c +++ b/src/ls.c @@ -123,6 +123,7 @@ int wcwidth (); #include "quote.h" #include "quotearg.h" #include "same.h" +#include "stat-time.h" #include "strftime.h" #include "strverscmp.h" #include "xstrtol.h" @@ -2855,7 +2856,8 @@ static inline int cmp_ctime (struct fileinfo const *a, struct fileinfo const *b, int (*cmp) (char const *, char const *)) { - int diff = CTIME_CMP (b->stat, a->stat); + int diff = timespec_cmp (get_stat_ctime (&b->stat), + get_stat_ctime (&a->stat)); return diff ? diff : cmp (a->name, b->name); } static int compare_ctime (V a, V b) { return cmp_ctime (a, b, xstrcoll); } @@ -2867,7 +2869,8 @@ static inline int cmp_mtime (struct fileinfo const *a, struct fileinfo const *b, int (*cmp) (char const *, char const *)) { - int diff = MTIME_CMP (b->stat, a->stat); + int diff = timespec_cmp (get_stat_mtime (&b->stat), + get_stat_mtime (&a->stat)); return diff ? diff : cmp (a->name, b->name); } static int compare_mtime (V a, V b) { return cmp_mtime (a, b, xstrcoll); } @@ -2879,7 +2882,8 @@ static inline int cmp_atime (struct fileinfo const *a, struct fileinfo const *b, int (*cmp) (char const *, char const *)) { - int diff = ATIME_CMP (b->stat, a->stat); + int diff = timespec_cmp (get_stat_atime (&b->stat), + get_stat_atime (&a->stat)); return diff ? diff : cmp (a->name, b->name); } static int compare_atime (V a, V b) { return cmp_atime (a, b, xstrcoll); } @@ -3231,7 +3235,8 @@ print_long_format (const struct fileinfo *f) size_t s; char *p; time_t when; - int when_ns IF_LINT (= 0); + int when_ns; + struct timespec when_timespec; struct tm *when_local; /* Compute mode string. On most systems, it's based on st_mode. @@ -3245,19 +3250,19 @@ print_long_format (const struct fileinfo *f) switch (time_type) { case time_ctime: - when = f->stat.st_ctime; - when_ns = TIMESPEC_NS (f->stat.st_ctim); + when_timespec = get_stat_ctime (&f->stat); break; case time_mtime: - when = f->stat.st_mtime; - when_ns = TIMESPEC_NS (f->stat.st_mtim); + when_timespec = get_stat_mtime (&f->stat); break; case time_atime: - when = f->stat.st_atime; - when_ns = TIMESPEC_NS (f->stat.st_atim); + when_timespec = get_stat_atime (&f->stat); break; } + when = when_timespec.tv_sec; + when_ns = when_timespec.tv_nsec; + p = buf; if (print_inode) @@ -3337,7 +3342,7 @@ print_long_format (const struct fileinfo *f) p[-1] = ' '; } - when_local = localtime (&when); + when_local = localtime (&when_timespec.tv_sec); s = 0; *p = '\1'; diff --git a/src/pr.c b/src/pr.c index 67ac6fe..4be18e3 100644 --- a/src/pr.c +++ b/src/pr.c @@ -319,6 +319,7 @@ #include "inttostr.h" #include "mbswidth.h" #include "quote.h" +#include "stat-time.h" #include "stdio--.h" #include "strftime.h" #include "xstrtol.h" @@ -1656,7 +1657,7 @@ init_header (char *filename, int desc) { char *buf = NULL; struct stat st; - time_t s; + struct timespec t; int ns; struct tm *tm; @@ -1664,25 +1665,22 @@ init_header (char *filename, int desc) if (STREQ (filename, "-")) desc = -1; if (0 <= desc && fstat (desc, &st) == 0) - { - s = st.st_mtime; - ns = TIMESPEC_NS (st.st_mtim); - } + t = get_stat_mtime (&st); else { static struct timespec timespec; if (! timespec.tv_sec) gettime (×pec); - s = timespec.tv_sec; - ns = timespec.tv_nsec; + t = timespec; } - tm = localtime (&s); + ns = t.tv_nsec; + tm = localtime (&t.tv_sec); if (tm == NULL) { buf = xmalloc (INT_BUFSIZE_BOUND (long int) + MAX (10, INT_BUFSIZE_BOUND (int))); - sprintf (buf, "%ld.%09d", (long int) s, ns); + sprintf (buf, "%ld.%09d", (long int) t.tv_sec, ns); } else { diff --git a/src/stat.c b/src/stat.c index 90f02bf..c9c71c5 100644 --- a/src/stat.c +++ b/src/stat.c @@ -52,6 +52,7 @@ #include "inttostr.h" #include "quote.h" #include "quotearg.h" +#include "stat-time.h" #include "strftime.h" #include "xreadlink.h" @@ -280,18 +281,18 @@ human_access (struct stat const *statbuf) } static char * -human_time (time_t t, int t_ns) +human_time (struct timespec t) { static char str[MAX (INT_BUFSIZE_BOUND (intmax_t), (INT_STRLEN_BOUND (int) /* YYYY */ + 1 /* because YYYY might equal INT_MAX + 1900 */ + sizeof "-MM-DD HH:MM:SS.NNNNNNNNN +ZZZZ"))]; - struct tm const *tm = localtime (&t); + struct tm const *tm = localtime (&t.tv_sec); if (tm == NULL) return (TYPE_SIGNED (time_t) - ? imaxtostr (t, str) - : umaxtostr (t, str)); - nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, 0, t_ns); + ? imaxtostr (t.tv_sec, str) + : umaxtostr (t.tv_sec, str)); + nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, 0, t.tv_nsec); return str; } @@ -506,8 +507,7 @@ print_stat (char *pformat, size_t buf_len, char m, break; case 'x': xstrcat (pformat, buf_len, "s"); - printf (pformat, human_time (statbuf->st_atime, - TIMESPEC_NS (statbuf->st_atim))); + printf (pformat, human_time (get_stat_atime (statbuf))); break; case 'X': xstrcat (pformat, buf_len, TYPE_SIGNED (time_t) ? "ld" : "lu"); @@ -515,8 +515,7 @@ print_stat (char *pformat, size_t buf_len, char m, break; case 'y': xstrcat (pformat, buf_len, "s"); - printf (pformat, human_time (statbuf->st_mtime, - TIMESPEC_NS (statbuf->st_mtim))); + printf (pformat, human_time (get_stat_mtime (statbuf))); break; case 'Y': xstrcat (pformat, buf_len, TYPE_SIGNED (time_t) ? "ld" : "lu"); @@ -524,8 +523,7 @@ print_stat (char *pformat, size_t buf_len, char m, break; case 'z': xstrcat (pformat, buf_len, "s"); - printf (pformat, human_time (statbuf->st_ctime, - TIMESPEC_NS (statbuf->st_ctim))); + printf (pformat, human_time (get_stat_ctime (statbuf))); break; case 'Z': xstrcat (pformat, buf_len, TYPE_SIGNED (time_t) ? "ld" : "lu"); diff --git a/src/tail.c b/src/tail.c index d6cdc86..19b4992 100644 --- a/src/tail.c +++ b/src/tail.c @@ -40,6 +40,7 @@ #include "posixver.h" #include "quote.h" #include "safe-read.h" +#include "stat-time.h" #include "xnanosleep.h" #include "xstrtol.h" #include "xstrtod.h" @@ -327,8 +328,7 @@ record_open_fd (struct File_spec *f, int fd, { f->fd = fd; f->size = size; - f->mtime.tv_sec = st->st_mtime; - f->mtime.tv_nsec = TIMESPEC_NS (st->st_mtim); + f->mtime = get_stat_mtime (st); f->dev = st->st_dev; f->ino = st->st_ino; f->mode = st->st_mode; @@ -1039,10 +1039,9 @@ tail_forever (struct File_spec *f, int nfiles, double sleep_interval) continue; } - if ((! S_ISREG (stats.st_mode) || f[i].size == stats.st_size) - && f[i].mtime.tv_sec == stats.st_mtime - && f[i].mtime.tv_nsec == TIMESPEC_NS (stats.st_mtim) - && f[i].mode == stats.st_mode) + if (f[i].mode == stats.st_mode + && (! S_ISREG (stats.st_mode) || f[i].size == stats.st_size) + && timespec_cmp (f[i].mtime, get_stat_mtime (&stats)) == 0) { if ((max_n_unchanged_stats_between_opens <= f[i].n_unchanged_stats++) @@ -1057,8 +1056,7 @@ tail_forever (struct File_spec *f, int nfiles, double sleep_interval) /* This file has changed. Print out what we can, and then keep looping. */ - f[i].mtime.tv_sec = stats.st_mtime; - f[i].mtime.tv_nsec = TIMESPEC_NS (stats.st_mtim); + f[i].mtime = get_stat_mtime (&stats); f[i].mode = stats.st_mode; /* reset counter */ diff --git a/src/touch.c b/src/touch.c index c7d5c00..1eaf8fa 100644 --- a/src/touch.c +++ b/src/touch.c @@ -32,6 +32,7 @@ #include "posixver.h" #include "quote.h" #include "safe-read.h" +#include "stat-time.h" #include "utimens.h" /* The official name of this program (e.g., no `g' prefix). */ @@ -170,22 +171,12 @@ touch (const char *file) } else { - if (change_times & CH_ATIME) - timespec[0] = newtime[0]; - else - { - timespec[0].tv_sec = sbuf.st_atime; - timespec[0].tv_nsec = TIMESPEC_NS (sbuf.st_atim); - } - - if (change_times & CH_MTIME) - timespec[1] = newtime[1]; - else - { - timespec[1].tv_sec = sbuf.st_mtime; - timespec[1].tv_nsec = TIMESPEC_NS (sbuf.st_mtim); - } - + timespec[0] = (change_times & CH_ATIME + ? newtime[0] + : get_stat_atime (&sbuf)); + timespec[1] = (change_times & CH_MTIME + ? newtime[1] + : get_stat_mtime (&sbuf)); t = timespec; } @@ -342,10 +333,8 @@ main (int argc, char **argv) if (stat (ref_file, &ref_stats)) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), quote (ref_file)); - newtime[0].tv_sec = ref_stats.st_atime; - newtime[0].tv_nsec = TIMESPEC_NS (ref_stats.st_atim); - newtime[1].tv_sec = ref_stats.st_mtime; - newtime[1].tv_nsec = TIMESPEC_NS (ref_stats.st_mtim); + newtime[0] = get_stat_atime (&ref_stats); + newtime[1] = get_stat_mtime (&ref_stats); date_set = true; if (flex_date) { -- 2.7.4