Просмотр исходного кода

libcompat: Add support for asprintf and vasprintf

Provide compatibility code whenever the system does not.
Guillem Jover лет назад: 16
Родитель
Сommit
0dc899648b
5 измененных файлов с 109 добавлено и 1 удалено
  1. 1 1
      configure.ac
  2. 4 0
      lib/compat/Makefile.am
  3. 38 0
      lib/compat/asprintf.c
  4. 7 0
      lib/compat/compat.h
  5. 59 0
      lib/compat/vasprintf.c

+ 1 - 1
configure.ac

@@ -122,7 +122,7 @@ DPKG_CHECK_DECL([offsetof], [stddef.h])
 DPKG_CHECK_DECL([WCOREDUMP], [sys/wait.h])
 DPKG_CHECK_DECL([TIOCNOTTY], [sys/ioctl.h])
 DPKG_CHECK_COMPAT_FUNCS([getopt getopt_long obstack_free \
-                         strnlen strerror strsignal \
+                         strnlen strerror strsignal asprintf \
                          scandir alphasort unsetenv])
 AC_CHECK_FUNCS([strtoul isascii bcopy memcpy setsid getdtablesize \
                 posix_fadvise])

+ 4 - 0
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

+ 38 - 0
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

+ 7 - 0
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

+ 59 - 0
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