[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Use the gettime module in ls.c.
From: |
James Youngman |
Subject: |
[PATCH] Use the gettime module in ls.c. |
Date: |
Sat, 16 Feb 2008 17:11:17 +0000 |
2008-02-16 James Youngman <address@hidden>
Implement TODO list item to make ls.c use the gettime module.
* TODO (ls): Now that we use gettime, remove the TODO entry.
* src/ls.c: Use timespec.h and the gettime module.
(current_time): Change type from time_t to struct timespec.
(current_time_ns): Removed.
(get_current_time): Removed.
(print_long_format): Remove when and when_ns, since we have
when_timespec anyway. Change type of variable six_months_ago from
time_t to struct timespec.
---
TODO | 4 +--
src/ls.c | 76 +++++++++++++++++--------------------------------------------
2 files changed, 22 insertions(+), 58 deletions(-)
diff --git a/TODO b/TODO
index 3cec054..24369c6 100644
--- a/TODO
+++ b/TODO
@@ -177,11 +177,9 @@ Adapt tools like wc, tr, fmt, etc. (most of the textutils)
to be
pr's use of nstrftime can make it malloc a very large (up to SIZE_MAX) buffer
-ls.c: use gettime rather than clock_gettime, gettimeofday, time
-
-----
-Copyright (C) 2002-2007 Free Software Foundation, Inc.
+Copyright (C) 2002-2008 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
diff --git a/src/ls.c b/src/ls.c
index 46713f2..5f466ca 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -101,6 +101,7 @@
#include "quotearg.h"
#include "same.h"
#include "stat-time.h"
+#include "timespec.h"
#include "strftime.h"
#include "strverscmp.h"
#include "xstrtol.h"
@@ -311,8 +312,7 @@ static struct pending *pending_dirs;
/* Current time in seconds and nanoseconds since 1970, updated as
needed when deciding whether a file is recent. */
-static time_t current_time = TYPE_MINIMUM (time_t);
-static int current_time_ns = -1;
+static struct timespec current_time;
static bool print_scontext;
static char UNKNOWN_SECURITY_CONTEXT[] = "?";
@@ -1155,6 +1155,9 @@ main (int argc, char **argv)
print_dir_name = true;
pending_dirs = NULL;
+ current_time.tv_sec = TYPE_MINIMUM (time_t);
+ current_time.tv_nsec = -1;
+
i = decode_switches (argc, argv);
if (print_with_color)
@@ -3306,42 +3309,6 @@ long_time_expected_width (void)
return width;
}
-/* Get the current time. */
-
-static void
-get_current_time (void)
-{
-#if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
- {
- struct timespec timespec;
- if (clock_gettime (CLOCK_REALTIME, ×pec) == 0)
- {
- current_time = timespec.tv_sec;
- current_time_ns = timespec.tv_nsec;
- return;
- }
- }
-#endif
-
- /* The clock does not have nanosecond resolution, so get the maximum
- possible value for the current time that is consistent with the
- reported clock. That way, files are not considered to be in the
- future merely because their time stamps have higher resolution
- than the clock resolution. */
-
-#if HAVE_GETTIMEOFDAY
- {
- struct timeval timeval;
- gettimeofday (&timeval, NULL);
- current_time = timeval.tv_sec;
- current_time_ns = timeval.tv_usec * 1000 + 999;
- }
-#else
- current_time = time (NULL);
- current_time_ns = 999999999;
-#endif
-}
-
/* Print the user or group name NAME, with numeric id ID, using a
print width of WIDTH columns. */
@@ -3441,8 +3408,6 @@ print_long_format (const struct fileinfo *f)
];
size_t s;
char *p;
- time_t when;
- int when_ns;
struct timespec when_timespec;
struct tm *when_local;
@@ -3476,9 +3441,6 @@ print_long_format (const struct fileinfo *f)
abort ();
}
- when = when_timespec.tv_sec;
- when_ns = when_timespec.tv_nsec;
-
p = buf;
if (print_inode)
@@ -3579,35 +3541,38 @@ print_long_format (const struct fileinfo *f)
if (f->stat_ok && when_local)
{
- time_t six_months_ago;
+ struct timespec six_months_ago;
bool recent;
char const *fmt;
/* If the file appears to be in the future, update the current
time, in case the file happens to have been modified since
the last time we checked the clock. */
- if (current_time < when
- || (current_time == when && current_time_ns < when_ns))
+ if (timespec_cmp (current_time, when_timespec) < 0)
{
- /* Note that get_current_time calls gettimeofday which, on some non-
+ /* Note that gettime calls gettimeofday which, on some non-
compliant systems, clobbers the buffer used for localtime's result.
But it's ok here, because we use a gettimeofday wrapper that
saves and restores the buffer around the gettimeofday call. */
- get_current_time ();
+ gettime (¤t_time);
}
/* Consider a time to be recent if it is within the past six
months. A Gregorian year has 365.2425 * 24 * 60 * 60 ==
31556952 seconds on the average. Write this value as an
integer constant to avoid floating point hassles. */
- six_months_ago = current_time - 31556952 / 2;
- recent = (six_months_ago <= when
- && (when < current_time
- || (when == current_time && when_ns <= current_time_ns)));
+ six_months_ago.tv_sec = current_time.tv_sec - 31556952 / 2;
+ six_months_ago.tv_nsec = current_time.tv_nsec;
+
+ recent = (timespec_cmp (six_months_ago, when_timespec) < 0
+ && (timespec_cmp (when_timespec, current_time) < 0));
fmt = long_time_format[recent];
+ /* We assume here that all time zones are offset from UTC by a
+ * whole number of seconds.
+ */
s = nstrftime (p, TIME_STAMP_LEN_MAXIMUM + 1, fmt,
- when_local, 0, when_ns);
+ when_local, 0, when_timespec.tv_nsec);
}
if (s || !*p)
@@ -3627,8 +3592,9 @@ print_long_format (const struct fileinfo *f)
(! f->stat_ok
? "?"
: (TYPE_SIGNED (time_t)
- ? imaxtostr (when, hbuf)
- : umaxtostr (when, hbuf))));
+ ? imaxtostr (when_timespec.tv_sec, hbuf)
+ : umaxtostr (when_timespec.tv_sec, hbuf))));
+ /* FIXME: (maybe) We discarded when_timespec.tv_nsec. */
p += strlen (p);
}
--
1.5.3.8
- [PATCH] Use the gettime module in ls.c.,
James Youngman <=