]> sjero.net Git - wget/blobdiff - src/utils.c
[svn] Better INT_MAX and UCHAR_MAX checks.
[wget] / src / utils.c
index 1559b2796ebc996d4ff73e4f4bb212ad81b6ea12..36542c9e3e6a3e201f2b0825f2d34373629f3dc2 100644 (file)
@@ -46,7 +46,9 @@ so, delete this exception statement from your version.  */
 #ifdef HAVE_PWD_H
 # include <pwd.h>
 #endif
-#include <limits.h>
+#ifdef HAVE_LIMITS_H
+# include <limits.h>
+#endif
 #ifdef HAVE_UTIME_H
 # include <utime.h>
 #endif
@@ -1147,10 +1149,13 @@ numdigit (long number)
   return cnt;
 }
 
-/* A half-assed implementation of INT_MAX on machines that don't
-   bother to define one. */
+/* Attempt to calculate INT_MAX on machines that don't bother to
+   define it. */
 #ifndef INT_MAX
-# define INT_MAX ((int) ~((unsigned)1 << 8 * sizeof (int) - 1))
+# ifndef CHAR_BIT
+#  define CHAR_BIT 8
+# endif
+# define INT_MAX ((int) ~((unsigned)1 << CHAR_BIT * sizeof (int) - 1))
 #endif
 
 #define ONE_DIGIT(figure) *p++ = n / (figure) + '0'
@@ -1318,6 +1323,9 @@ typedef ULARGE_INTEGER wget_sys_time;
 #endif
 
 struct wget_timer {
+  /* Whether the start time has been initialized. */
+  int initialized;
+
   /* The starting point in time which, subtracted from the current
      time, yields elapsed time. */
   wget_sys_time start;
@@ -1331,13 +1339,15 @@ struct wget_timer {
   double elapsed_pre_start;
 };
 
-/* Allocate a timer.  It is not legal to do anything with a freshly
-   allocated timer, except call wtimer_reset() or wtimer_delete().  */
+/* Allocate a timer.  Calling wtimer_read on the timer will return
+   zero.  It is not legal to call wtimer_update with a freshly
+   allocated timer -- use wtimer_reset first.  */
 
 struct wget_timer *
 wtimer_allocate (void)
 {
   struct wget_timer *wt = xnew (struct wget_timer);
+  xzero (*wt);
   return wt;
 }
 
@@ -1408,6 +1418,7 @@ wtimer_reset (struct wget_timer *wt)
   wtimer_sys_set (&wt->start);
   wt->elapsed_last = 0;
   wt->elapsed_pre_start = 0;
+  wt->initialized = 1;
 }
 
 static double
@@ -1444,6 +1455,8 @@ wtimer_update (struct wget_timer *wt)
   wget_sys_time now;
   double elapsed;
 
+  assert (wt->initialized != 0);
+
   wtimer_sys_set (&now);
   elapsed = wt->elapsed_pre_start + wtimer_sys_diff (&now, &wt->start);