$OpenBSD: patch-b_c,v 1.2 2005/05/24 20:27:49 naddy Exp $
--- b.c.orig	Sat May 21 21:49:04 2005
+++ b.c	Sat May 21 21:52:29 2005
@@ -196,6 +196,7 @@ static B *bmkchn(H *chn, B *prop, long a
 	else
 		b->o = pdefault;
 	mset(b->marks, 0, sizeof(b->marks));
+	b->filehandle = -1;
 	b->rdonly = 0;
 	b->orphan = 0;
 	b->oldcur = NULL;
@@ -255,6 +256,8 @@ extern B *errbuf;
 void brm(B *b)
 {
 	if (b && !--b->count) {
+		if (b->filehandle != -1)
+			close (b->filehandle);
 		if (b->changed)
 			abrerr(b->name);
 		if (b->locked && !b->ignored_lock && plain_file(b))
@@ -2119,7 +2122,7 @@ B *bload(unsigned char *s)
 	B *b;
 	long skip, amnt;
 	unsigned char *n;
-	int nowrite = 0;
+	int nowrite = 0, fh = -1;
 	P *p;
 	int x;
 	long mod_time = 0;
@@ -2172,6 +2175,12 @@ B *bload(unsigned char *s)
 		setopt(b,n);
 		b->rdonly = b->o.readonly;
 		goto opnerr;
+	}
+
+	/* Lock the file &&& ob,petef */
+	if (fi && !nowrite) {
+		fh = dup(fileno(fi));
+		nowrite = (flock (fh, LOCK_EX | LOCK_NB));
 	}
 
 	/* Skip data if we need to */
