summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2002-02-22 00:18:38 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2002-02-22 00:18:38 +0000
commitd72c8ba8818ccba20efea7e933fda46c6f2537c1 (patch)
tree23b6bb7b814ba36514f68f4be35da80b2c7f7454 /usr.bin
parent9e5fcbe443f7fb01da2c2315f10462c5b0d20f13 (diff)
if file mode known, try to avoid a race.. i think. millert spotted a bug in my first draft
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/mg/fileio.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c
index 81ee0077331..66847e06086 100644
--- a/usr.bin/mg/fileio.c
+++ b/usr.bin/mg/fileio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fileio.c,v 1.23 2002/02/14 22:58:20 vincent Exp $ */
+/* $OpenBSD: fileio.c,v 1.24 2002/02/22 00:18:37 deraadt Exp $ */
/*
* POSIX fileio.c
@@ -45,16 +45,29 @@ ffwopen(fn, bp)
char *fn;
BUFFER *bp;
{
+ int fd;
+ mode_t mode = DEFFILEMODE;
- if ((ffp = fopen(fn, "w")) == NULL) {
+ if (bp && bp->b_fi.fi_mode)
+ mode = bp->b_fi.fi_mode & 07777;
+
+ fd = open(fn, O_RDWR | O_CREAT | O_TRUNC, mode);
+ if (fd == -1) {
+ ffp = NULL;
ewprintf("Cannot open file for writing : %s", strerror(errno));
return (FIOERR);
+ }
+
+ if ((ffp = fdopen(fd, "w")) == NULL) {
+ ewprintf("Cannot open file for writing : %s", strerror(errno));
+ close(fd);
+ return (FIOERR);
}
/*
* If we have file information, use it. We don't bother to check for
* errors, because there's no a lot we can do about it. Certainly
- * trying to change ownership will fail if we aren' root. That's
+ * trying to change ownership will fail if we aren't root. That's
* probably OK. If we don't have info, no need to get it, since any
* future writes will do the same thing.
*/