From 152fc6633e0889ea9db40df2d2241022e7af534c Mon Sep 17 00:00:00 2001 From: ShreyasMahajann Date: Sat, 28 Mar 2026 00:35:26 +0530 Subject: [PATCH] options: add -q quiet flag to CLI getopt Signed-off-by: Shreyas Mahajan Reviewed-by: Jakub Jelen --- examples/ssh_client.c | 1 + src/options.c | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/ssh_client.c b/examples/ssh_client.c index 6f568c01..b1e11b12 100644 --- a/examples/ssh_client.c +++ b/examples/ssh_client.c @@ -178,6 +178,7 @@ usage(void) " -p port : connect to port\n" " -o option : set configuration option (e.g., -o Compression=yes)\n" " -r : use RSA to verify host public key\n" + " -q : quiet mode\n" " -F file : parse configuration file instead of default one\n" " -E file : append debug logs to a log file instead of stderr\n" #ifdef WITH_PCAP diff --git a/src/options.c b/src/options.c index 9dc19ec6..ed466118 100644 --- a/src/options.c +++ b/src/options.c @@ -1847,21 +1847,23 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) char **tmp = NULL; size_t i = 0; int argc = *argcptr; - int debuglevel = 0; int compress = 0; size_t current = 0; int opt_rc = 0; int saveoptind = optind; /* need to save 'em */ int saveopterr = opterr; int opt; + int rv; - /* Nothing to do here */ + /* Keep preconfigured global log level and let -v flags raise it further. */ + int verbosity = ssh_get_log_level(); + int verbosity_set = 0; if (argc <= 1) { return SSH_OK; } opterr = 0; /* shut up getopt */ - while ((opt = getopt(argc, argv, "c:i:o:Cl:p:vb:r12")) != -1) { + while ((opt = getopt(argc, argv, "c:i:o:Cl:p:qvb:r12")) != -1) { switch(opt) { case 'l': user = optarg; @@ -1870,8 +1872,12 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) port = optarg; break; case 'v': - debuglevel++; - ssh_set_log_level(debuglevel); + verbosity++; + verbosity_set = 1; + break; + case 'q': + verbosity = SSH_LOG_NOLOG; + verbosity_set = 1; break; case 'r': break; @@ -1958,6 +1964,14 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) *argcptr = current + 1; SAFE_FREE(save); + if (verbosity_set) { + verbosity = MIN(verbosity, SSH_LOG_FUNCTIONS); + rv = ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); + if (rv < 0) { + return SSH_ERROR; + } + } + if (compress) { if (ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "yes") < 0) { return SSH_ERROR;