src: drop detecting and redefining system symbol ftruncate

Introduce `toolx_ftruncate()` macro and map it to existing replacements
for non-mingw-w64 Windows and DJGPP, or to `ftruncate` otherwise.

Follow-up to 6041b9b11b #21109

Closes #21130
This commit is contained in:
Viktor Szakats 2026-03-27 21:51:43 +01:00
parent 335dc0e3c5
commit 20b18a43f6
No known key found for this signature in database
14 changed files with 17 additions and 135 deletions

View file

@ -270,7 +270,7 @@ static size_t save_etag(const char *etag_h, const char *endp,
if((fd != -1) &&
!curlx_fstat(fd, &file) &&
(S_ISREG(file.st_mode) &&
ftruncate(fd, 0)))
toolx_ftruncate(fd, 0)))
return CURL_WRITEFUNC_ERROR;
fwrite(etag_h, 1, etag_length, etag_save->stream);

View file

@ -559,7 +559,7 @@ static CURLcode retrycheck(struct OperationConfig *config,
notef("Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes", outs->bytes);
/* truncate file at the position where we started appending */
if(ftruncate(fileno(outs->stream), outs->init)) {
if(toolx_ftruncate(fileno(outs->stream), outs->init)) {
/* when truncate fails, we cannot append as then we
create something strange, bail out */
errorf("Failed to truncate file");

View file

@ -94,26 +94,16 @@ extern FILE *tool_stderr;
#ifdef _WIN32
/* set in init_terminal() */
extern bool tool_term_has_bold;
#endif
#ifndef HAVE_FTRUNCATE
int tool_ftruncate64(int fd, curl_off_t where);
#undef ftruncate
#define ftruncate(fd, where) tool_ftruncate64(fd, where)
#define HAVE_FTRUNCATE 1
#define USE_TOOL_FTRUNCATE 1
#endif /* !HAVE_FTRUNCATE */
#endif /* _WIN32 */
#ifdef __DJGPP__
int msdos_ftruncate(int fd, curl_off_t where);
#undef HAVE_FTRUNCATE
#undef ftruncate /* to be sure */
#define ftruncate(fd, where) msdos_ftruncate(fd, where)
#define USE_MSDOS_FTRUNCATE 1
#if defined(_WIN32) && !defined(__MINGW32__)
int toolx_ftruncate_win32(int fd, curl_off_t where);
#define toolx_ftruncate toolx_ftruncate_win32
#elif defined(__DJGPP__)
int toolx_ftruncate_djgpp(int fd, curl_off_t where);
#define toolx_ftruncate toolx_ftruncate_djgpp
#else
#define toolx_ftruncate ftruncate
#endif
#ifdef CURL_CA_EMBED

View file

@ -78,11 +78,11 @@ int struplocompare4sort(const void *p1, const void *p2)
return struplocompare(*(char * const *)p1, *(char * const *)p2);
}
#ifdef USE_TOOL_FTRUNCATE
#if defined(_WIN32) && !defined(__MINGW32__)
/*
* Truncate a file handle at a 64-bit position 'where'.
*/
int tool_ftruncate64(int fd, curl_off_t where)
int toolx_ftruncate_win32(int fd, curl_off_t where)
{
intptr_t handle = _get_osfhandle(fd);
@ -94,21 +94,18 @@ int tool_ftruncate64(int fd, curl_off_t where)
return 0;
}
#endif /* USE_TOOL_FTRUNCATE */
#ifdef USE_MSDOS_FTRUNCATE
#elif defined(__DJGPP__)
/*
* Only supports 'off_t' (signed 32 bit) as file size.
*/
int msdos_ftruncate(int fd, curl_off_t where)
int toolx_ftruncate_djgpp(int fd, curl_off_t where)
{
if(where > INT_MAX)
return -1;
/* avoid using the macro for this */
return (ftruncate)(fd, (off_t) where);
return ftruncate(fd, (off_t)where);
}
#endif /* USE_MSDOS_FTRUNCATE */
#endif
#ifdef _WIN32
FILE *tool_execpath(const char *filename, char **pathp)