Add xasprintf() and xvasprintf().
[oweals/tinc.git] / lib / xmalloc.c
index 204469f240ab7a67a48f92bdbe3e2ed92aafec61..51356e460b71b45c48d08cf9e6b3e0436b28db50 100644 (file)
 #endif
 
 #include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
 
 #if STDC_HEADERS
 # include <stdlib.h>
@@ -30,16 +34,7 @@ void *realloc ();
 void free ();
 #endif
 
-#if ENABLE_NLS
-# include <libintl.h>
-# define _(Text) gettext (Text)
-#else
-# define textdomain(Domain)
-# define _(Text) Text
-#endif
-#define N_(Text) Text
-
-#include "error.h"
+#include "gettext.h"
 #include "xalloc.h"
 
 #ifndef EXIT_FAILURE
@@ -53,14 +48,6 @@ void *xcalloc (size_t n, size_t s);
 void *xrealloc (void *p, size_t n);
 #endif
 
-#ifndef HAVE_DONE_WORKING_MALLOC_CHECK
-you must run the autoconf test for a properly working malloc -- see malloc.m4
-#endif
-
-#ifndef HAVE_DONE_WORKING_REALLOC_CHECK
-you must run the autoconf test for a properly working realloc -- see realloc.m4
-#endif
-
 /* Exit value when the requested amount of memory is not available.
    The caller may set it to some other value.  */
 int xalloc_exit_failure = EXIT_FAILURE;
@@ -69,20 +56,15 @@ int xalloc_exit_failure = EXIT_FAILURE;
 char *const xalloc_msg_memory_exhausted = N_("Memory exhausted");
 
 /* FIXME: describe */
-void (*xalloc_fail_func) () = 0;
-
-#if __STDC__ && (HAVE_VPRINTF || HAVE_DOPRNT)
-void error (int, int, const char *, ...);
-#else
-void error ();
-#endif
+void (*xalloc_fail_func) (int) = 0;
 
 static void
-xalloc_fail ()
+xalloc_fail (int size)
 {
   if (xalloc_fail_func)
-    (*xalloc_fail_func) ();
-  error (xalloc_exit_failure, 0, xalloc_msg_memory_exhausted);
+    (*xalloc_fail_func) (size);
+  fprintf(stderr, "%s\n", xalloc_msg_memory_exhausted);
+  exit(xalloc_exit_failure);
 }
 
 /* Allocate N bytes of memory dynamically, with error checking.  */
@@ -95,7 +77,22 @@ xmalloc (n)
 
   p = malloc (n);
   if (p == 0)
-    xalloc_fail ();
+    xalloc_fail ((int)n);
+  return p;
+}
+
+/* Allocate N bytes of memory dynamically, and set it all to zero. */
+
+void *
+xmalloc_and_zero (n)
+     size_t n;
+{
+  void *p;
+
+  p = malloc (n);
+  if (p == 0)
+    xalloc_fail ((int)n);
+  memset (p, '\0', n);
   return p;
 }
 
@@ -110,7 +107,19 @@ xrealloc (p, n)
 {
   p = realloc (p, n);
   if (p == 0)
-    xalloc_fail ();
+    xalloc_fail (n);
+  return p;
+}
+
+/* Duplicate a string */
+
+char *xstrdup(const char *s)
+{
+  char *p;
+  
+  p = strdup(s);
+  if(!p)
+    xalloc_fail ((int)strlen(s));
   return p;
 }
 
@@ -131,3 +140,21 @@ xcalloc (n, s)
 }
 
 #endif /* NOT_USED */
+
+int xasprintf(char **strp, const char *fmt, ...) {
+       int result;
+       va_list ap;
+       va_start(ap, fmt);
+       result = xvasprintf(strp, fmt, ap);
+       va_end(ap);
+       return result;
+}
+
+int xvasprintf(char **strp, const char *fmt, va_list ap) {
+       int result = vasprintf(strp, fmt, ap);
+       if(result < 0) {
+               fprintf(stderr, "vasprintf() failed: %s\n", strerror(errno));
+               exit(xalloc_exit_failure);
+       }
+       return result;
+}