[PATCH] Add asprintf function for non-gnu systems.

Clément Bœsch ubitux at gmail.com
Wed Jan 12 23:37:11 CET 2011


---
 Makefile         |    1 +
 configure        |   15 +++++++++++++++
 osdep/asprintf.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 osdep/asprintf.h |   26 ++++++++++++++++++++++++++
 4 files changed, 92 insertions(+), 0 deletions(-)
 create mode 100644 osdep/asprintf.c
 create mode 100644 osdep/asprintf.h

diff --git a/Makefile b/Makefile
index 2de75fb..f2df49c 100644
--- a/Makefile
+++ b/Makefile
@@ -176,6 +176,7 @@ SRCS_COMMON-$(NATIVE_RTSP)           += stream/stream_rtsp.c \
                                         stream/librtsp/rtsp_rtp.c \
                                         stream/librtsp/rtsp_session.c \
 
+SRCS_COMMON-$(NEED_ASPRINTF)         += osdep/asprintf.c
 SRCS_COMMON-$(NEED_GETTIMEOFDAY)     += osdep/gettimeofday.c
 SRCS_COMMON-$(NEED_GLOB)             += osdep/glob-win.c
 SRCS_COMMON-$(NEED_MMAP)             += osdep/mmap-os2.c
diff --git a/configure b/configure
index 3b32a23..87d88f5 100755
--- a/configure
+++ b/configure
@@ -3811,6 +3811,19 @@ fi
 echores "$_shm"
 
 
+echocheck "asprintf()"
+_asprintf=no
+statement_check stdio.h 'char *s; asprintf(&s, "%s:%d", "hello", 0x2a)' && _asprintf=yes
+if test "$_asprintf" = yes ; then
+  def_asprintf='#define HAVE_ASPRINTF 1'
+ _need_asprintf=no
+else
+  def_asprintf='#undef HAVE_ASPRINTF'
+ _need_asprintf=yes
+fi
+echores "$_asprintf"
+
+
 echocheck "strsep()"
 _strsep=no
 statement_check string.h 'char *s = "Hello, world!"; strsep(&s, ",")' && _strsep=yes
@@ -7920,6 +7933,7 @@ $(mak_enable "$cpuexts_all" "$cpuexts" HAVE)
 MENCODER = $_mencoder
 MPLAYER  = $_mplayer
 
+NEED_ASPRINTF     = $_need_asprintf
 NEED_GETTIMEOFDAY = $_need_gettimeofday
 NEED_GLOB         = $_need_glob
 NEED_MMAP         = $_need_mmap
@@ -8254,6 +8268,7 @@ $def_winsock2_h
 
 
 /* system functions */
+$def_asprintf
 $def_gethostbyname2
 $def_gettimeofday
 $def_glob
diff --git a/osdep/asprintf.c b/osdep/asprintf.c
new file mode 100644
index 0000000..97c1c25
--- /dev/null
+++ b/osdep/asprintf.c
@@ -0,0 +1,50 @@
+/*
+ * asprintf implementation for non-gnu systems
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer 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.
+ *
+ * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "asprintf.h"
+
+int asprintf(char **strp, const char *fmt, ...)
+{
+    va_list va, va_bak;
+    int len;
+
+    va_start(va, fmt);
+    va_copy(va_bak, va);
+
+    len = vsnprintf(NULL, 0, fmt, va);
+    if (len < 0)
+        goto end;
+
+    *strp = malloc(len + 1);
+    if (!*strp) {
+        len = -1;
+        goto end;
+    }
+
+    len = vsnprintf(*strp, len, fmt, va_bak);
+
+end:
+    va_end(va);
+    return len;
+}
diff --git a/osdep/asprintf.h b/osdep/asprintf.h
new file mode 100644
index 0000000..f101a9a
--- /dev/null
+++ b/osdep/asprintf.h
@@ -0,0 +1,26 @@
+/*
+ * asprintf implementation for non-gnu systems
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer 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.
+ *
+ * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_ASPRINTF_H
+#define MPLAYER_ASPRINTF_H
+
+int asprintf(char **strp, const char *fmt, ...);
+
+#endif /* MPLAYER_ASPRINTF_H */
-- 
1.7.3.5


--/04w6evG8XlLl3ft--


More information about the MPlayer-dev-eng mailing list