From: deraadt Date: Tue, 19 Jul 2016 06:43:27 +0000 (+0000) Subject: Cleanup close(open idioms. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9208e1c3382e11818ef65126ebd67c3577123c98;p=openbsd Cleanup close(open idioms. ok krw --- diff --git a/usr.bin/mail/quit.c b/usr.bin/mail/quit.c index 29109ed148e..0c47b1de68f 100644 --- a/usr.bin/mail/quit.c +++ b/usr.bin/mail/quit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: quit.c,v 1.22 2015/11/11 01:12:10 deraadt Exp $ */ +/* $OpenBSD: quit.c,v 1.23 2016/07/19 06:43:27 deraadt Exp $ */ /* $NetBSD: quit.c,v 1.6 1996/12/28 07:11:07 tls Exp $ */ /* @@ -188,6 +188,8 @@ quit(void) mbox = expand("&"); mcount = c; if (value("append") == NULL) { + int fdx; + (void)snprintf(tempname, sizeof(tempname), "%s/mail.RmXXXXXXXXXX", tmpdir); if ((fd = mkstemp(tempname)) == -1 || @@ -220,7 +222,8 @@ quit(void) return(-1); } (void)Fclose(obuf); - (void)close(open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)); + if ((fdx = open(mbox, O_CREAT | O_TRUNC | O_WRONLY, 0600)) != -1) + close(fdx); if ((obuf = Fopen(mbox, "r+")) == NULL) { warn("%s", mbox); (void)Fclose(ibuf); diff --git a/usr.bin/mail/v7.local.c b/usr.bin/mail/v7.local.c index 91e9526763b..f9cf2ef7f13 100644 --- a/usr.bin/mail/v7.local.c +++ b/usr.bin/mail/v7.local.c @@ -1,4 +1,4 @@ -/* $OpenBSD: v7.local.c,v 1.16 2015/11/11 01:12:10 deraadt Exp $ */ +/* $OpenBSD: v7.local.c,v 1.17 2016/07/19 06:43:27 deraadt Exp $ */ /* $NetBSD: v7.local.c,v 1.8 1997/05/13 06:15:58 mikel Exp $ */ /* @@ -70,9 +70,13 @@ findmail(char *user, char *buf, int buflen) void demail(void) { + int fd; - if (value("keep") != NULL || rm(mailname) < 0) - (void)close(open(mailname, O_CREAT | O_TRUNC | O_WRONLY, 0600)); + if (value("keep") != NULL || rm(mailname) < 0) { + fd = open(mailname, O_CREAT | O_TRUNC | O_WRONLY, 0600); + if (fd != -1) + close(fd); + } } /* diff --git a/usr.bin/patch/inp.c b/usr.bin/patch/inp.c index 6606ede551f..3f6086108f4 100644 --- a/usr.bin/patch/inp.c +++ b/usr.bin/patch/inp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: inp.c,v 1.45 2015/11/11 01:12:10 deraadt Exp $ */ +/* $OpenBSD: inp.c,v 1.46 2016/07/19 06:43:27 deraadt Exp $ */ /* * patch - a program to apply diffs to original files @@ -148,6 +148,8 @@ plan_a(const char *filename) statfailed = stat(filename, &filestat); if (statfailed && ok_to_create_file) { + int fd; + if (verbose) say("(Creating file %s...)\n", filename); @@ -159,7 +161,9 @@ plan_a(const char *filename) if (check_only) return true; makedirs(filename, true); - close(open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666)); + if ((fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0666)) != -1) + close(fd); + statfailed = stat(filename, &filestat); } if (statfailed)