#include <stdio.h>
#include <stdlib.h>
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else /* not HAVE_STRING_H */
-# include <strings.h>
-#endif /* not HAVE_STRING_H */
-#include <sys/types.h>
+#include <string.h>
+#include <time.h>
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#endif
#include <fcntl.h>
#include <assert.h>
-#ifdef WGET_USE_STDARG
-# include <stdarg.h>
-#else
-# include <varargs.h>
-#endif
+#include <stdarg.h>
/* For TIOCGWINSZ and friends: */
#ifdef HAVE_SYS_IOCTL_H
#include "utils.h"
#include "hash.h"
-#ifndef errno
-extern int errno;
-#endif
-
/* Utility function: like xstrdup(), but also lowercases S. */
char *
return res;
}
\f
-#ifdef WGET_USE_STDARG
-# define VA_START(args, arg1) va_start (args, arg1)
-#else
-# define VA_START(args, ignored) va_start (args)
-#endif
-
/* Like sprintf, but allocates a string of sufficient size with malloc
and returns it. GNU libc has a similar function named asprintf,
which requires the pointer to the string to be passed. */
/* See log_vprintf_internal for explanation why it's OK to rely
on the return value of vsnprintf. */
- VA_START (args, fmt);
+ va_start (args, fmt);
n = vsnprintf (str, size, fmt, args);
va_end (args);
size <<= 1; /* twice the old size */
str = xrealloc (str, size);
}
- return NULL; /* unreached */
}
/* Concatenate the NULL-terminated list of string arguments into
/* Calculate the length of and allocate the resulting string. */
argcount = 0;
- VA_START (args, str0);
+ va_start (args, str0);
for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
{
int len = strlen (next_str);
/* Copy the strings into the allocated space. */
argcount = 0;
- VA_START (args, str0);
+ va_start (args, str0);
for (next_str = str0; next_str != NULL; next_str = va_arg (args, char *))
{
int len;
}
#endif /* not WINDOWS */
\f
-/* "Touch" FILE, i.e. make its atime and mtime equal to the time
- specified with TM. */
+/* "Touch" FILE, i.e. make its mtime ("modified time") equal the time
+ specified with TM. The atime ("access time") is set to the current
+ time. */
+
void
touch (const char *file, time_t tm)
{
#ifdef HAVE_STRUCT_UTIMBUF
struct utimbuf times;
- times.actime = times.modtime = tm;
#else
- time_t times[2];
- times[0] = times[1] = tm;
+ struct {
+ time_t actime;
+ time_t modtime;
+ } times;
#endif
-
+ times.modtime = tm;
+ times.actime = time (NULL);
if (utime (file, ×) == -1)
logprintf (LOG_NOTQUIET, "utime(%s): %s\n", file, strerror (errno));
}
return result;
}
\f
-static int in_acclist PARAMS ((const char *const *, const char *, int));
+static int in_acclist (const char *const *, const char *, int);
/* Determine whether a file is acceptable to be followed, according to
lists of patterns to accept/reject. */
proclist (char **strlist, const char *s, enum accd flags)
{
char **x;
-
for (x = strlist; *x; x++)
- if (has_wildcards_p (*x))
- {
- if (fnmatch (*x, s, FNM_PATHNAME) == 0)
- break;
- }
- else
- {
- char *p = *x + ((flags & ALLABS) && (**x == '/')); /* Remove '/' */
- if (frontcmp (p, s))
- break;
- }
+ {
+ /* Remove leading '/' if ALLABS */
+ char *p = *x + ((flags & ALLABS) && (**x == '/'));
+ if (has_wildcards_p (p))
+ {
+ if (fnmatch (p, s, FNM_PATHNAME) == 0)
+ break;
+ }
+ else
+ {
+ if (frontcmp (p, s))
+ break;
+ }
+ }
return *x;
}
xfree (v2);
return v1;
}
+
+/* Append a freshly allocated copy of STR to VEC. If VEC is NULL, it
+ is allocated as needed. Return the new value of the vector. */
+
+char **
+vec_append (char **vec, const char *str)
+{
+ int cnt; /* count of vector elements, including
+ the one we're about to append */
+ if (vec != NULL)
+ {
+ for (cnt = 0; vec[cnt]; cnt++)
+ ;
+ ++cnt;
+ }
+ else
+ cnt = 1;
+ /* Reallocate the array to fit the new element and the NULL. */
+ vec = xrealloc (vec, (cnt + 1) * sizeof (char *));
+ /* Append a copy of STR to the vector. */
+ vec[cnt - 1] = xstrdup (str);
+ vec[cnt] = NULL;
+ return vec;
+}
\f
/* Sometimes it's useful to create "sets" of strings, i.e. special
hash tables where you want to store strings as keys and merely
usually improves readability."
This intentionally uses kilobyte (KB), megabyte (MB), etc. in their
- original computer science meaning of "multiples of 1024".
- Multiples of 1000 would be useless since Wget already adds thousand
- separators for legibility. We don't use the "*bibyte" names
- invented in 1998, and seldom used in practice. Wikipedia's entry
- on kilobyte discusses this in some detail. */
+ original computer science meaning of "powers of 1024". Powers of
+ 1000 would be useless since Wget already displays sizes with
+ thousand separators. We don't use the "*bibyte" names invented in
+ 1998, and seldom used in practice. Wikipedia's entry on kilobyte
+ discusses this in some detail. */
char *
human_readable (wgint n)
/* wgint is 32 bits wide: no number has more than 10 digits. */
else DIGITS_10 (1000000000);
#else
- /* wgint is 64 bits wide: handle numbers with more than 9 decimal
- digits. Constants are constructed by compile-time multiplication
- to avoid dealing with different notations for 64-bit constants
- (nnnL, nnnLL, and nnnI64, depending on the compiler). */
+ /* wgint is 64 bits wide: handle numbers with 9-19 decimal digits.
+ Constants are constructed by compile-time multiplication to avoid
+ dealing with different notations for 64-bit constants
+ (nL/nLL/nI64, depending on the compiler and architecture). */
else if (n < 10*(W)1000000000) DIGITS_10 (1000000000);
else if (n < 100*(W)1000000000) DIGITS_11 (10*(W)1000000000);
else if (n < 1000*(W)1000000000) DIGITS_12 (100*(W)1000000000);
#endif /* not WINDOWS */
-/* Encode the string S of length LENGTH to base64 format and place it
+/* Encode the string STR of length LENGTH to base64 format and place it
to B64STORE. The output will be \0-terminated, and must point to a
writable buffer of at least 1+BASE64_LENGTH(length) bytes. It
returns the length of the resulting base64 data, not counting the
base64 data. */
int
-base64_encode (const char *s, int length, char *b64store)
+base64_encode (const char *str, int length, char *b64store)
{
/* Conversion table. */
static char tbl[64] = {
'4','5','6','7','8','9','+','/'
};
int i;
- unsigned char *p = (unsigned char *) b64store;
+ const unsigned char *s = (const unsigned char *) str;
+ char *p = b64store;
/* Transform the 3x8 bits to 4x6 bits, as required by base64. */
for (i = 0; i < length; i += 3)
/* ...and zero-terminate it. */
*p = '\0';
- return p - (unsigned char *) b64store;
+ return p - b64store;
}
#define IS_ASCII(c) (((c) & 0x80) == 0)
#undef IS_ASCII
#undef IS_BASE64
#undef NEXT_BASE64_CHAR
+\f
+/* Simple merge sort for use by stable_sort. Implementation courtesy
+ Zeljko Vrba with additional debugging by Nenad Barbutov. */
+
+static void
+mergesort_internal (void *base, void *temp, size_t size, size_t from, size_t to,
+ int (*cmpfun) (const void *, const void *))
+{
+#define ELT(array, pos) ((char *)(array) + (pos) * size)
+ if (from < to)
+ {
+ size_t i, j, k;
+ size_t mid = (to + from) / 2;
+ mergesort_internal (base, temp, size, from, mid, cmpfun);
+ mergesort_internal (base, temp, size, mid + 1, to, cmpfun);
+ i = from;
+ j = mid + 1;
+ for (k = from; (i <= mid) && (j <= to); k++)
+ if (cmpfun (ELT (base, i), ELT (base, j)) <= 0)
+ memcpy (ELT (temp, k), ELT (base, i++), size);
+ else
+ memcpy (ELT (temp, k), ELT (base, j++), size);
+ while (i <= mid)
+ memcpy (ELT (temp, k++), ELT (base, i++), size);
+ while (j <= to)
+ memcpy (ELT (temp, k++), ELT (base, j++), size);
+ for (k = from; k <= to; k++)
+ memcpy (ELT (base, k), ELT (temp, k), size);
+ }
+#undef ELT
+}
+
+/* Stable sort with interface exactly like standard library's qsort.
+ Uses mergesort internally, allocating temporary storage with
+ alloca. */
+
+void
+stable_sort (void *base, size_t nmemb, size_t size,
+ int (*cmpfun) (const void *, const void *))
+{
+ if (size > 1)
+ {
+ void *temp = alloca (nmemb * size * sizeof (void *));
+ mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun);
+ }
+}