curlx: dedupe basename copies into curlx_basename()

Also stop redefining system `basename()` symbol. Call `curlx_basename()`
instead, and map that to `basename()` if available.

Closes #20424
This commit is contained in:
Viktor Szakats 2026-01-25 05:35:00 +01:00
parent 0e2507a3c6
commit 6974bd7cc8
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
7 changed files with 54 additions and 79 deletions

View 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 \

74
lib/curlx/basename.c Normal file
View file

@ -0,0 +1,74 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 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 https://curl.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.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "../curl_setup.h"
#ifndef HAVE_BASENAME
#include "basename.h"
/*
(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 : s2) + 1;
else if(s1)
path = s1 + 1;
else if(s2)
path = s2 + 1;
return path;
}
#endif /* !HAVE_BASENAME */

39
lib/curlx/basename.h Normal file
View file

@ -0,0 +1,39 @@
#ifndef HEADER_CURLX_BASENAME_H
#define HEADER_CURLX_BASENAME_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 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 https://curl.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.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "../curl_setup.h"
#ifndef HAVE_BASENAME
char *curlx_basename(char *path);
#else
#ifdef HAVE_LIBGEN_H
#include <libgen.h>
#endif
#define curlx_basename(x) basename(x)
#endif /* !HAVE_BASENAME */
#endif /* HEADER_CURLX_BASENAME_H */

View file

@ -31,6 +31,9 @@
* be.
*/
#include "basename.h"
/* for curlx_basename() function */
#include "binmode.h"
/* "binmode.h" provides macro CURLX_SET_BINMODE() */

View 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 */