#include "wget.h"
#include "utils.h"
-#include "fnmatch.h"
#include "hash.h"
#ifndef errno
that way we can also verify whether the file is readable.
Inspired by the POST patch by Arnaud Wylie. */
FILE *fp = fopen (filename, "rb");
+ if (!fp)
+ return -1;
fseek (fp, 0, SEEK_END);
size = ftell (fp);
fclose (fp);
return NULL;
}
+/* Return non-zero if S contains globbing wildcards (`*', `?', `[' or
+ `]'). */
+
+int
+has_wildcards_p (const char *s)
+{
+ for (; *s; s++)
+ if (*s == '*' || *s == '?' || *s == '[' || *s == ']')
+ return 1;
+ return 0;
+}
+
/* Return non-zero if FNAME ends with a typical HTML suffix. The
following (case-insensitive) suffixes are presumed to be HTML files:
}
/* Read a line from FP and return the pointer to freshly allocated
- storage. The stoarage space is obtained through malloc() and
- should be freed with free() when it is no longer needed.
+ storage. The storage space is obtained through malloc() and should
+ be freed with free() when it is no longer needed.
The length of the line is not limited, except by available memory.
The newline character at the end of line is retained. The line is
/* Normally, we grow SIZE exponentially to make the number
of calls to read() and realloc() logarithmic in relation
to file size. However, read() can read an amount of data
- smaller than requested, and it would be unreasonably to
+ smaller than requested, and it would be unreasonable to
double SIZE every time *something* was read. Therefore,
we double SIZE only when the length exceeds half of the
entire allocated size. */
}
\f
-/* Engine for legible and legible_very_long; this function works on
- strings. */
+/* Engine for legible and legible_large_int; add thousand separators
+ to numbers printed in strings. */
static char *
legible_1 (const char *repr)
{
- static char outbuf[128];
+ static char outbuf[48];
int i, i1, mod;
char *outptr;
const char *inptr;
/* Reset the pointers. */
outptr = outbuf;
inptr = repr;
- /* If the number is negative, shift the pointers. */
+
+ /* Ignore the sign for the purpose of adding thousand
+ separators. */
if (*inptr == '-')
{
*outptr++ = '-';
}
/* Legible -- return a static pointer to the legibly printed long. */
+
char *
legible (long l)
{
return legible_1 (inbuf);
}
-/* Write a string representation of NUMBER into the provided buffer.
- We cannot use sprintf() because we cannot be sure whether the
- platform supports printing of what we chose for VERY_LONG_TYPE.
-
- Example: Gcc supports `long long' under many platforms, but on many
- of those the native libc knows nothing of it and therefore cannot
- print it.
-
- How long BUFFER needs to be depends on the platform and the content
- of NUMBER. For 64-bit VERY_LONG_TYPE (the most common case), 24
- bytes are sufficient. Using more might be a good idea.
+/* Write a string representation of LARGE_INT NUMBER into the provided
+ buffer. The buffer should be able to accept 24 characters,
+ including the terminating zero.
- This function does not go through the hoops that long_to_string
- goes to because it doesn't aspire to be fast. (It's called perhaps
- once in a Wget run.) */
+ It would be dangerous to use sprintf, because the code wouldn't
+ work on a machine with gcc-provided long long support, but without
+ libc support for "%lld". However, such platforms will typically
+ not have snprintf and will use our version, which does support
+ "%lld" where long longs are available. */
static void
-very_long_to_string (char *buffer, VERY_LONG_TYPE number)
+large_int_to_string (char *buffer, LARGE_INT number)
{
- int i = 0;
- int j;
-
- /* Print the number backwards... */
- do
- {
- buffer[i++] = '0' + number % 10;
- number /= 10;
- }
- while (number);
-
- /* ...and reverse the order of the digits. */
- for (j = 0; j < i / 2; j++)
- {
- char c = buffer[j];
- buffer[j] = buffer[i - 1 - j];
- buffer[i - 1 - j] = c;
- }
- buffer[i] = '\0';
+ snprintf (buffer, 24, LARGE_INT_FMT, number);
}
-/* The same as legible(), but works on VERY_LONG_TYPE. See sysdep.h. */
+/* The same as legible(), but works on LARGE_INT. */
+
char *
-legible_very_long (VERY_LONG_TYPE l)
+legible_large_int (LARGE_INT l)
{
- char inbuf[128];
- /* Print the number into the buffer. */
- very_long_to_string (inbuf, l);
+ char inbuf[48];
+ large_int_to_string (inbuf, l);
return legible_1 (inbuf);
}
#endif
#ifdef TIMER_TIME
- /* This is clear. */
return 1000;
#endif
This uses rand() for portability. It has been suggested that
random() offers better randomness, but this is not required for
Wget, so I chose to go for simplicity and use rand
- unconditionally. */
+ unconditionally.
+
+ DO NOT use this for cryptographic purposes. It is only meant to be
+ used in situations where quality of the random numbers returned
+ doesn't really matter. */
int
random_number (int max)
}
#endif
\f
-/* Implementation of run_with_timeout, a generic timeout handler for
- systems with Unix-like signal handling. */
+/* Implementation of run_with_timeout, a generic timeout-forcing
+ routine for systems with Unix-like signal handling. */
+
#ifdef USE_SIGNAL_TIMEOUT
# ifdef HAVE_SIGSETJMP
# define SETJMP(env) sigsetjmp (env, 1)
if we longjumped out of the handler at this point, SIGALRM would
remain blocked. We must unblock it manually. */
int mask = siggetmask ();
- mask &= ~sigmask(SIGALRM);
+ mask &= ~sigmask (SIGALRM);
sigsetmask (mask);
/* Now it's safe to longjump. */
#endif /* not ITIMER_REAL */
}
-#endif /* USE_SIGNAL_TIMEOUT */
+/* Call FUN(ARG), but don't allow it to run for more than TIMEOUT
+ seconds. Returns non-zero if the function was interrupted with a
+ timeout, zero otherwise.
+
+ This works by setting up SIGALRM to be delivered in TIMEOUT seconds
+ using setitimer() or alarm(). The timeout is enforced by
+ longjumping out of the SIGALRM handler. This has several
+ advantages compared to the traditional approach of relying on
+ signals causing system calls to exit with EINTR:
+
+ * The callback function is *forcibly* interrupted after the
+ timeout expires, (almost) regardless of what it was doing and
+ whether it was in a syscall. For example, a calculation that
+ takes a long time is interrupted as reliably as an IO
+ operation.
+
+ * It works with both SYSV and BSD signals because it doesn't
+ depend on the default setting of SA_RESTART.
+
+ * It doesn't special handler setup beyond a simple call to
+ signal(). (It does use sigsetjmp/siglongjmp, but they're
+ optional.)
+
+ The only downside is that, if FUN allocates internal resources that
+ are normally freed prior to exit from the functions, they will be
+ lost in case of timeout. */
int
run_with_timeout (double timeout, void (*fun) (void *), void *arg)
{
-#ifndef USE_SIGNAL_TIMEOUT
- fun (arg);
- return 0;
-#else
int saved_errno;
if (timeout == 0)
errno = saved_errno;
return 0;
-#endif
}
+
+#else /* not USE_SIGNAL_TIMEOUT */
+
+#ifndef WINDOWS
+/* A stub version of run_with_timeout that just calls FUN(ARG). Don't
+ define it under Windows, because Windows has its own version of
+ run_with_timeout that uses threads. */
+
+int
+run_with_timeout (double timeout, void (*fun) (void *), void *arg)
+{
+ fun (arg);
+ return 0;
+}
+#endif /* not WINDOWS */
+#endif /* not USE_SIGNAL_TIMEOUT */