diff options
author | Guillem Jover <guillem@debian.org> | 2010-06-06 21:06:34 +0200 |
---|---|---|
committer | Guillem Jover <guillem@debian.org> | 2010-06-07 04:16:45 +0200 |
commit | 0dc899648b1e907a745dbeedc5be46b3ddaabf8d (patch) | |
tree | f5fee3c56885b0298d0e256075e5d90e73426d59 /lib/compat | |
parent | 85c60fecfe245b542ceea503994fef0347f2e51b (diff) | |
download | dpkg-0dc899648b1e907a745dbeedc5be46b3ddaabf8d.tar.gz |
libcompat: Add support for asprintf and vasprintf
Provide compatibility code whenever the system does not.
Diffstat (limited to 'lib/compat')
-rw-r--r-- | lib/compat/Makefile.am | 4 | ||||
-rw-r--r-- | lib/compat/asprintf.c | 38 | ||||
-rw-r--r-- | lib/compat/compat.h | 7 | ||||
-rw-r--r-- | lib/compat/vasprintf.c | 59 |
4 files changed, 108 insertions, 0 deletions
diff --git a/lib/compat/Makefile.am b/lib/compat/Makefile.am index a0b3b4773..d71b6ead9 100644 --- a/lib/compat/Makefile.am +++ b/lib/compat/Makefile.am @@ -40,6 +40,10 @@ if !HAVE_C99_SNPRINTF libcompat_a_SOURCES += snprintf.c vsnprintf.c endif +if !HAVE_ASPRINTF +libcompat_a_SOURCES += asprintf.c vasprintf.c +endif + if !HAVE_ALPHASORT libcompat_a_SOURCES += alphasort.c endif diff --git a/lib/compat/asprintf.c b/lib/compat/asprintf.c new file mode 100644 index 000000000..223d514d7 --- /dev/null +++ b/lib/compat/asprintf.c @@ -0,0 +1,38 @@ +/* + * libcompat - system compatibility library + * + * Copyright © 2010 Guillem Jover <guillem@debian.org> + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include <stdarg.h> +#include <stdio.h> + +#ifndef HAVE_ASPRINTF +int +asprintf(char **strp, char const *fmt, ...) +{ + va_list args; + int n; + + va_start(args, fmt); + n = vasprintf(strp, fmt, args); + va_end(args); + + return n; +} +#endif diff --git a/lib/compat/compat.h b/lib/compat/compat.h index 551110c19..4cd215cd1 100644 --- a/lib/compat/compat.h +++ b/lib/compat/compat.h @@ -49,6 +49,13 @@ extern "C" { #include <strnlen.h> +#ifndef HAVE_ASPRINTF +#include <stdarg.h> + +int asprintf(char *str, char const *fmt, ...); +int vasprintf(char *str, const char *fmt, va_list args); +#endif + #ifndef HAVE_STRERROR const char *strerror(int); #endif diff --git a/lib/compat/vasprintf.c b/lib/compat/vasprintf.c new file mode 100644 index 000000000..3d2bb304a --- /dev/null +++ b/lib/compat/vasprintf.c @@ -0,0 +1,59 @@ +/* + * libcompat - system compatibility library + * + * Copyright © 2010 Guillem Jover <guillem@debian.org> + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#ifndef HAVE_VASPRINTF +int +vasprintf(char **strp, char const *fmt, va_list args) +{ + va_list args_copy; + int needed, n; + char *str; + + va_copy(args_copy, args); + needed = vsnprintf(NULL, 0, fmt, args_copy); + va_end(args_copy); + + if (needed < 0) { + *strp = NULL; + return -1; + } + + str = malloc(needed + 1); + if (str == NULL) { + *strp = NULL; + return -1; + } + + n = vsnprintf(str, needed + 1, fmt, args); + if (n < 0) { + free(str); + str = NULL; + } + + *strp = str; + + return n; +} +#endif |