summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2004-07-12 17:55:06 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2004-07-12 17:55:06 +0000
commit6301ff874e3dba7c321129cc61e9fa99a09948eb (patch)
tree2c0aed79d8a7a48fc6bd246c9cb47581aab81bc8 /src
parent576958b1fd3ce98dae938d76cbb8fb4fa0e3ec9f (diff)
downloadcoreutils-6301ff874e3dba7c321129cc61e9fa99a09948eb.tar.xz
Include c-strtod.h.
(print_uptime): Use c_strtod instead of setlocale and sscanf. Use long int rather than int to count days (for 64-bit hosts), and check for arithmetic overflow when converting double to time_t.
Diffstat (limited to 'src')
-rw-r--r--src/uptime.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/uptime.c b/src/uptime.c
index 72621bfa6..f70d9f125 100644
--- a/src/uptime.c
+++ b/src/uptime.c
@@ -28,6 +28,7 @@
# include <sys/sysctl.h>
#endif
+#include "c-strtod.h"
#include "error.h"
#include "long-options.h"
#include "quote.h"
@@ -55,7 +56,7 @@ print_uptime (int n, const STRUCT_UTMP *this)
time_t boot_time = 0;
time_t time_now;
time_t uptime = 0;
- int updays;
+ long int updays;
int uphours;
int upmins;
struct tm *tmn;
@@ -73,12 +74,11 @@ print_uptime (int n, const STRUCT_UTMP *this)
char *b = fgets (buf, BUFSIZ, fp);
if (b == buf)
{
- /* The following sscanf must use the C locale. */
- setlocale (LC_NUMERIC, "C");
- res = sscanf (buf, "%lf", &upsecs);
- setlocale (LC_NUMERIC, "");
- if (res == 1)
- uptime = (time_t) upsecs;
+ char *end_ptr;
+ upsecs = c_strtod (buf, &end_ptr);
+ if (buf != end_ptr)
+ uptime = (0 <= upsecs && upsecs < TYPE_MAXIMUM (time_t)
+ ? upsecs : -1);
}
fclose (fp);
@@ -141,9 +141,14 @@ print_uptime (int n, const STRUCT_UTMP *this)
tmn->tm_min, (tmn->tm_hour < 12 ? _("am") : _("pm")));
else
printf (_(" ??:???? up "));
- if (updays > 0)
- printf (ngettext("%d day", "%d days", updays), updays);
- printf (" %2d:%02d, ", uphours, upmins);
+ if (uptime == (time_t) -1)
+ printf (_("???? days ??:??, "));
+ else
+ {
+ if (0 < updays)
+ printf (ngettext ("%ld day", "%ld days", updays), updays);
+ printf (" %2d:%02d, ", uphours, upmins);
+ }
printf (ngettext ("%d user", "%d users", entries), entries);
#if defined (HAVE_GETLOADAVG) || defined (C_GETLOADAVG)