]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Include c-strtod.h.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 12 Jul 2004 17:55:06 +0000 (17:55 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 12 Jul 2004 17:55:06 +0000 (17:55 +0000)
(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.

src/uptime.c

index 72621bfa66f1e7a67b7b57d2f50f0b6c8885ecd8..f70d9f125f05620b3dd2d8b19c29e833215651d4 100644 (file)
@@ -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)