]> sjero.net Git - wget/blobdiff - src/retr.c
[svn] New option --random-wait.
[wget] / src / retr.c
index dcd812268df40116fc69038b1936da6cae5cf51e..b8fb6c84b3a05e348a2784705294890438f7a670 100644 (file)
@@ -147,16 +147,31 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
 }
 \f
 /* Return a printed representation of the download rate, as
-   appropriate for the speed.  Appropriate means that if rate is
-   greater than 1K/s, kilobytes are used, and if rate is greater than
-   1MB/s, megabytes are used.
-
-   If PAD is non-zero, strings will be padded to the width of 7
-   characters (xxxx.xx).  */
+   appropriate for the speed.  If PAD is non-zero, strings will be
+   padded to the width of 7 characters (xxxx.xx).  */
 char *
-rate (long bytes, long msecs, int pad)
+retr_rate (long bytes, long msecs, int pad)
+{
+  static char res[20];
+  static char *rate_names[] = {"B/s", "KB/s", "MB/s", "GB/s" };
+  int units = 0;
+
+  double dlrate = calc_rate (bytes, msecs, &units);
+  sprintf (res, pad ? "%7.2f %s" : "%.2f %s", dlrate, rate_names[units]);
+
+  return res;
+}
+
+/* Calculate the download rate and trim it as appropriate for the
+   speed.  Appropriate means that if rate is greater than 1K/s,
+   kilobytes are used, and if rate is greater than 1MB/s, megabytes
+   are used.
+
+   UNITS is zero for B/s, one for KB/s, two for MB/s, and three for
+   GB/s.  */
+double
+calc_rate (long bytes, long msecs, int *units)
 {
-  static char res[15];
   double dlrate;
 
   assert (msecs >= 0);
@@ -170,18 +185,17 @@ rate (long bytes, long msecs, int pad)
 
   dlrate = (double)1000 * bytes / msecs;
   if (dlrate < 1024.0)
-    sprintf (res, pad ? "%7.2f B/s" : "%.2f B/s", dlrate);
+    *units = 0;
   else if (dlrate < 1024.0 * 1024.0)
-    sprintf (res, pad ? "%7.2f K/s" : "%.2f K/s", dlrate / 1024.0);
+    *units = 1, dlrate /= 1024.0;
   else if (dlrate < 1024.0 * 1024.0 * 1024.0)
-    sprintf (res, pad ? "%7.2f M/s" : "%.2f M/s", dlrate / (1024.0 * 1024.0));
+    *units = 2, dlrate /= (1024.0 * 1024.0);
   else
     /* Maybe someone will need this one day.  More realistically, it
        will get tickled by buggy timers. */
-    sprintf (res, pad ? "%7.2f GB/s" : "%.2f GB/s",
-            dlrate / (1024.0 * 1024.0 * 1024.0));
+    *units = 3, dlrate /= (1024.0 * 1024.0 * 1024.0);
 
-  return res;
+  return dlrate;
 }
 \f
 static int
@@ -322,7 +336,6 @@ retrieve_url (const char *origurl, char **file, char **newloc,
          char *suf = suffix (u->local);
          if (suf && (!strcasecmp (suf, "html") || !strcasecmp (suf, "htm")))
            *dt |= TEXTHTML;
-         FREE_MAYBE (suf);
        }
 #endif
     }
@@ -448,6 +461,9 @@ retrieve_from_file (const char *file, int html, int *count)
       char *filename = NULL, *new_file;
       int dt;
 
+      if (cur_url->ignore_when_downloading)
+       continue;
+
       if (downloaded_exceeds_quota ())
        {
          status = QUOTEXC;
@@ -544,8 +560,18 @@ sleep_between_retrievals (int count)
            sleep (opt.waitretry);
        }
       else if (opt.wait)
-       /* Otherwise, check if opt.wait is specified.  If so, sleep.  */
-       sleep (opt.wait);
+       {
+         /* Otherwise, check if opt.wait is specified.  If so, sleep.  */
+         if (count > 1 || !opt.random_wait)
+           sleep (opt.wait);
+         else
+           {
+             int waitsecs = random() % (opt.wait * 2 + 1);
+             DEBUGP(("sleep_between_retrievals: norm=%ld,random=%ld,sleep=%d\n",
+                     opt.wait, waitsecs - opt.wait, waitsecs));
+             sleep(waitsecs);
+           }
+       }
     }
   if (first_retrieval)
     first_retrieval = 0;