From 753a1ada03bffcb3056b60b7a36587ac72379cbb Mon Sep 17 00:00:00 2001 From: deraadt Date: Fri, 8 Dec 2017 17:04:14 +0000 Subject: [PATCH] Convert snprintf+write into dprintf. It is simply easier to read, and provides retry on short-write file descriptors. ok florian, previous versions seen by millert --- bin/pax/pax.c | 11 +++-------- sbin/dump/tape.c | 7 +++---- usr.bin/awk/lib.c | 26 +++++++++----------------- usr.bin/chpass/chpass.c | 18 +++--------------- usr.bin/find/misc.c | 12 +++--------- usr.bin/login/login.c | 7 ++----- usr.sbin/rtadvd/advcap.c | 10 +++++----- usr.sbin/tcpdump/tcpdump.c | 14 +++++--------- 8 files changed, 33 insertions(+), 72 deletions(-) diff --git a/bin/pax/pax.c b/bin/pax/pax.c index ca46ba50ad6..91045afbfe5 100644 --- a/bin/pax/pax.c +++ b/bin/pax/pax.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pax.c,v 1.50 2017/03/11 12:55:47 tb Exp $ */ +/* $OpenBSD: pax.c,v 1.51 2017/12/08 17:04:14 deraadt Exp $ */ /* $NetBSD: pax.c,v 1.5 1996/03/26 23:54:20 mrg Exp $ */ /*- @@ -311,8 +311,6 @@ main(int argc, char **argv) void sig_cleanup(int which_sig) { - char errbuf[80]; - /* * restore modes and times for any dirs we may have created * or any dirs we may have read. @@ -320,12 +318,9 @@ sig_cleanup(int which_sig) /* paxwarn() uses stdio; fake it as well as we can */ if (which_sig == SIGXCPU) - strlcpy(errbuf, "\nCPU time limit reached, cleaning up.\n", - sizeof errbuf); + dprintf(STDERR_FILENO, "\nCPU time limit reached, cleaning up.\n"); else - strlcpy(errbuf, "\nSignal caught, cleaning up.\n", - sizeof errbuf); - (void) write(STDERR_FILENO, errbuf, strlen(errbuf)); + dprintf(STDERR_FILENO, "\nSignal caught, cleaning up.\n"); ar_close(1); sltab_process(1); diff --git a/sbin/dump/tape.c b/sbin/dump/tape.c index dc0eaf1d3bb..445a75df555 100644 --- a/sbin/dump/tape.c +++ b/sbin/dump/tape.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tape.c,v 1.43 2015/08/20 22:02:20 deraadt Exp $ */ +/* $OpenBSD: tape.c,v 1.44 2017/12/08 17:04:15 deraadt Exp $ */ /* $NetBSD: tape.c,v 1.11 1997/06/05 11:13:26 lukem Exp $ */ /*- @@ -258,7 +258,6 @@ void statussig(int signo) { time_t tnow, deltat; - char msgbuf[128]; int save_errno = errno; if (blockswritten < 500) @@ -266,12 +265,12 @@ statussig(int signo) (void) time(&tnow); deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing)) / blockswritten * tapesize; - (void)snprintf(msgbuf, sizeof(msgbuf), + /* XXX not safe due to floating point printf */ + dprintf(STDERR_FILENO, "dump: %s %3.2f%% done at %lld KB/s, finished in %d:%02d\n", tape, (blockswritten * 100.0) / tapesize, (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume), (int)(deltat / 3600), (int)((deltat % 3600) / 60)); - write(STDERR_FILENO, msgbuf, strlen(msgbuf)); errno = save_errno; } diff --git a/usr.bin/awk/lib.c b/usr.bin/awk/lib.c index 2a40a02f6f3..f1ff64731ca 100644 --- a/usr.bin/awk/lib.c +++ b/usr.bin/awk/lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lib.c,v 1.24 2017/10/09 14:51:31 deraadt Exp $ */ +/* $OpenBSD: lib.c,v 1.25 2017/12/08 17:04:15 deraadt Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -532,33 +532,25 @@ void SYNTAX(const char *fmt, ...) void fpecatch(int sig) { extern Node *curnode; - char buf[1024]; - snprintf(buf, sizeof buf, "floating point exception\n"); - write(STDERR_FILENO, buf, strlen(buf)); + dprintf(STDERR_FILENO, "floating point exception\n"); if (compile_time != 2 && NR && *NR > 0) { - snprintf(buf, sizeof buf, " input record number %d", (int) (*FNR)); - write(STDERR_FILENO, buf, strlen(buf)); - + dprintf(STDERR_FILENO, " input record number %d", (int) (*FNR)); if (strcmp(*FILENAME, "-") != 0) { - snprintf(buf, sizeof buf, ", file %s", *FILENAME); - write(STDERR_FILENO, buf, strlen(buf)); + dprintf(STDERR_FILENO, ", file %s", *FILENAME); } - write(STDERR_FILENO, "\n", 1); + dprintf(STDERR_FILENO, "\n"); } if (compile_time != 2 && curnode) { - snprintf(buf, sizeof buf, " source line number %d", curnode->lineno); - write(STDERR_FILENO, buf, strlen(buf)); + dprintf(STDERR_FILENO, " source line number %d", curnode->lineno); } else if (compile_time != 2 && lineno) { - snprintf(buf, sizeof buf, " source line number %d", lineno); - write(STDERR_FILENO, buf, strlen(buf)); + dprintf(STDERR_FILENO, " source line number %d", lineno); } if (compile_time == 1 && cursource() != NULL) { - snprintf(buf, sizeof buf, " source file %s", cursource()); - write(STDERR_FILENO, buf, strlen(buf)); + dprintf(STDERR_FILENO, " source file %s", cursource()); } - write(STDERR_FILENO, "\n", 1); + dprintf(STDERR_FILENO, "\n"); if (dbg > 1) /* core dump if serious debugging on */ abort(); _exit(1); diff --git a/usr.bin/chpass/chpass.c b/usr.bin/chpass/chpass.c index 4e47a82b441..1c1836c800a 100644 --- a/usr.bin/chpass/chpass.c +++ b/usr.bin/chpass/chpass.c @@ -1,4 +1,4 @@ -/* $OpenBSD: chpass.c,v 1.43 2015/11/26 19:01:47 deraadt Exp $ */ +/* $OpenBSD: chpass.c,v 1.44 2017/12/08 17:04:15 deraadt Exp $ */ /* $NetBSD: chpass.c,v 1.8 1996/05/15 21:50:43 jtc Exp $ */ /*- @@ -219,20 +219,8 @@ baduser(void) void kbintr(int signo) { - struct iovec iv[5]; - - iv[0].iov_base = "\n"; - iv[0].iov_len = 1; - iv[1].iov_base = __progname; - iv[1].iov_len = strlen(__progname); - iv[2].iov_base = ": "; - iv[2].iov_len = 2; - iv[3].iov_base = _PATH_MASTERPASSWD; - iv[3].iov_len = sizeof(_PATH_MASTERPASSWD) - 1; - iv[4].iov_base = " unchanged\n"; - iv[4].iov_len = 11; - writev(STDERR_FILENO, iv, 5); - + dprintf(STDERR_FILENO, "\n%s: %s unchanged\n", + __progname, _PATH_MASTERPASSWD); _exit(1); } diff --git a/usr.bin/find/misc.c b/usr.bin/find/misc.c index c4f358cadfd..69a4434bd4e 100644 --- a/usr.bin/find/misc.c +++ b/usr.bin/find/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.15 2015/07/14 17:18:48 millert Exp $ */ +/* $OpenBSD: misc.c,v 1.16 2017/12/08 17:04:15 deraadt Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -148,16 +148,10 @@ show_path(int signo) { int save_errno = errno; extern FTSENT *entry; - struct iovec iov[3]; if (entry != NULL) { - iov[0].iov_base = "find path: "; - iov[0].iov_len = strlen(iov[0].iov_base); - iov[1].iov_base = entry->fts_path; - iov[1].iov_len = entry->fts_pathlen; - iov[2].iov_base = "\n"; - iov[2].iov_len = strlen(iov[2].iov_base); - writev(STDERR_FILENO, iov, 3); + dprintf(STDERR_FILENO, "find path: %*s\n", + entry->fts_pathlen, entry->fts_path); errno = save_errno; } } diff --git a/usr.bin/login/login.c b/usr.bin/login/login.c index 9755972b443..edbb4d48c87 100644 --- a/usr.bin/login/login.c +++ b/usr.bin/login/login.c @@ -1,4 +1,4 @@ -/* $OpenBSD: login.c,v 1.68 2016/08/21 03:26:04 beck Exp $ */ +/* $OpenBSD: login.c,v 1.69 2017/12/08 17:04:15 deraadt Exp $ */ /* $NetBSD: login.c,v 1.13 1996/05/15 23:50:16 jtc Exp $ */ /*- @@ -825,11 +825,8 @@ sigint(int signo) void timedout(int signo) { - char warn[1024]; - - snprintf(warn, sizeof warn, + dprintf(STDERR_FILENO, "Login timed out after %d seconds\n", timeout); - write(STDERR_FILENO, warn, strlen(warn)); if (username) badlogin(username); _exit(0); diff --git a/usr.sbin/rtadvd/advcap.c b/usr.sbin/rtadvd/advcap.c index 9b2cf3a19ec..ace81f4fa7c 100644 --- a/usr.sbin/rtadvd/advcap.c +++ b/usr.sbin/rtadvd/advcap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: advcap.c,v 1.17 2015/10/25 22:36:17 jca Exp $ */ +/* $OpenBSD: advcap.c,v 1.18 2017/12/08 17:04:15 deraadt Exp $ */ /* $KAME: advcap.c,v 1.9 2002/05/29 14:28:35 itojun Exp $ */ /* @@ -151,7 +151,7 @@ getent(char *bp, char *name, char *cp) break; } if (cp >= bp + BUFSIZ) { - write(STDERR_FILENO, "Remcap entry too long\n", 23); + dprintf(STDERR_FILENO, "Remcap entry too long\n"); break; } else *cp++ = c; @@ -187,7 +187,7 @@ tnchktc(void) p = tbuf + strlen(tbuf) - 2; /* before the last colon */ while (*--p != ':') if (p < tbuf) { - write(STDERR_FILENO, "Bad remcap entry\n", 18); + dprintf(STDERR_FILENO, "Bad remcap entry\n"); return (0); } p++; @@ -200,7 +200,7 @@ tnchktc(void) q++; *q = 0; if (++hopcount > MAXHOP) { - write(STDERR_FILENO, "Infinite tc= loop\n", 18); + dprintf(STDERR_FILENO, "Infinite tc= loop\n"); return (0); } if (getent(tcbuf, tcname, remotefile) != 1) { @@ -212,7 +212,7 @@ tnchktc(void) ; l = p - holdtbuf + strlen(q); if (l > BUFSIZ) { - write(STDERR_FILENO, "Remcap entry too long\n", 23); + dprintf(STDERR_FILENO, "Remcap entry too long\n"); q[BUFSIZ - (p-holdtbuf)] = 0; } strlcpy(p, q, holdtbuf + BUFSIZ - p); diff --git a/usr.sbin/tcpdump/tcpdump.c b/usr.sbin/tcpdump/tcpdump.c index d90fd45d7b8..378d1574edb 100644 --- a/usr.sbin/tcpdump/tcpdump.c +++ b/usr.sbin/tcpdump/tcpdump.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tcpdump.c,v 1.80 2017/09/08 19:10:57 brynet Exp $ */ +/* $OpenBSD: tcpdump.c,v 1.81 2017/12/08 17:04:15 deraadt Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 @@ -513,25 +513,21 @@ cleanup(int signo) { struct pcap_stat stat; sigset_t allsigs; - char buf[1024]; sigfillset(&allsigs); sigprocmask(SIG_BLOCK, &allsigs, NULL); /* Can't print the summary if reading from a savefile */ - (void)write(STDERR_FILENO, "\n", 1); + dprintf(STDERR_FILENO, "\n"); if (pd != NULL && pcap_file(pd) == NULL) { if (priv_pcap_stats(&stat) < 0) { - (void)snprintf(buf, sizeof buf, + dprintf(STDERR_FILENO, "pcap_stats: %s\n", pcap_geterr(pd)); - write(STDERR_FILENO, buf, strlen(buf)); } else { - (void)snprintf(buf, sizeof buf, + dprintf(STDERR_FILENO, "%u packets received by filter\n", stat.ps_recv); - write(STDERR_FILENO, buf, strlen(buf)); - (void)snprintf(buf, sizeof buf, + dprintf(STDERR_FILENO, "%u packets dropped by kernel\n", stat.ps_drop); - write(STDERR_FILENO, buf, strlen(buf)); } } _exit(0); -- 2.20.1