[pacman-dev] [PATCH 4/6] common: Add support for systems without asprintf/vasprintf.
Will Miles
wmiles at sgl.com
Fri Feb 27 19:43:26 UTC 2015
Signed-off-by: Will Miles <wmiles at sgl.com>
---
configure.ac | 2 +-
src/common/util-common.c | 27 +++++++++++++++++++++++++++
src/common/util-common.h | 7 +++++++
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/configure.ac b/configure.ac
index 359d61b..da1953e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -302,7 +302,7 @@ AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_FUNC_STRCOLL
-AC_CHECK_FUNCS([dup2 getcwd getmntinfo gettimeofday memmove memset \
+AC_CHECK_FUNCS([asprintf dup2 getcwd getmntinfo gettimeofday memmove memset \
mkdir realpath regcomp rmdir setenv setlocale strcasecmp \
strchr strcspn strdup strerror strndup strnlen strrchr \
strsep strstr strtol swprintf tcflush wcwidth uname])
diff --git a/src/common/util-common.c b/src/common/util-common.c
index 6c9b263..a7284a8 100644
--- a/src/common/util-common.c
+++ b/src/common/util-common.c
@@ -388,4 +388,31 @@ int wcswidth(const wchar_t *pwcs, size_t n)
#endif /* HAVE_WCWIDTH */
+#ifndef HAVE_ASPRINTF
+int asprintf(char **strp, const char *fmt, ...)
+{
+ int ret = 0;
+ va_list args;
+
+ /* print the message using va_arg list */
+ va_start(args, fmt);
+ ret = vasprintf(strp, fmt, args);
+ va_end(args);
+ return ret;
+}
+
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+ /* measure the string */
+ int len = vsnprintf(NULL,0,fmt,ap);
+ /* try allocating some memory */
+ if((len < 0) || ((*strp = malloc(++len)) == NULL)) return -1;
+ /* print the string */
+ len = vsnprintf(*strp,len,fmt,ap);
+ /* handle failure */
+ if(len < 0) free(*strp);
+ return len;
+}
+#endif /* HAVE_ASPRINTF */
+
/* vim: set noet: */
diff --git a/src/common/util-common.h b/src/common/util-common.h
index 117486f..ca1bc5d 100644
--- a/src/common/util-common.h
+++ b/src/common/util-common.h
@@ -22,6 +22,9 @@
#include <stdio.h>
#include <sys/stat.h> /* struct stat */
+#ifndef HAVE_ASPRINTF
+#include <stdarg.h>
+#endif
const char *mbasename(const char *path);
char *mdirname(const char *path);
@@ -39,6 +42,10 @@ int wcwidth(wchar_t ucs);
int wcswidth(const wchar_t *pwcs, size_t n);
#endif
+#ifndef HAVE_ASPRINTF
+int asprintf(char **strp, const char *fmt, ...) __attribute__((format(printf,2,3)));
+int vasprintf(char **strp, const char *fmt, va_list ap) __attribute__((format(printf,3,0)));
+#endif
#endif /* _PM_UTIL_COMMON_H */
--
2.3.0
More information about the pacman-dev
mailing list