strdup() clone for systems/configurations which lack it
authorYang Tse <yangsita@gmail.com>
Thu, 11 Sep 2008 04:02:49 +0000 (04:02 +0000)
committerYang Tse <yangsita@gmail.com>
Thu, 11 Sep 2008 04:02:49 +0000 (04:02 +0000)
Makefile.inc
ares_private.h
ares_strdup.c [new file with mode: 0644]
ares_strdup.h [new file with mode: 0644]
config-win32.h
configure.ac
m4/cares-functions.m4 [new file with mode: 0644]
setup.h

index c524802..2a2c784 100644 (file)
@@ -3,14 +3,14 @@ ares_query.c ares__close_sockets.c ares_free_string.c ares_search.c   \
 ares__get_hostent.c ares_gethostbyaddr.c ares_send.c ares__read_line.c \
 ares_gethostbyname.c ares_strerror.c ares_cancel.c ares_init.c         \
 ares_timeout.c ares_destroy.c ares_mkquery.c ares_version.c            \
-ares_expand_name.c ares_parse_a_reply.c windows_port.c                 \
+ares_expand_name.c ares_parse_a_reply.c windows_port.c ares_strdup.c   \
 ares_expand_string.c ares_parse_ptr_reply.c ares_parse_aaaa_reply.c    \
 ares_getnameinfo.c inet_net_pton.c bitncmp.c inet_ntop.c               \
 ares_parse_ns_reply.c ares_llist.c ares__timeval.c
 
 HHEADERS = ares.h ares_private.h setup.h ares_dns.h ares_version.h          \
            nameser.h inet_net_pton.h inet_ntop.h ares_ipv6.h bitncmp.h      \
-           setup_once.h ares_llist.h
+           setup_once.h ares_llist.h ares_strdup.h
 
 MANPAGES= ares_destroy.3 ares_expand_name.3 ares_expand_string.3 ares_fds.3 \
  ares_free_hostent.3 ares_free_string.3 ares_gethostbyaddr.3               \
index 38269b0..877985b 100644 (file)
 #include "ares_ipv6.h"
 #include "ares_llist.h"
 
+#ifndef HAVE_STRDUP
+#  include "ares_strdup.h"
+#  define strdup(ptr) ares_strdup(ptr)
+#endif
+
 struct query;
 
 struct send_request {
diff --git a/ares_strdup.c b/ares_strdup.c
new file mode 100644 (file)
index 0000000..284a813
--- /dev/null
@@ -0,0 +1,43 @@
+\r
+/* $Id$ */\r
+\r
+/* Copyright 1998 by the Massachusetts Institute of Technology.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this\r
+ * software and its documentation for any purpose and without\r
+ * fee is hereby granted, provided that the above copyright\r
+ * notice appear in all copies and that both that copyright\r
+ * notice and this permission notice appear in supporting\r
+ * documentation, and that the name of M.I.T. not be used in\r
+ * advertising or publicity pertaining to distribution of the\r
+ * software without specific, written prior permission.\r
+ * M.I.T. makes no representations about the suitability of\r
+ * this software for any purpose.  It is provided "as is"\r
+ * without express or implied warranty.\r
+ */\r
+\r
+#include "setup.h"\r
+#include "ares_strdup.h"\r
+\r
+#ifndef HAVE_STRDUP\r
+char *ares_strdup(const char *s1)\r
+{\r
+  size_t sz;\r
+  char * s2;\r
+\r
+  if(s1) {\r
+    sz = strlen(s1);\r
+    if(sz < (size_t)-1) {\r
+      sz++;\r
+      if(sz < ((size_t)-1) / sizeof(char)) {\r
+        s2 = malloc(sz * sizeof(char));\r
+        if(s2) {\r
+          memcpy(s2, s1, sz * sizeof(char));\r
+          return s2;\r
+        }\r
+      }\r
+    }\r
+  }\r
+  return (char *)NULL;\r
+}\r
+#endif\r
diff --git a/ares_strdup.h b/ares_strdup.h
new file mode 100644 (file)
index 0000000..4a649cb
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef HEADER_CARES_STRDUP_H\r
+#define HEADER_CARES_STRDUP_H\r
+\r
+/* $Id$ */\r
+\r
+/* Copyright 1998 by the Massachusetts Institute of Technology.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this\r
+ * software and its documentation for any purpose and without\r
+ * fee is hereby granted, provided that the above copyright\r
+ * notice appear in all copies and that both that copyright\r
+ * notice and this permission notice appear in supporting\r
+ * documentation, and that the name of M.I.T. not be used in\r
+ * advertising or publicity pertaining to distribution of the\r
+ * software without specific, written prior permission.\r
+ * M.I.T. makes no representations about the suitability of\r
+ * this software for any purpose.  It is provided "as is"\r
+ * without express or implied warranty.\r
+ */\r
+\r
+#include "setup.h"\r
+\r
+#ifndef HAVE_STRDUP\r
+extern char *ares_strdup(const char *s1);\r
+#endif\r
+\r
+#endif /* HEADER_CARES_STRDUP_H */\r
index eb8fed7..5e26fa6 100644 (file)
@@ -79,6 +79,9 @@
 /* Define if you have the ioctlsocket function.  */
 #define HAVE_IOCTLSOCKET 1
 
+/* Define if you have the strdup function. */
+#define HAVE_STRDUP 1
+
 /* Define if you have the recv function. */
 #define HAVE_RECV 1
 
index 745a39c..80b5be6 100644 (file)
@@ -643,6 +643,8 @@ CURL_CHECK_FUNC_RECVFROM
 CURL_CHECK_FUNC_SEND
 CURL_CHECK_MSG_NOSIGNAL
 
+CARES_CHECK_FUNC_STRDUP
+
 dnl check for AF_INET6
 CARES_CHECK_CONSTANT(
   [
diff --git a/m4/cares-functions.m4 b/m4/cares-functions.m4
new file mode 100644 (file)
index 0000000..3d263ca
--- /dev/null
@@ -0,0 +1,126 @@
+#***************************************************************************
+# $Id$
+#
+# Copyright (C) 2008 by Daniel Stenberg et al
+#
+# Permission to use, copy, modify, and distribute this software and its
+# documentation for any purpose and without fee is hereby granted, provided
+# that the above copyright notice appear in all copies and that both that
+# copyright notice and this permission notice appear in supporting
+# documentation, and that the name of M.I.T. not be used in advertising or
+# publicity pertaining to distribution of the software without specific,
+# written prior permission.  M.I.T. makes no representations about the
+# suitability of this software for any purpose.  It is provided "as is"
+# without express or implied warranty.
+#
+#***************************************************************************
+
+# File version for 'aclocal' use. Keep it a single number.
+# serial 1
+
+
+dnl CARES_INCLUDES_STRING
+dnl -------------------------------------------------
+dnl Set up variable with list of headers that must be
+dnl included when string.h is to be included.
+
+AC_DEFUN([CARES_INCLUDES_STRING], [
+cares_includes_string="\
+/* includes start */
+#ifdef HAVE_SYS_TYPES_H
+#  include <sys/types.h>
+#endif
+#ifdef HAVE_STRING_H
+#  include <string.h>
+#endif
+/* includes end */"
+  AC_CHECK_HEADERS(
+    sys/types.h string.h,
+    [], [], [$cares_includes_string])
+])
+
+
+dnl CARES_CHECK_FUNC_STRDUP
+dnl -------------------------------------------------
+dnl Verify if strdup is available, prototyped, and
+dnl can be compiled. If all of these are true, and
+dnl usage has not been previously disallowed with
+dnl shell variable cares_disallow_strdup, then
+dnl HAVE_STRDUP will be defined.
+
+AC_DEFUN([CARES_CHECK_FUNC_STRDUP], [
+  AC_REQUIRE([CARES_INCLUDES_STRING])dnl
+  #
+  tst_links_strdup="unknown"
+  tst_proto_strdup="unknown"
+  tst_compi_strdup="unknown"
+  tst_allow_strdup="unknown"
+  #
+  AC_MSG_CHECKING([if strdup can be linked])
+  AC_LINK_IFELSE([
+    AC_LANG_FUNC_LINK_TRY([strdup])
+  ],[
+    AC_MSG_RESULT([yes])
+    tst_links_strdup="yes"
+  ],[
+    AC_MSG_RESULT([no])
+    tst_links_strdup="no"
+  ])
+  #
+  if test "$tst_links_strdup" = "yes"; then
+    AC_MSG_CHECKING([if strdup is prototyped])
+    AC_EGREP_CPP([strdup],[
+      $cares_includes_string
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_proto_strdup="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_proto_strdup="no"
+    ])
+  fi
+  #
+  if test "$tst_proto_strdup" = "yes"; then
+    AC_MSG_CHECKING([if strdup is compilable])
+    AC_COMPILE_IFELSE([
+      AC_LANG_PROGRAM([[
+        $cares_includes_string
+      ]],[[
+        if(0 != strdup(0))
+          return 1;
+      ]])
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_compi_strdup="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_compi_strdup="no"
+    ])
+  fi
+  #
+  if test "$tst_compi_strdup" = "yes"; then
+    AC_MSG_CHECKING([if strdup usage allowed])
+    if test "x$cares_disallow_strdup" != "xyes"; then
+      AC_MSG_RESULT([yes])
+      tst_allow_strdup="yes"
+    else
+      AC_MSG_RESULT([no])
+      tst_allow_strdup="no"
+    fi
+  fi
+  #
+  AC_MSG_CHECKING([if strdup might be used])
+  if test "$tst_links_strdup" = "yes" &&
+     test "$tst_proto_strdup" = "yes" &&
+     test "$tst_compi_strdup" = "yes" &&
+     test "$tst_allow_strdup" = "yes"; then
+    AC_MSG_RESULT([yes])
+    AC_DEFINE_UNQUOTED(HAVE_STRDUP, 1,
+      [Define to 1 if you have the strdup function.])
+    ac_cv_func_strdup="yes"
+  else
+    AC_MSG_RESULT([no])
+    ac_cv_func_strdup="no"
+  fi
+])
+
diff --git a/setup.h b/setup.h
index 9fe081b..5ae3b9e 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -150,11 +150,6 @@ int ares_strcasecmp(const char *s1, const char *s2);
    same */
 #define strncasecmp(a,b,c) ares_strncasecmp(a,b,c)
 #define strcasecmp(a,b) ares_strcasecmp(a,b)
-#ifdef _MSC_VER
-#  if _MSC_VER >= 1400
-#    define strdup(a) _strdup(a)
-#  endif
-#endif
 #endif
 
 /* IPv6 compatibility */