]> sjero.net Git - wget/commitdiff
[svn] Use vasprintf where available.
authorhniksic <devnull@localhost>
Fri, 8 Jul 2005 07:33:05 +0000 (00:33 -0700)
committerhniksic <devnull@localhost>
Fri, 8 Jul 2005 07:33:05 +0000 (00:33 -0700)
ChangeLog
configure.in
src/ChangeLog
src/utils.c

index 214a60f10d79016c053099a752d15ae0bcd3d526..4a70701390bb436b2c93a45548b2d472b4a079cb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2005-07-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * configure.in: Don't check for symlink, which is expected to
+       exist.  Check for asprintf.
+
 2005-07-07  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * configure.bat: Copy the common config.h and config-compiler.h.
index c3487b91c770a16cff66563dd34827d61622e94f..ec943cdeb204a0a55503639c2e3b32e794284b60 100644 (file)
@@ -204,8 +204,8 @@ dnl
 AC_FUNC_ALLOCA
 AC_FUNC_MMAP
 AC_FUNC_FSEEKO
-AC_CHECK_FUNCS(strptime timegm snprintf vsnprintf drand48)
-AC_CHECK_FUNCS(usleep ftello sigblock sigsetjmp symlink)
+AC_CHECK_FUNCS(strptime timegm snprintf vsnprintf vasprintf drand48)
+AC_CHECK_FUNCS(usleep ftello sigblock sigsetjmp)
 
 dnl We expect to have these functions on Unix-like systems configure
 dnl runs on.  The defines are provided to get them in config.h.in so
index dbc9832244689f40cf89b274f6da9e8656309caa..185da492e0768a0b01bac12875eb3e2348b5cc4b 100644 (file)
@@ -1,3 +1,7 @@
+2005-07-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * utils.c (aprintf): Use vasprintf where available.
+
 2005-07-08  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * url.c (rewrite_shorthand_url): Simplify code using aprintf and
index 29262c12e0a97ad91bd1f713e24c1e94ccb296b2..b79c8dcdd3d1556c0b0f3f48f43bcfa3e4f9dcd2 100644 (file)
@@ -42,9 +42,6 @@ so, delete this exception statement from your version.  */
 #ifdef HAVE_MMAP
 # include <sys/mman.h>
 #endif
-#ifdef HAVE_PWD_H
-# include <pwd.h>
-#endif
 #ifdef HAVE_UTIME_H
 # include <utime.h>
 #endif
@@ -52,9 +49,6 @@ so, delete this exception statement from your version.  */
 # include <sys/utime.h>
 #endif
 #include <errno.h>
-#ifdef NeXT
-# include <libc.h>             /* for access() */
-#endif
 #include <fcntl.h>
 #include <assert.h>
 #include <stdarg.h>
@@ -79,8 +73,7 @@ so, delete this exception statement from your version.  */
 # endif
 #endif
 
-#undef USE_SIGNAL_TIMEOUT
-#if defined(HAVE_SIGSETJMP) || defined(HAVE_SIGBLOCK)
+#if defined HAVE_SIGSETJMP || defined HAVE_SIGBLOCK
 # define USE_SIGNAL_TIMEOUT
 #endif
 
@@ -148,17 +141,37 @@ sepstring (const char *s)
   return res;
 }
 \f
-/* 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.  */
+/* Like sprintf, but prints into a string of sufficient size freshly
+   allocated with malloc, which is returned.  If unable to print due
+   to invalid format, returns NULL.  Inability to allocate needed
+   memory results in abort, as with xmalloc.  This is in spirit
+   similar to the GNU/BSD extension asprintf, but somewhat easier to
+   use.
+
+   Internally the function either calls vasprintf or loops around
+   vsnprintf until the correct size is found.  Since Wget also ships a
+   fallback implementation of vsnprintf, this should be portable.  */
 
 char *
 aprintf (const char *fmt, ...)
 {
-  /* This function is implemented using vsnprintf, which we provide
-     for the systems that don't have it.  Therefore, it should be 100%
-     portable.  */
+#ifdef HAVE_VASPRINTF
+  /* Use vasprintf. */
+  int ret;
+  va_list args;
+  char *str;
+  va_start (args, fmt);
+  ret = vasprintf (&str, fmt, args);
+  va_end (args);
+  if (ret < 0 && errno == ENOMEM)
+    abort ();                  /* for consistency with xmalloc/xrealloc */
+  else if (ret < 0)
+    return NULL;
+  return str;
+#else  /* not HAVE_VASPRINTF */
 
+  /* vasprintf is unavailable.  snprintf into a small buffer and
+     resize it as necessary. */
   int size = 32;
   char *str = xmalloc (size);
 
@@ -167,9 +180,6 @@ aprintf (const char *fmt, ...)
       int n;
       va_list args;
 
-      /* See log_vprintf_internal for explanation why it's OK to rely
-        on the return value of vsnprintf.  */
-
       va_start (args, fmt);
       n = vsnprintf (str, size, fmt, args);
       va_end (args);
@@ -185,6 +195,7 @@ aprintf (const char *fmt, ...)
        size <<= 1;             /* twice the old size */
       str = xrealloc (str, size);
     }
+#endif /* not HAVE_VASPRINTF */
 }
 
 /* Concatenate the NULL-terminated list of string arguments into