Browse Source

Moved strdup replacement from src/main.c into src/strdup.c so it's available
in libcurl as well, if necessary.

Dan Fandrich 18 years ago
parent
commit
c6fc5a1a26
7 changed files with 105 additions and 31 deletions
  1. 2 2
      lib/Makefile.inc
  2. 16 6
      lib/easy.c
  3. 43 0
      lib/strdup.c
  4. 34 0
      lib/strdup.h
  5. 2 1
      src/Makefile.inc
  6. 3 22
      src/main.c
  7. 5 0
      src/setup.h

+ 2 - 2
lib/Makefile.inc

@@ -8,7 +8,7 @@ CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c	\
   content_encoding.c share.c http_digest.c md5.c http_negotiate.c	\
   http_ntlm.c inet_pton.c strtoofft.c strerror.c hostares.c hostasyn.c	\
   hostip4.c hostip6.c hostsyn.c hostthre.c inet_ntop.c parsedate.c	\
-  select.c gtls.c sslgen.c tftp.c splay.c
+  select.c gtls.c sslgen.c tftp.c splay.c strdup.c
 
 HHEADERS = arpa_telnet.h netrc.h file.h timeval.h base64.h hostip.h	\
   progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h	\
@@ -18,6 +18,6 @@ HHEADERS = arpa_telnet.h netrc.h file.h timeval.h base64.h hostip.h	\
   share.h md5.h http_digest.h http_negotiate.h http_ntlm.h ca-bundle.h	\
   inet_pton.h strtoofft.h strerror.h inet_ntop.h curlx.h memory.h	\
   setup.h transfer.h select.h easyif.h multiif.h parsedate.h sslgen.h   \
-  gtls.h tftp.h sockaddr.h splay.h
+  gtls.h tftp.h sockaddr.h splay.h strdup.h
 
 

+ 16 - 6
lib/easy.c

@@ -80,6 +80,7 @@
 #include "getinfo.h"
 #include "hostip.h"
 #include "share.h"
+#include "strdup.h"
 #include "memory.h"
 #include "progress.h"
 #include "easyif.h"
@@ -181,19 +182,28 @@ static void idna_init (void)
 static unsigned int  initialized;
 static long          init_flags;
 
+/*
+ * strdup (and other memory functions) is redefined in complicated
+ * ways, but at this point it must be defined as the system-supplied strdup
+ * so the callback pointer is initialized correctly.
+ */
+#if defined(_WIN32_WCE)
+#define system_strdup _strdup
+#elif !defined(HAVE_STRDUP)
+#define system_strdup curlx_strdup
+#else
+#define system_strdup strdup
+#endif
+
 /*
  * If a memory-using function (like curl_getenv) is used before
  * curl_global_init() is called, we need to have these pointers set already.
  */
 
-#ifdef _WIN32_WCE
-#define strdup _strdup
-#endif
-
 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
 curl_free_callback Curl_cfree = (curl_free_callback)free;
 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
-curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
+curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)system_strdup;
 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
 
 /**
@@ -209,7 +219,7 @@ CURLcode curl_global_init(long flags)
   Curl_cmalloc = (curl_malloc_callback)malloc;
   Curl_cfree = (curl_free_callback)free;
   Curl_crealloc = (curl_realloc_callback)realloc;
-  Curl_cstrdup = (curl_strdup_callback)strdup;
+  Curl_cstrdup = (curl_strdup_callback)system_strdup;
   Curl_ccalloc = (curl_calloc_callback)calloc;
 
   if (flags & CURL_GLOBAL_SSL)

+ 43 - 0
lib/strdup.c

@@ -0,0 +1,43 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id$
+ ***************************************************************************/
+
+#include "setup.h"
+#include "strdup.h"
+
+#ifndef HAVE_STRDUP
+char *curlx_strdup(const char *str)
+{
+  int len;
+  char *newstr;
+
+  len = strlen(str);
+  newstr = (char *) malloc((len+1)*sizeof(char));
+  if (!newstr)
+    return (char *)NULL;
+
+  strcpy(newstr,str);
+
+  return newstr;
+
+}
+#endif

+ 34 - 0
lib/strdup.h

@@ -0,0 +1,34 @@
+/***************************************************************************
+ *                                  _   _ ____  _     
+ *  Project                     ___| | | |  _ \| |    
+ *                             / __| | | | |_) | |    
+ *                            | (__| |_| |  _ <| |___ 
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ * 
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id$
+ ***************************************************************************/
+
+#ifndef _CURL_STRDUP_H
+#define _CURL_STRDUP_H
+
+#include "setup.h"
+
+#ifndef HAVE_STRDUP
+extern char *curlx_strdup(const char *str);
+#endif
+
+#endif
+

+ 2 - 1
src/Makefile.inc

@@ -2,7 +2,8 @@
  
 # libcurl has sources that provide functions named curlx_* that aren't part of
 # the official API, but we re-use the code here to avoid duplication.
-CURLX_ONES =  $(top_srcdir)/lib/strtoofft.c $(top_srcdir)/lib/timeval.c
+CURLX_ONES =  $(top_srcdir)/lib/strtoofft.c $(top_srcdir)/lib/timeval.c \
+	$(top_srcdir)/lib/strdup.c 
 
 CURL_SOURCES = main.c hugehelp.c urlglob.c writeout.c writeenv.c \
 	getpass.c homedir.c

+ 3 - 22
src/main.c

@@ -183,25 +183,6 @@ typedef enum {
 /* Send authentication (user+password) when following
  * locations, even when hostname changed */
 
-#ifndef HAVE_STRDUP
-/* Ultrix doesn't have strdup(), so make a quick clone: */
-char *strdup(char *str)
-{
-  int len;
-  char *newstr;
-
-  len = strlen(str);
-  newstr = (char *) malloc((len+1)*sizeof(char));
-  if (!newstr)
-    return (char *)NULL;
-
-  strcpy(newstr,str);
-
-  return newstr;
-
-}
-#endif
-
 #ifdef WIN32
 #include <direct.h>
 #define F_OK 0
@@ -1271,11 +1252,11 @@ static ParameterError add2list(struct curl_slist **list,
 
 static int ftpfilemethod(struct Configurable *config, char *str)
 {
-  if(strequal("singlecwd", str))
+  if(curlx_strequal("singlecwd", str))
     return CURLFTPMETHOD_SINGLECWD;
-  if(strequal("nocwd", str))
+  if(curlx_strequal("nocwd", str))
     return CURLFTPMETHOD_NOCWD;
-  if(strequal("multicwd", str))
+  if(curlx_strequal("multicwd", str))
     return CURLFTPMETHOD_MULTICWD;
   warnf(config, "unrecognized ftp file method '%s', using default\n", str);
   return CURLFTPMETHOD_MULTICWD;

+ 5 - 0
src/setup.h

@@ -177,4 +177,9 @@ int fileno( FILE *stream);
 #define UNPRINTABLE_CHAR '.'
 #endif
 
+#ifndef HAVE_STRDUP
+#include "strdup.h"
+#define strdup(ptr) curlx_strdup(ptr)
+#endif
+
 #endif /* __SRC_CURL_SETUP_H */