Fixed ftp support with uClibc due to differing inet_ntoa_r() behaviour.
[platform/upstream/curl.git] / lib / inet_ntop.c
index 22c053e..293723a 100644 (file)
 #include <string.h>
 #include <errno.h>
 
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
 #include "inet_ntop.h"
 
-#define        IN6ADDRSZ       16
-#define        INADDRSZ         4
-#define        INT16SZ          2
+#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
+/* this platform has a inet_ntoa_r() function, but no proto declared anywhere
+   so we include our own proto to make compilers happy */
+#include "inet_ntoa_r.h"
+#endif
+
+#define IN6ADDRSZ       16
+#define INADDRSZ         4
+#define INT16SZ          2
 
 #ifdef WIN32
 #define EAFNOSUPPORT    WSAEAFNOSUPPORT
  */
 static const char *inet_ntop4 (const u_char *src, char *dst, size_t size)
 {
-#ifdef HAVE_INET_NTOA_R
+#if defined(HAVE_INET_NTOA_R_2_ARGS)
+  const char *ptr;
+  size_t len;
+  curlassert(size >= 16);
+  ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
+  len = strlen(ptr);
+  memmove(dst, ptr, len);
+  dst[len] = 0;
+  return dst;
+
+#elif defined(HAVE_INET_NTOA_R)
   return inet_ntoa_r(*(struct in_addr*)src, dst, size);
+
 #else
   const char *addr = inet_ntoa(*(struct in_addr*)src);
 
@@ -146,7 +166,7 @@ static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
       tp += strlen(tp);
       break;
     }
-    tp += sprintf (tp, "%lx", words[i]);
+    tp += snprintf(tp, 5, "%lx", words[i]);
   }
 
   /* Was it a trailing run of 0x00's?