+
+/* If opt.wait or opt.waitretry are specified, and if certain
+ conditions are met, sleep the appropriate number of seconds. See
+ the documentation of --wait and --waitretry for more information.
+
+ COUNT is the count of current retrieval, beginning with 1. */
+
+void
+sleep_between_retrievals (int count)
+{
+ static bool first_retrieval = true;
+
+ if (first_retrieval)
+ {
+ /* Don't sleep before the very first retrieval. */
+ first_retrieval = false;
+ return;
+ }
+
+ if (opt.waitretry && count > 1)
+ {
+ /* If opt.waitretry is specified and this is a retry, wait for
+ COUNT-1 number of seconds, or for opt.waitretry seconds. */
+ if (count <= opt.waitretry)
+ xsleep (count - 1);
+ else
+ xsleep (opt.waitretry);
+ }
+ else if (opt.wait)
+ {
+ if (!opt.random_wait || count > 1)
+ /* If random-wait is not specified, or if we are sleeping
+ between retries of the same download, sleep the fixed
+ interval. */
+ xsleep (opt.wait);
+ else
+ {
+ /* Sleep a random amount of time averaging in opt.wait
+ seconds. The sleeping amount ranges from 0.5*opt.wait to
+ 1.5*opt.wait. */
+ double waitsecs = (0.5 + random_float ()) * opt.wait;
+ DEBUGP (("sleep_between_retrievals: avg=%f,sleep=%f\n",
+ opt.wait, waitsecs));
+ xsleep (waitsecs);
+ }
+ }
+}
+
+/* Free the linked list of urlpos. */
+void
+free_urlpos (struct urlpos *l)
+{
+ while (l)
+ {
+ struct urlpos *next = l->next;
+ if (l->url)
+ url_free (l->url);
+ xfree_null (l->local_name);
+ xfree (l);
+ l = next;
+ }
+}
+
+/* Rotate FNAME opt.backups times */
+void
+rotate_backups(const char *fname)
+{
+ int maxlen = strlen (fname) + 1 + numdigit (opt.backups) + 1;
+ char *from = (char *)alloca (maxlen);
+ char *to = (char *)alloca (maxlen);
+ struct_stat sb;
+ int i;
+
+ if (stat (fname, &sb) == 0)
+ if (S_ISREG (sb.st_mode) == 0)
+ return;
+
+ for (i = opt.backups; i > 1; i--)
+ {
+ sprintf (from, "%s.%d", fname, i - 1);
+ sprintf (to, "%s.%d", fname, i);
+ rename (from, to);
+ }
+
+ sprintf (to, "%s.%d", fname, 1);
+ rename(fname, to);
+}
+
+static bool no_proxy_match (const char *, const char **);
+
+/* Return the URL of the proxy appropriate for url U. */
+
+static char *
+getproxy (struct url *u)
+{
+ char *proxy = NULL;
+ char *rewritten_url;
+ static char rewritten_storage[1024];
+
+ if (!opt.use_proxy)
+ return NULL;
+ if (no_proxy_match (u->host, (const char **)opt.no_proxy))
+ return NULL;
+
+ switch (u->scheme)
+ {
+ case SCHEME_HTTP:
+ proxy = opt.http_proxy ? opt.http_proxy : getenv ("http_proxy");
+ break;
+#ifdef HAVE_SSL
+ case SCHEME_HTTPS:
+ proxy = opt.https_proxy ? opt.https_proxy : getenv ("https_proxy");
+ break;
+#endif
+ case SCHEME_FTP:
+ proxy = opt.ftp_proxy ? opt.ftp_proxy : getenv ("ftp_proxy");
+ break;
+ case SCHEME_INVALID:
+ break;
+ }
+ if (!proxy || !*proxy)
+ return NULL;
+
+ /* Handle shorthands. `rewritten_storage' is a kludge to allow
+ getproxy() to return static storage. */
+ rewritten_url = rewrite_shorthand_url (proxy);
+ if (rewritten_url)
+ {
+ strncpy (rewritten_storage, rewritten_url, sizeof (rewritten_storage));
+ rewritten_storage[sizeof (rewritten_storage) - 1] = '\0';
+ proxy = rewritten_storage;
+ }
+
+ return proxy;
+}
+
+/* Returns true if URL would be downloaded through a proxy. */
+
+bool
+url_uses_proxy (struct url * u)
+{
+ bool ret;
+ if (!u)
+ return false;
+ ret = getproxy (u) != NULL;
+ return ret;
+}
+
+/* Should a host be accessed through proxy, concerning no_proxy? */
+static bool
+no_proxy_match (const char *host, const char **no_proxy)
+{
+ if (!no_proxy)
+ return false;
+ else
+ return sufmatch (no_proxy, host);
+}
+
+/* Set the file parameter to point to the local file string. */
+void
+set_local_file (const char **file, const char *default_file)
+{
+ if (opt.output_document)
+ {
+ if (output_stream_regular)
+ *file = opt.output_document;
+ }
+ else
+ *file = default_file;
+}