]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curlx: dedupe basename copies into `curlx_basename()`
authorViktor Szakats <commit@vsz.me>
Sun, 25 Jan 2026 04:35:00 +0000 (05:35 +0100)
committerViktor Szakats <commit@vsz.me>
Sun, 25 Jan 2026 11:21:54 +0000 (12:21 +0100)
Also stop redefining system `basename()` symbol. Call `curlx_basename()`
instead, and map that to `basename()` if available.

Closes #20424

lib/Makefile.inc
lib/curlx/basename.c [moved from src/tool_bname.c with 50% similarity]
lib/curlx/basename.h [moved from src/tool_bname.h with 79% similarity]
lib/curlx/curlx.h
lib/mime.c
src/Makefile.inc
src/tool_doswin.c

index d5c55dff6e5ba84c2374501aadbea6f323c8fe88..0146d875575b5d67864decfd9a7ef1fcd0c99dbe 100644 (file)
@@ -25,6 +25,7 @@
 
 LIB_CURLX_CFILES =      \
   curlx/base64.c        \
+  curlx/basename.c      \
   curlx/dynbuf.c        \
   curlx/fopen.c         \
   curlx/inet_ntop.c     \
@@ -42,8 +43,9 @@ LIB_CURLX_CFILES =      \
   curlx/winapi.c
 
 LIB_CURLX_HFILES =      \
-  curlx/binmode.h       \
   curlx/base64.h        \
+  curlx/binmode.h       \
+  curlx/basename.h      \
   curlx/curlx.h         \
   curlx/dynbuf.h        \
   curlx/fopen.h         \
similarity index 50%
rename from src/tool_bname.c
rename to lib/curlx/basename.c
index f628d819fb2ed22dee75deba4a3f8ed3cf2e375e..27c29f3c00aace71a0a9f9194c11dc580c530429 100644 (file)
  * SPDX-License-Identifier: curl
  *
  ***************************************************************************/
-#include "tool_setup.h"
+#include "../curl_setup.h"
 
 #ifndef HAVE_BASENAME
 
-#include "tool_bname.h"
+#include "basename.h"
 
-char *tool_basename(char *path)
+/*
+  (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
+  Edition)
+
+  The basename() function shall take the pathname pointed to by path and
+  return a pointer to the final component of the pathname, deleting any
+  trailing '/' characters.
+
+  If the string pointed to by path consists entirely of the '/' character,
+  basename() shall return a pointer to the string "/". If the string pointed
+  to by path is exactly "//", it is implementation-defined whether '/' or "//"
+  is returned.
+
+  If path is a null pointer or points to an empty string, basename() shall
+  return a pointer to the string ".".
+
+  The basename() function may modify the string pointed to by path, and may
+  return a pointer to static storage that may then be overwritten by a
+  subsequent call to basename().
+
+  The basename() function need not be reentrant. A function that is not
+  required to be reentrant is not required to be thread-safe.
+
+*/
+char *curlx_basename(char *path)
 {
+  /* Ignore all the details above for now and make a quick and simple
+     implementation here */
   char *s1;
   char *s2;
 
   s1 = strrchr(path, '/');
   s2 = strrchr(path, '\\');
 
-  if(s1 && s2) {
-    path = (s1 > s2) ? s1 + 1 : s2 + 1;
-  }
+  if(s1 && s2)
+    path = ((s1 > s2) ? s1 : s2) + 1;
   else if(s1)
     path = s1 + 1;
   else if(s2)
@@ -46,4 +71,4 @@ char *tool_basename(char *path)
   return path;
 }
 
-#endif /* HAVE_BASENAME */
+#endif /* !HAVE_BASENAME */
similarity index 79%
rename from src/tool_bname.h
rename to lib/curlx/basename.h
index 4047c5cc78c8d35eb241a758d5b2fc753dd680c5..287a14aa14aef0d2aae54a56a5a4520ba5ad402f 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef HEADER_CURL_TOOL_BNAME_H
-#define HEADER_CURL_TOOL_BNAME_H
+#ifndef HEADER_CURLX_BASENAME_H
+#define HEADER_CURLX_BASENAME_H
 /***************************************************************************
  *                                  _   _ ____  _
  *  Project                     ___| | | |  _ \| |
  * SPDX-License-Identifier: curl
  *
  ***************************************************************************/
-#include "tool_setup.h"
+#include "../curl_setup.h"
 
 #ifndef HAVE_BASENAME
+char *curlx_basename(char *path);
+#else
 
-char *tool_basename(char *path);
+#ifdef HAVE_LIBGEN_H
+#include <libgen.h>
+#endif
 
-#define basename(x) tool_basename(x)
+#define curlx_basename(x) basename(x)
+#endif /* !HAVE_BASENAME */
 
-#endif /* HAVE_BASENAME */
-
-#endif /* HEADER_CURL_TOOL_BNAME_H */
+#endif /* HEADER_CURLX_BASENAME_H */
index 817acb07cde9e5000cadd46af5d04ef314988333..1733625fd8d06b88276144f321a45e6a1e903bc5 100644 (file)
@@ -31,6 +31,9 @@
  * be.
  */
 
+#include "basename.h"
+/* for curlx_basename() function */
+
 #include "binmode.h"
 /* "binmode.h" provides macro CURLX_SET_BINMODE() */
 
index 4d8177c0667cd66130699527fe65d9682df777d6..45a93eb3fc0cae82b167434f6db7147f40983bfc 100644 (file)
@@ -31,6 +31,7 @@ struct Curl_easy;
 #include "curl_trc.h"
 #include "transfer.h"
 #include "strdup.h"
+#include "curlx/basename.h"
 #include "curlx/strcopy.h"
 #include "curlx/fopen.h"
 #include "curlx/base64.h"
@@ -39,10 +40,6 @@ struct Curl_easy;
                                     !defined(CURL_DISABLE_SMTP) ||      \
                                     !defined(CURL_DISABLE_IMAP))
 
-#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
-#include <libgen.h>
-#endif
-
 #include "rand.h"
 #include "slist.h"
 #include "curlx/dynbuf.h"
@@ -182,55 +179,6 @@ static FILE *vmsfopenread(const char *file, const char *mode)
 #define fopen_read vmsfopenread
 #endif /* !__VMS */
 
-#ifndef HAVE_BASENAME
-/*
-  (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
-  Edition)
-
-  The basename() function shall take the pathname pointed to by path and
-  return a pointer to the final component of the pathname, deleting any
-  trailing '/' characters.
-
-  If the string pointed to by path consists entirely of the '/' character,
-  basename() shall return a pointer to the string "/". If the string pointed
-  to by path is exactly "//", it is implementation-defined whether '/' or "//"
-  is returned.
-
-  If path is a null pointer or points to an empty string, basename() shall
-  return a pointer to the string ".".
-
-  The basename() function may modify the string pointed to by path, and may
-  return a pointer to static storage that may then be overwritten by a
-  subsequent call to basename().
-
-  The basename() function need not be reentrant. A function that is not
-  required to be reentrant is not required to be thread-safe.
-
-*/
-static char *Curl_basename(char *path)
-{
-  /* Ignore all the details above for now and make a quick and simple
-     implementation here */
-  char *s1;
-  char *s2;
-
-  s1 = strrchr(path, '/');
-  s2 = strrchr(path, '\\');
-
-  if(s1 && s2) {
-    path = (s1 > s2 ? s1 : s2) + 1;
-  }
-  else if(s1)
-    path = s1 + 1;
-  else if(s2)
-    path = s2 + 1;
-
-  return path;
-}
-
-#define basename(x)  Curl_basename(x)
-#endif /* !HAVE_BASENAME */
-
 /* Set readback state. */
 static void mimesetstate(struct mime_state *state,
                          enum mimestate tok, void *ptr)
@@ -319,7 +267,7 @@ static char *strippath(const char *fullfile)
                                         the buffer it works on */
   if(!filename)
     return NULL;
-  base = curlx_strdup(basename(filename));
+  base = curlx_strdup(curlx_basename(filename));
 
   curlx_free(filename); /* free temporary buffer */
 
index 4e8efdd9265bee9d36800f1d7bc591811d23f8bf..d17d92f829658a3de1711cbc2f72245244b08cf9 100644 (file)
@@ -34,6 +34,7 @@
 # the official API, but we reuse the code here to avoid duplication.
 CURLX_CFILES = \
   ../lib/curlx/base64.c \
+  ../lib/curlx/basename.c \
   ../lib/curlx/dynbuf.c \
   ../lib/curlx/fopen.c \
   ../lib/curlx/multibyte.c \
@@ -76,7 +77,6 @@ CURL_CFILES = \
   config2setopts.c \
   slist_wc.c \
   terminal.c \
-  tool_bname.c \
   tool_cb_dbg.c \
   tool_cb_hdr.c \
   tool_cb_prg.c \
@@ -122,7 +122,6 @@ CURL_HFILES = \
   config2setopts.h \
   slist_wc.h \
   terminal.h \
-  tool_bname.h \
   tool_cb_dbg.h \
   tool_cb_hdr.h \
   tool_cb_prg.h \
index f4fb9f8e8325884745814572ccec3ac4c88a2f1d..e1ba4f8fc1dd9ea3dae2e61a3f084d760f4d5c08 100644 (file)
 
 #if defined(_WIN32) || defined(MSDOS)
 
-#if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
-#  include <libgen.h>
-#endif
-
 #ifdef _WIN32
 #  include <tlhelp32.h>
 #  undef  PATH_MAX
@@ -41,7 +37,6 @@
 #endif
 
 #include "tool_cfgable.h"
-#include "tool_bname.h"
 #include "tool_doswin.h"
 #include "tool_msgs.h"
 
@@ -327,7 +322,7 @@ static SANITIZEcode rename_if_reserved_dos(char ** const sanitized,
 
   memcpy(buffer, file_name, len + 1);
 
-  base = basename(buffer);
+  base = curlx_basename(buffer);
 
   /* Rename reserved device names that are known to be accessible without \\.\
      Examples: CON => _CON, CON.EXT => CON_EXT, CON:ADS => CON_ADS
@@ -378,7 +373,7 @@ static SANITIZEcode rename_if_reserved_dos(char ** const sanitized,
 
     /* the basename pointer must be updated since the path has expanded */
     if(p == buffer)
-      base = basename(buffer);
+      base = curlx_basename(buffer);
   }
 
   /* This is the legacy portion from rename_if_dos_device_name that checks for