Signed-off-by: Will Miles <wmiles@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 e977550..d7ef08b 100644 --- a/src/common/util-common.c +++ b/src/common/util-common.c @@ -305,4 +305,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