curl: add long option '--out-null'

Add a new commandline option --out-null that discards all
response bytes into the void. Replaces non-portable use of
'-o /dev/null' with more efficiency.

Feature added in 8.16.0

Closes #17800
This commit is contained in:
Stefan Eissing 2025-07-02 09:43:13 +02:00 committed by Daniel Stenberg
parent 0e49234128
commit 6845533e24
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
13 changed files with 137 additions and 6 deletions

View file

@ -141,6 +141,9 @@ size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
intptr_t fhnd;
#endif
if(outs->out_null)
return bytes;
#ifdef DEBUGBUILD
{
char *tty = curl_getenv("CURL_ISATTY");

View file

@ -224,6 +224,7 @@ static const struct LongShort aliases[]= {
{"ntlm", ARG_BOOL, ' ', C_NTLM},
{"ntlm-wb", ARG_BOOL|ARG_DEPR, ' ', C_NTLM_WB},
{"oauth2-bearer", ARG_STRG|ARG_CLEAR, ' ', C_OAUTH2_BEARER},
{"out-null", ARG_BOOL, ' ', C_OUT_NULL},
{"output", ARG_FILE, 'o', C_OUTPUT},
{"output-dir", ARG_STRG, ' ', C_OUTPUT_DIR},
{"parallel", ARG_BOOL, 'Z', C_PARALLEL},
@ -1310,9 +1311,13 @@ static ParameterError parse_output(struct OperationConfig *config,
return PARAM_NO_MEM;
/* fill in the outfile */
err = getstr(&url->outfile, nextarg, DENY_BLANK);
if(nextarg)
err = getstr(&url->outfile, nextarg, DENY_BLANK);
else
url->outfile = NULL;
url->useremote = FALSE; /* switch off */
url->outset = TRUE;
url->out_null = !nextarg;
return err;
}
@ -1351,6 +1356,7 @@ static ParameterError parse_remote_name(struct OperationConfig *config,
url->outfile = NULL; /* leave it */
url->useremote = toggle;
url->outset = TRUE;
url->out_null = FALSE;
return PARAM_OK;
}
@ -1833,6 +1839,8 @@ static ParameterError opt_bool(struct OperationConfig *config,
return PARAM_LIBCURL_DOESNT_SUPPORT;
togglebit(toggle, &config->authtype, CURLAUTH_NTLM);
break;
case C_OUT_NULL: /* --out-null */
return parse_output(config, NULL);
case C_BASIC: /* --basic */
togglebit(toggle, &config->authtype, CURLAUTH_BASIC);
break;

View file

@ -167,6 +167,7 @@ typedef enum {
C_NTLM,
C_NTLM_WB,
C_OAUTH2_BEARER,
C_OUT_NULL,
C_OUTPUT,
C_OUTPUT_DIR,
C_PARALLEL,

View file

@ -442,6 +442,9 @@ const struct helptxt helptext[] = {
"OAuth 2 Bearer Token",
CURLHELP_AUTH | CURLHELP_IMAP | CURLHELP_POP3 | CURLHELP_SMTP |
CURLHELP_LDAP},
{" --out-null",
"Discard response data into the void",
CURLHELP_OUTPUT},
{"-o, --output <file>",
"Write to file instead of stdout",
CURLHELP_IMPORTANT | CURLHELP_OUTPUT},

View file

@ -1321,7 +1321,8 @@ static CURLcode single_transfer(struct OperationConfig *config,
}
}
if((urlnode->useremote ||
outs->out_null = urlnode->out_null;
if(!outs->out_null && (urlnode->useremote ||
(per->outfile && strcmp("-", per->outfile)))) {
result = setup_outfile(config, per, outs, skipped);
if(result)

View file

@ -74,6 +74,7 @@ struct OutStruct {
BIT(is_cd_filename);
BIT(s_isreg);
BIT(fopened);
BIT(out_null);
};
/*
@ -95,6 +96,7 @@ struct getout {
BIT(useremote); /* use remote filename locally */
BIT(noupload); /* if set, -T "" has been used */
BIT(noglob); /* disable globbing for this URL */
BIT(out_null); /* discard output for this URL */
};
/*
* 'trace' enumeration represents curl's output look'n feel possibilities.