summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2013-03-03 09:54:59 +0100
committerStef Walter <stefw@gnome.org>2013-03-03 10:07:14 +0100
commit193f0043a546e0ef186addb2a0487d09e690d5b1 (patch)
tree2788d3662230edea60878dc453672a1663744995
parent66ee55e5947682d10eed7a36b9da72a8cf6a40f2 (diff)
downloadp11-kit-193f0043a546e0ef186addb2a0487d09e690d5b1.tar.gz
Add compat vasprintf() and asprintf() functions
These are not available on Win32 and ancient unixes
-rw-r--r--common/compat.c66
-rw-r--r--common/compat.h17
-rw-r--r--configure.ac1
3 files changed, 84 insertions, 0 deletions
diff --git a/common/compat.c b/common/compat.c
index ff8ee08..3aae490 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -485,3 +485,69 @@ strconcat (const char *first,
}
#endif /* HAVE_STRCONCAT */
+
+#ifndef HAVE_ASPRINTF
+
+int
+asprintf (char **strp,
+ const char *fmt,
+ ...)
+{
+ va_list va;
+ int ret;
+
+ va_start (va, fmt);
+ ret = vasprintf (strp, fmt, va);
+ va_end (va);
+
+ return ret;
+}
+
+#endif /* HAVE_ASPRINTF */
+
+#ifndef HAVE_VASPRINTF
+#include <stdio.h>
+
+int
+vasprintf (char **strp,
+ const char *fmt,
+ va_list ap)
+{
+ char *buf = NULL;
+ char *nbuf;
+ int guess = 128;
+ int length = 0;
+ int ret;
+
+ if (fmt == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ for (;;) {
+ nbuf = realloc (buf, guess);
+ if (!nbuf) {
+ free (buf);
+ return -1;
+ }
+
+ buf = nbuf;
+ length = guess;
+
+ ret = vsnprintf (buf, length, fmt, ap);
+
+ if (ret < 0)
+ guess *= 2;
+
+ else if (ret >= length)
+ guess = ret + 1;
+
+ else
+ break;
+ }
+
+ *strp = buf;
+ return ret;
+}
+
+#endif /* HAVE_VASPRINTF */
diff --git a/common/compat.h b/common/compat.h
index 27e4403..cb4d2ad 100644
--- a/common/compat.h
+++ b/common/compat.h
@@ -243,4 +243,21 @@ char * strconcat (const char *first,
#endif /* HAVE_STRCONCAT */
+#ifndef HAVE_ASPRINTF
+
+int asprintf (char **strp,
+ const char *fmt,
+ ...);
+
+#endif /* HAVE_ASPRINTF */
+
+#ifndef HAVE_VASPRINTF
+#include <stdarg.h>
+
+int vasprintf (char **strp,
+ const char *fmt,
+ va_list ap);
+
+#endif /* HAVE_VASPRINTF */
+
#endif /* __COMPAT_H__ */
diff --git a/configure.ac b/configure.ac
index 11ac492..ac049c9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -77,6 +77,7 @@ if test "$os_unix" = "yes"; then
AC_CHECK_MEMBERS([struct dirent.d_type],,,[#include <dirent.h>])
AC_CHECK_FUNCS([getprogname getexecname basename mkstemp mkdtemp])
AC_CHECK_FUNCS([strnstr memdup strndup])
+ AC_CHECK_FUNCS([asprintf vasprintf vsnprintf])
# Check if these are declared and/or available to link against
AC_CHECK_DECLS([program_invocation_short_name])