aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog4
-rw-r--r--common/util.h4
-rw-r--r--common/xasprintf.c20
3 files changed, 27 insertions, 1 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index e323dc148..db0593176 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,7 @@
+2005-02-25 Werner Koch <[email protected]>
+
+ * xasprintf.c (xtryasprintf): New.
+
2005-01-26 Moritz Schulte <[email protected]>
* Makefile.am (libcommon_a_SOURCES): New source files: estream.c,
diff --git a/common/util.h b/common/util.h
index 4ab55acb4..bbf7241a3 100644
--- a/common/util.h
+++ b/common/util.h
@@ -131,6 +131,10 @@ const char *default_homedir (void);
freed using xfree. This function simply dies on memory failure,
thus no extra check is required. */
char *xasprintf (const char *fmt, ...) JNLIB_GCC_A_PRINTF(1,2);
+/* Same as asprintf but return an allocated buffer suitable to be
+ freed using xfree. This function returns NULL on memory failure and
+ sets errno. */
+char *xtryasprintf (const char *fmt, ...) JNLIB_GCC_A_PRINTF(1,2);
const char *print_fname_stdout (const char *s);
const char *print_fname_stdin (const char *s);
diff --git a/common/xasprintf.c b/common/xasprintf.c
index 2c8fafc06..a3b5e27ac 100644
--- a/common/xasprintf.c
+++ b/common/xasprintf.c
@@ -1,5 +1,5 @@
/* xasprintf.c
- * Copyright (C) 2003 Free Software Foundation, Inc.
+ * Copyright (C) 2003, 2005 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@@ -42,3 +42,21 @@ xasprintf (const char *fmt, ...)
free (buf);
return p;
}
+
+/* Same as above bit return NULL on memory failure. */
+char *
+xtryasprintf (const char *fmt, ...)
+{
+ int rc;
+ va_list ap;
+ char *buf, *p;
+
+ va_start (ap, fmt);
+ rc = vasprintf (&buf, fmt, ap);
+ va_end (ap);
+ if (rc < 0)
+ return NULL;
+ p = xtrystrdup (buf);
+ free (buf);
+ return p;
+}