build: avoid overriding system symbols for fopen functions

By introducing wrappers for them in the curlx namespace:
`curlx_fopen()`, `curlx_fdopen()`, `curlx_fclose()`.

The undefine/redefine/`(function)()` methods broke on systems
implementing these functions as macros. E.g. AIX 32-bit's `fopen()`.

Also:
- rename `lib/fopen.*` to `lib/curl_fopen.*` (for `Curl_fopen()`)
  to make room for the newly added `curlx/fopen.h`.
- curlx: move file-related functions from `multibyte.c` to `fopen.c`.
- tests/server: stop using the curl-specific `fopen()` implementation
  on Windows. Unicode isn't used by runtests, and it isn't critical to
  run tests on longs path. It can be re-enabled if this becomes
  necessary, or if the wrapper receives a feature that's critical for
  test servers.

Reported-by: Andrew Kirillov
Bug: https://github.com/curl/curl/issues/18510#issuecomment-3274393640

Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #18634
This commit is contained in:
Viktor Szakats 2025-09-14 15:34:18 +02:00
parent 10bac43b87
commit 20142f5d06
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
65 changed files with 568 additions and 484 deletions

View file

@ -627,7 +627,7 @@ static ParameterError data_urlencode(const char *nextarg,
CURLX_SET_BINMODE(stdin);
}
else {
file = fopen(p, "rb");
file = curlx_fopen(p, "rb");
if(!file) {
errorf("Failed to open %s", p);
return PARAM_READ_ERROR;
@ -637,7 +637,7 @@ static ParameterError data_urlencode(const char *nextarg,
err = file2memory(&postdata, &size, file);
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
}
@ -899,7 +899,7 @@ static ParameterError set_data(cmdline_t cmd,
CURLX_SET_BINMODE(stdin);
}
else {
file = fopen(nextarg, "rb");
file = curlx_fopen(nextarg, "rb");
if(!file) {
errorf("Failed to open %s", nextarg);
return PARAM_READ_ERROR;
@ -917,7 +917,7 @@ static ParameterError set_data(cmdline_t cmd,
}
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
@ -1094,7 +1094,7 @@ static ParameterError parse_url(struct OperationConfig *config,
if(fromstdin)
f = stdin;
else
f = fopen(&nextarg[1], FOPEN_READTEXT);
f = curlx_fopen(&nextarg[1], FOPEN_READTEXT);
if(f) {
curlx_dyn_init(&line, 8092);
while(my_get_line(f, &line, &error)) {
@ -1104,7 +1104,7 @@ static ParameterError parse_url(struct OperationConfig *config,
break;
}
if(!fromstdin)
fclose(f);
curlx_fclose(f);
curlx_dyn_free(&line);
if(error || err)
return PARAM_READ_ERROR;
@ -1206,7 +1206,7 @@ static ParameterError parse_ech(struct OperationConfig *config,
file = stdin;
}
else {
file = fopen(nextarg, FOPEN_READTEXT);
file = curlx_fopen(nextarg, FOPEN_READTEXT);
}
if(!file) {
warnf("Couldn't read file \"%s\" "
@ -1216,7 +1216,7 @@ static ParameterError parse_ech(struct OperationConfig *config,
}
err = file2string(&tmpcfg, file);
if(file != stdin)
fclose(file);
curlx_fclose(file);
if(err)
return err;
config->ech_config = aprintf("ecl:%s",tmpcfg);
@ -1242,7 +1242,7 @@ static ParameterError parse_header(struct OperationConfig *config,
if(nextarg[0] == '@') {
/* read many headers from a file or stdin */
bool use_stdin = !strcmp(&nextarg[1], "-");
FILE *file = use_stdin ? stdin : fopen(&nextarg[1], FOPEN_READTEXT);
FILE *file = use_stdin ? stdin : curlx_fopen(&nextarg[1], FOPEN_READTEXT);
if(!file) {
errorf("Failed to open %s", &nextarg[1]);
err = PARAM_READ_ERROR;
@ -1263,7 +1263,7 @@ static ParameterError parse_header(struct OperationConfig *config,
err = PARAM_READ_ERROR;
curlx_dyn_free(&line);
if(!use_stdin)
fclose(file);
curlx_fclose(file);
}
}
else {
@ -1536,7 +1536,7 @@ static ParameterError parse_writeout(struct OperationConfig *config,
}
else {
fname = nextarg;
file = fopen(fname, FOPEN_READTEXT);
file = curlx_fopen(fname, FOPEN_READTEXT);
if(!file) {
errorf("Failed to open %s", fname);
return PARAM_READ_ERROR;
@ -1545,7 +1545,7 @@ static ParameterError parse_writeout(struct OperationConfig *config,
tool_safefree(config->writeout);
err = file2string(&config->writeout, file);
if(file && (file != stdin))
fclose(file);
curlx_fclose(file);
if(err)
return err;
if(!config->writeout)