[augeas-devel] [PATCH 3/8] Utility function xasprintf

David Lutterkort lutter at redhat.com
Mon Aug 31 21:44:29 UTC 2009


Like asprintf, but don't leave first arg in limbo on error

*  src/internal.h (xasprintf): new prototype
*  src/internal.c (xasprintf): new function
---
 src/internal.c |   12 ++++++++++++
 src/internal.h |    3 +++
 2 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/src/internal.c b/src/internal.c
index 86e64c2..4f0b779 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -377,6 +377,18 @@ const char *xstrerror(int errnum, char *buf, size_t len) {
 #endif
 }
 
+int xasprintf(char **strp, const char *format, ...) {
+  va_list args;
+  int result;
+
+  va_start (args, format);
+  result = vasprintf (strp, format, args);
+  va_end (args);
+  if (result < 0)
+      *strp = NULL;
+  return result;
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/src/internal.h b/src/internal.h
index 5a0c64c..fcc3982 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -263,6 +263,9 @@ char* read_file(const char *path);
  */
 const char *xstrerror(int errnum, char *buf, size_t len);
 
+/* Like asprintf, but set *STRP to NULL on error */
+int xasprintf(char **strp, const char *format, ...);
+
 /* Struct: augeas
  * The data structure representing a connection to Augeas. */
 struct augeas {
-- 
1.6.2.5




More information about the augeas-devel mailing list