From 6d4e6cc8137ff2ebdbaf055e39ce1263ad634191 Mon Sep 17 00:00:00 2001
From: Yang Tse <yangsita@gmail.com>
Date: Sun, 18 Oct 2009 03:37:39 +0000
Subject: [PATCH] Check for basename() is now done the same as other function
 checks

---
 configure.ac         |  19 +-------
 lib/config-symbian.h |   3 --
 lib/config-tpf.h     |   3 --
 lib/config-vxworks.h |   3 --
 lib/formdata.c       |   8 +--
 m4/curl-functions.m4 | 114 ++++++++++++++++++++++++++++++++++++++++++-
 src/main.c           |   9 +++-
 7 files changed, 127 insertions(+), 32 deletions(-)

diff --git a/configure.ac b/configure.ac
index 56c763d937..095ec64de0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2034,6 +2034,7 @@ CURL_CHECK_FUNC_SEND
 CURL_CHECK_MSG_NOSIGNAL
 
 CURL_CHECK_FUNC_ALARM
+CURL_CHECK_FUNC_BASENAME
 CURL_CHECK_FUNC_CLOSESOCKET
 CURL_CHECK_FUNC_CLOSESOCKET_CAMEL
 CURL_CHECK_FUNC_CONNECT
@@ -2093,8 +2094,7 @@ case $host in
     ;;
 esac
 
-AC_CHECK_FUNCS([basename \
-  fork \
+AC_CHECK_FUNCS([fork \
   geteuid \
   getpass_r \
   getppid \
@@ -2132,21 +2132,6 @@ AC_CHECK_FUNCS([basename \
   fi
 ])
 
-
-AC_CHECK_DECL(basename, ,
-              AC_DEFINE(NEED_BASENAME_PROTO, 1, [If you lack a fine basename() prototype]),
-#ifdef HAVE_STRING_H
-#include <string.h>
-#endif
-#ifdef HAVE_LIBGEN_H
-#include <libgen.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-)
-
-
 dnl Check if the getnameinfo function is available
 dnl and get the types of five of its arguments.
 CURL_CHECK_FUNC_GETNAMEINFO
diff --git a/lib/config-symbian.h b/lib/config-symbian.h
index 1cdce26163..b26fe46cd5 100644
--- a/lib/config-symbian.h
+++ b/lib/config-symbian.h
@@ -634,9 +634,6 @@
 /* Define to 1 if you are building a native Windows target. */
 /* #undef NATIVE_WINDOWS */
 
-/* If you lack a fine basename() prototype */
-/* #undef NEED_BASENAME_PROTO */
-
 /* Define to 1 if you need the lber.h header file even with ldap.h */
 /* #undef NEED_LBER_H */
 
diff --git a/lib/config-tpf.h b/lib/config-tpf.h
index 3e482a6bd7..cea5d3bd92 100644
--- a/lib/config-tpf.h
+++ b/lib/config-tpf.h
@@ -559,9 +559,6 @@
 /* if you have the zlib.h header file */
 /* #undef HAVE_ZLIB_H */
 
-/* If you lack a fine basename() prototype */
-/* #undef NEED_BASENAME_PROTO */
-
 /* need REENTRANT defined */
 /* #undef NEED_REENTRANT */
 
diff --git a/lib/config-vxworks.h b/lib/config-vxworks.h
index 5fa94a9d2a..34a8c71727 100644
--- a/lib/config-vxworks.h
+++ b/lib/config-vxworks.h
@@ -733,9 +733,6 @@
 /* Define to 1 if you are building a native Windows target. */
 /* #undef NATIVE_WINDOWS */
 
-/* If you lack a fine basename() prototype */
-/* #undef NEED_BASENAME_PROTO */
-
 /* Define to 1 if you need the lber.h header file even with ldap.h */
 /* #undef NEED_LBER_H */
 
diff --git a/lib/formdata.c b/lib/formdata.c
index 9b458c0e21..f2d556ff22 100644
--- a/lib/formdata.c
+++ b/lib/formdata.c
@@ -135,9 +135,9 @@ Content-Disposition: form-data; name="FILECONTENT"
 
 #ifndef CURL_DISABLE_HTTP
 
-#if defined(HAVE_BASENAME) && defined(NEED_BASENAME_PROTO)
-/* This system has a basename() but no prototype for it! */
-char *basename(char *path);
+#ifndef HAVE_BASENAME
+static char *Curl_basename(char *path);
+#define basename(x)  Curl_basename((x))
 #endif
 
 static size_t readfromfile(struct Form *form, char *buffer, size_t size);
@@ -1067,7 +1067,7 @@ void curl_formfree(struct curl_httppost *form)
   required to be reentrant is not required to be thread-safe.
 
 */
-static char *basename(char *path)
+static char *Curl_basename(char *path)
 {
   /* Ignore all the details above for now and make a quick and simple
      implementaion here */
diff --git a/m4/curl-functions.m4 b/m4/curl-functions.m4
index 573560b79f..c366779370 100644
--- a/m4/curl-functions.m4
+++ b/m4/curl-functions.m4
@@ -22,7 +22,7 @@
 #***************************************************************************
 
 # File version for 'aclocal' use. Keep it a single number.
-# serial 53
+# serial 54
 
 
 dnl CURL_INCLUDES_ARPA_INET
@@ -127,6 +127,27 @@ curl_includes_inttypes="\
 ])
 
 
+dnl CURL_INCLUDES_LIBGEN
+dnl -------------------------------------------------
+dnl Set up variable with list of headers that must be
+dnl included when libgen.h is to be included.
+
+AC_DEFUN([CURL_INCLUDES_LIBGEN], [
+curl_includes_libgen="\
+/* includes start */
+#ifdef HAVE_SYS_TYPES_H
+#  include <sys/types.h>
+#endif
+#ifdef HAVE_LIBGEN_H
+#  include <libgen.h>
+#endif
+/* includes end */"
+  AC_CHECK_HEADERS(
+    sys/types.h libgen.h,
+    [], [], [$curl_includes_libgen])
+])
+
+
 dnl CURL_INCLUDES_NETDB
 dnl -------------------------------------------------
 dnl Set up variable with list of headers that must be
@@ -598,6 +619,97 @@ AC_DEFUN([CURL_CHECK_FUNC_ALARM], [
 ])
 
 
+dnl CURL_CHECK_FUNC_BASENAME
+dnl -------------------------------------------------
+dnl Verify if basename 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 curl_disallow_basename, then
+dnl HAVE_BASENAME will be defined.
+
+AC_DEFUN([CURL_CHECK_FUNC_BASENAME], [
+  AC_REQUIRE([CURL_INCLUDES_STRING])dnl
+  AC_REQUIRE([CURL_INCLUDES_LIBGEN])dnl
+  AC_REQUIRE([CURL_INCLUDES_UNISTD])dnl
+  #
+  tst_links_basename="unknown"
+  tst_proto_basename="unknown"
+  tst_compi_basename="unknown"
+  tst_allow_basename="unknown"
+  #
+  AC_MSG_CHECKING([if basename can be linked])
+  AC_LINK_IFELSE([
+    AC_LANG_FUNC_LINK_TRY([basename])
+  ],[
+    AC_MSG_RESULT([yes])
+    tst_links_basename="yes"
+  ],[
+    AC_MSG_RESULT([no])
+    tst_links_basename="no"
+  ])
+  #
+  if test "$tst_links_basename" = "yes"; then
+    AC_MSG_CHECKING([if basename is prototyped])
+    AC_EGREP_CPP([basename],[
+      $curl_includes_string
+      $curl_includes_libgen
+      $curl_includes_unistd
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_proto_basename="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_proto_basename="no"
+    ])
+  fi
+  #
+  if test "$tst_proto_basename" = "yes"; then
+    AC_MSG_CHECKING([if basename is compilable])
+    AC_COMPILE_IFELSE([
+      AC_LANG_PROGRAM([[
+        $curl_includes_string
+        $curl_includes_libgen
+        $curl_includes_unistd
+      ]],[[
+        if(0 != basename(0))
+          return 1;
+      ]])
+    ],[
+      AC_MSG_RESULT([yes])
+      tst_compi_basename="yes"
+    ],[
+      AC_MSG_RESULT([no])
+      tst_compi_basename="no"
+    ])
+  fi
+  #
+  if test "$tst_compi_basename" = "yes"; then
+    AC_MSG_CHECKING([if basename usage allowed])
+    if test "x$curl_disallow_basename" != "xyes"; then
+      AC_MSG_RESULT([yes])
+      tst_allow_basename="yes"
+    else
+      AC_MSG_RESULT([no])
+      tst_allow_basename="no"
+    fi
+  fi
+  #
+  AC_MSG_CHECKING([if basename might be used])
+  if test "$tst_links_basename" = "yes" &&
+     test "$tst_proto_basename" = "yes" &&
+     test "$tst_compi_basename" = "yes" &&
+     test "$tst_allow_basename" = "yes"; then
+    AC_MSG_RESULT([yes])
+    AC_DEFINE_UNQUOTED(HAVE_BASENAME, 1,
+      [Define to 1 if you have the basename function.])
+    ac_cv_func_basename="yes"
+  else
+    AC_MSG_RESULT([no])
+    ac_cv_func_basename="no"
+  fi
+])
+
+
 dnl CURL_CHECK_FUNC_CLOSESOCKET
 dnl -------------------------------------------------
 dnl Verify if closesocket is available, prototyped, and
diff --git a/src/main.c b/src/main.c
index f2efc2caa5..3459eb53f5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,6 +31,12 @@
 #include <ctype.h>
 #include <errno.h>
 
+#if defined(MSDOS) || defined(WIN32)
+#  if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
+#    include <libgen.h>
+#  endif
+#endif
+
 #include <curl/curl.h>
 
 #include "urlglob.h"
@@ -5437,7 +5443,7 @@ static int create_dir_hierarchy(const char *outfile, FILE *errors)
 /* basename() returns a pointer to the last component of a pathname.
  * Ripped from lib/formdata.c.
  */
-static char *basename(char *path)
+static char *Curl_basename(char *path)
 {
   /* Ignore all the details above for now and make a quick and simple
      implementaion here */
@@ -5457,6 +5463,7 @@ static char *basename(char *path)
 
   return path;
 }
+#define basename(x) Curl_basename((x))
 #endif /* HAVE_BASENAME */
 
 /* The following functions are taken with modification from the DJGPP
-- 
GitLab