Skip to content

Commit

Permalink
Prefer fputc over fputs where possible
Browse files Browse the repository at this point in the history
fputc is meant for single characters, fputs is meant for strings.
  • Loading branch information
AreaZR committed Jul 19, 2023
1 parent 20b768f commit 1b07621
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 100 deletions.
2 changes: 1 addition & 1 deletion dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic,
}
for (i = 0; i < rdata_digest_len; i++)
fprintf(f, "%02x", rdata_digest[i]);
fprintf(f, "\n");
fputc('\n', f);
free(rdata_digest); /* from sshkey_fingerprint_raw() */
success = 1;
}
Expand Down
10 changes: 5 additions & 5 deletions kexdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
int kout, r;

#ifdef DEBUG_KEXDH
fprintf(stderr, "dh_pub= ");
fputs("dh_pub= ", stderr);
BN_print_fp(stderr, dh_pub);
fprintf(stderr, "\n");
fputc('\n', stderr);
debug("bits %d", BN_num_bits(dh_pub));
DHparams_print_fp(stderr, kex->dh);
fprintf(stderr, "\n");
fputc('\n', stderr);
#endif

if (!dh_pub_is_valid(kex->dh, dh_pub)) {
Expand Down Expand Up @@ -128,9 +128,9 @@ kex_dh_keypair(struct kex *kex)
goto out;
#ifdef DEBUG_KEXDH
DHparams_print_fp(stderr, kex->dh);
fprintf(stderr, "pub= ");
fputs("pub= ", stderr);
BN_print_fp(stderr, pub_key);
fprintf(stderr, "\n");
fputc('\n', stderr);
#endif
kex->client_pub = buf;
buf = NULL;
Expand Down
4 changes: 2 additions & 2 deletions kexgexc.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh)
debug("SSH2_MSG_KEX_DH_GEX_INIT sent");
#ifdef DEBUG_KEXDH
DHparams_print_fp(stderr, kex->dh);
fprintf(stderr, "pub= ");
fputs("pub= ", stderr);
BN_print_fp(stderr, pub_key);
fprintf(stderr, "\n");
fputc('\n', stderr);
#endif
debug("expecting SSH2_MSG_KEX_DH_GEX_REPLY");
ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_REPLY, &input_kex_dh_gex_reply);
Expand Down
2 changes: 1 addition & 1 deletion moduli.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries,
if (BN_print_fp(ofile, omodulus) < 1)
return (-1);

res = fprintf(ofile, "\n");
res = fputc('\n', ofile);
fflush(ofile);

return (res > 0 ? 0 : -1);
Expand Down
6 changes: 3 additions & 3 deletions mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ mux_client_forward(int fd, int cancel_flag, u_int ftype, struct Forward *fwd)
fwd->connect_host ? fwd->connect_host : "",
fwd->connect_port);
if (muxclient_command == SSHMUX_COMMAND_FORWARD)
fprintf(stdout, "%i\n", fwd->allocated_port);
printf("%i\n", fwd->allocated_port);
break;
case MUX_S_PERMISSION_DENIED:
if ((r = sshbuf_get_cstring(m, &e, NULL)) != 0)
Expand Down Expand Up @@ -2323,7 +2323,7 @@ muxclient(const char *path)
case SSHMUX_COMMAND_TERMINATE:
mux_client_request_terminate(sock);
if (options.log_level != SYSLOG_LEVEL_QUIET)
fprintf(stderr, "Exit request sent.\r\n");
fputs("Exit request sent.\r\n", stderr);
exit(0);
case SSHMUX_COMMAND_FORWARD:
if (mux_client_forwards(sock, 0) != 0)
Expand All @@ -2342,7 +2342,7 @@ muxclient(const char *path)
case SSHMUX_COMMAND_STOP:
mux_client_request_stop_listening(sock);
if (options.log_level != SYSLOG_LEVEL_QUIET)
fprintf(stderr, "Stop listening request sent.\r\n");
fputs("Stop listening request sent.\r\n", stderr);
exit(0);
case SSHMUX_COMMAND_CANCEL_FWD:
if (mux_client_forwards(sock, 1) != 0)
Expand Down
10 changes: 5 additions & 5 deletions packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ ssh_packet_send2_wrapped(struct ssh *ssh)
if (ssh_packet_log_type(type))
debug3("send packet: type %u", type);
#ifdef PACKET_DEBUG
fprintf(stderr, "plain: ");
fputs("plain: ", stderr);
sshbuf_dump(state->outgoing_packet, stderr);
#endif

Expand Down Expand Up @@ -1201,7 +1201,7 @@ ssh_packet_send2_wrapped(struct ssh *ssh)
goto out;
}
#ifdef PACKET_DEBUG
fprintf(stderr, "encrypted: ");
fputs("encrypted: ", stderr);
sshbuf_dump(state->output, stderr);
#endif
/* increment sequence number for outgoing packets */
Expand Down Expand Up @@ -1542,9 +1542,9 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
if (state->packlen < 1 + 4 ||
state->packlen > PACKET_MAX_SIZE) {
#ifdef PACKET_DEBUG
fprintf(stderr, "input: \n");
fputs("input: \n", stderr);
sshbuf_dump(state->input, stderr);
fprintf(stderr, "incoming_packet: \n");
fputs("incoming_packet: \n", stderr);
sshbuf_dump(state->incoming_packet, stderr);
#endif
logit("Bad packet length %u.", state->packlen);
Expand Down Expand Up @@ -1585,7 +1585,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
return 0; /* packet is incomplete */
#ifdef PACKET_DEBUG
fprintf(stderr, "read_poll enc/full: ");
fputs("read_poll enc/full: ", stderr);
sshbuf_dump(state->input, stderr);
#endif
/* EtM: check mac over encrypted input */
Expand Down
2 changes: 1 addition & 1 deletion regress/misc/sk-dummy/sk-dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ dump(const char *preamble, const void *sv, size_t l)
fprintf(stderr, "%04zu: ", i);
fprintf(stderr, "%02x", s[i]);
if (i % 16 == 15 || i == l - 1)
fprintf(stderr, "\n");
fputc('\n', stderr);
}
#endif
}
Expand Down
15 changes: 7 additions & 8 deletions scp.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ do_local_cmd(arglist *a)
fatal("do_local_cmd: no arguments");

if (verbose_mode) {
fprintf(stderr, "Executing:");
fputs("Executing:", stderr);
for (i = 0; i < a->num; i++)
fmprintf(stderr, " %s", a->list[i]);
fprintf(stderr, "\n");
fputc('\n', stderr);
}
if ((pid = fork()) == -1)
fatal("do_local_cmd: fork: %s", strerror(errno));
Expand Down Expand Up @@ -2105,10 +2105,10 @@ response(void)
void
usage(void)
{
(void) fprintf(stderr,
(void) fputs(
"usage: scp [-346ABCOpqRrsTv] [-c cipher] [-D sftp_server_path] [-F ssh_config]\n"
" [-i identity_file] [-J destination] [-l limit] [-o ssh_option]\n"
" [-P port] [-S program] [-X sftp_option] source ... target\n");
" [-P port] [-S program] [-X sftp_option] source ... target\n", stderr);
exit(1);
}

Expand All @@ -2120,20 +2120,19 @@ run_err(const char *fmt,...)

++errs;
if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
(void) fprintf(fp, "%c", 0x01);
(void) fprintf(fp, "scp: ");
(void) fprintf(fp, "%cscp: ", 0x01);
va_start(ap, fmt);
(void) vfprintf(fp, fmt, ap);
va_end(ap);
(void) fprintf(fp, "\n");
(void) fputc('\n', fp);
(void) fflush(fp);
}

if (!iamremote) {
va_start(ap, fmt);
vfmprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
fputc('\n', stderr);
}
}

Expand Down
14 changes: 7 additions & 7 deletions session.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ do_motd(void)
#endif
if (f) {
while (fgets(buf, sizeof(buf), f))
fputs(buf, stdout);
puts(buf);
fclose(f);
}
}
Expand Down Expand Up @@ -1186,7 +1186,7 @@ do_setup_env(struct ssh *ssh, Session *s, const char *shell)

if (debug_flag) {
/* dump the environment */
fprintf(stderr, "Environment:\n");
fputs("Environment:\n", stderr);
for (i = 0; env[i]; i++)
fprintf(stderr, " %.200s\n", env[i]);
}
Expand Down Expand Up @@ -1441,10 +1441,10 @@ static void
do_pwchange(Session *s)
{
fflush(NULL);
fprintf(stderr, "WARNING: Your password has expired.\n");
fputs( "WARNING: Your password has expired.\n", stderr);
if (s->ttyfd != -1) {
fprintf(stderr,
"You must change your password now and login again!\n");
fputs("You must change your password now and login again!\n",
stderr);
#ifdef WITH_SELINUX
setexeccon(NULL);
#endif
Expand All @@ -1456,8 +1456,8 @@ do_pwchange(Session *s)
#endif
perror("passwd");
} else {
fprintf(stderr,
"Password change required but no TTY available.\n");
fputs("Password change required but no TTY available.\n",
stderr);
}
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion sftp-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ sftp_server_main(int argc, char **argv, struct passwd *user_pw)
switch (ch) {
case 'Q':
if (strcasecmp(optarg, "requests") != 0) {
fprintf(stderr, "Invalid query type\n");
fputs("Invalid query type\n", stderr);
exit(1);
}
for (i = 0; handlers[i].handler != NULL; i++)
Expand Down
4 changes: 2 additions & 2 deletions sftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,7 @@ interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
}
history(hl, &hev, H_ENTER, line);
if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
fprintf(stderr, "Error: input line too long\n");
fputs("Error: input line too long\n", stderr);
continue;
}
#endif /* USE_LIBEDIT */
Expand Down Expand Up @@ -2621,7 +2621,7 @@ main(int argc, char **argv)
file2 = *(argv + 1);

if (!*host) {
fprintf(stderr, "Missing hostname\n");
fputs("Missing hostname\n", stderr);
usage();
}

Expand Down
40 changes: 19 additions & 21 deletions ssh-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ delete_all(int agent_fd, int qflag)
ssh_remove_all_identities(agent_fd, 1);

if (ret != 0)
fprintf(stderr, "Failed to remove all identities.\n");
fputs("Failed to remove all identities.\n", stderr);
else if (!qflag)
fprintf(stderr, "All identities removed.\n");
fputs("All identities removed.\n", stderr);

return ret;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ add_file(int agent_fd, const char *filename, int key_only, int qflag,
"Only %d signatures left.\n", left);
break;
}
fprintf(stderr, "Skipping update: ");
fputs("Skipping update: ", stderr);
if (left == minleft) {
fprintf(stderr,
"required signatures left (%d).\n", left);
Expand Down Expand Up @@ -373,8 +373,8 @@ add_file(int agent_fd, const char *filename, int key_only, int qflag,
"Lifetime set to %d seconds\n", lifetime);
}
if (confirm != 0) {
fprintf(stderr, "The user must confirm "
"each use of the key\n");
fputs("The user must confirm each use of the key\n",
stderr);
}
}
} else {
Expand Down Expand Up @@ -430,8 +430,8 @@ add_file(int agent_fd, const char *filename, int key_only, int qflag,
lifetime);
}
if (confirm != 0) {
fprintf(stderr, "The user must confirm each use "
"of the key\n");
fputs("The user must confirm each use of the key\n",
stderr);
}
}

Expand Down Expand Up @@ -538,12 +538,11 @@ list_identities(int agent_fd, int do_fp)
ssh_err(r));
continue;
}
fprintf(stdout, " %s", idlist->comments[i]);
printf(" %s", idlist->comments[i]);
left = sshkey_signatures_left(idlist->keys[i]);
if (left > 0)
fprintf(stdout,
" [signatures left %d]", left);
fprintf(stdout, "\n");
printf(" [signatures left %d]", left);
fputc('\n', stdout);
}
}
ssh_free_identitylist(idlist);
Expand All @@ -562,7 +561,7 @@ lock_agent(int agent_fd, int lock)
strlcpy(prompt, "Again: ", sizeof prompt);
p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
if (strcmp(p1, p2) != 0) {
fprintf(stderr, "Passwords do not match.\n");
fputs("Passwords do not match.\n", stderr);
passok = 0;
}
freezero(p2, strlen(p2));
Expand Down Expand Up @@ -620,8 +619,8 @@ load_resident_keys(int agent_fd, const char *skprovider, int qflag,
"Lifetime set to %d seconds\n", lifetime);
}
if (confirm != 0) {
fprintf(stderr, "The user must confirm "
"each use of the key\n");
fputs("The user must confirm each use of the key\n",
stderr);
}
}
free(fp);
Expand Down Expand Up @@ -769,7 +768,7 @@ parse_dest_constraint(const char *s, struct dest_constraint ***dcp,
static void
usage(void)
{
fprintf(stderr,
fputs(
"usage: ssh-add [-cDdKkLlqvXx] [-E fingerprint_hash] [-H hostkey_file]\n"
" [-h destination_constraint] [-S provider] [-t life]\n"
#ifdef WITH_XMSS
Expand All @@ -779,7 +778,7 @@ usage(void)
" ssh-add -s pkcs11\n"
" ssh-add -e pkcs11\n"
" ssh-add -T pubkey ...\n"
);
, stderr);
}

int
Expand Down Expand Up @@ -812,8 +811,8 @@ main(int argc, char **argv)
case 0:
break;
case SSH_ERR_AGENT_NOT_PRESENT:
fprintf(stderr, "Could not open a connection to your "
"authentication agent.\n");
fputs("Could not open a connection to your "
"authentication agent.\n", stderr);
exit(2);
default:
fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
Expand Down Expand Up @@ -895,9 +894,8 @@ main(int argc, char **argv)
pkcs11provider = optarg;
break;
case 't':
if ((lifetime = convtime(optarg)) == -1 ||
lifetime < 0 || (u_long)lifetime > UINT32_MAX) {
fprintf(stderr, "Invalid lifetime\n");
if ((lifetime = convtime(optarg)) < 0 || (u_long)lifetime > UINT32_MAX) {
fputs("Invalid lifetime\n", stderr);
ret = 1;
goto done;
}
Expand Down
6 changes: 3 additions & 3 deletions ssh-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1987,12 +1987,12 @@ check_parent_exists(void)
static void
usage(void)
{
fprintf(stderr,
fputs(
"usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
" [-O option] [-P allowed_providers] [-t life]\n"
" ssh-agent [-a bind_address] [-E fingerprint_hash] [-O option]\n"
" [-P allowed_providers] [-t life] command [arg ...]\n"
" ssh-agent [-c | -s] -k\n");
" ssh-agent [-c | -s] -k\n", stderr);
exit(1);
}

Expand Down Expand Up @@ -2079,7 +2079,7 @@ main(int ac, char **av)
break;
case 't':
if ((lifetime = convtime(optarg)) == -1) {
fprintf(stderr, "Invalid lifetime\n");
fputs("Invalid lifetime\n", stderr);
usage();
}
break;
Expand Down
Loading

0 comments on commit 1b07621

Please sign in to comment.