--- b.c.orig	Fri Dec  7 19:13:04 2001
+++ b.c	Fri Dec  7 19:13:10 2001
@@ -17,6 +17,9 @@
 #include <pwd.h>
 #endif
 #include <errno.h>
+#include <sys/file.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
@@ -187,6 +190,7 @@
 	else
 		b->o = pdefault;
 	mset(b->marks, 0, sizeof(b->marks));
+	b->filehandle = -1; /* initialize filehande &&& ob */
 	b->rdonly = 0;
 	b->orphan = 0;
 	b->oldcur = 0;
@@ -239,6 +243,10 @@
 void brm(B * b)
 {
 	if (b && !--b->count) {
+		if (b->filehandle != -1) {
+			/* close filehandle, free lock &&& ob */
+			close (b->filehandle);
+		}
 		if (b->changed)
 			abrerr(b->name);
 		if (b == errbuf)
@@ -1805,7 +1813,8 @@
 	B *b;
 	long skip, amnt;
 	char *n;
-	int nowrite = 0;
+	struct stat sb;
+	int nowrite = 0, fh = -1;
 
 	if (!s || !s[0]) {
 		error = -1;
@@ -1852,6 +1861,12 @@
 		goto opnerr;
 	}
 
+	/* Lock the file &&& ob,petef */
+	if (fi) {
+		fh = dup( fileno(fi) );
+		nowrite = (flock (fh, LOCK_EX | LOCK_NB));
+	}
+
 	/* Skip data if we need to */
 	if (skip && lseek(fileno(fi), skip, 0) < 0) {
 		int r;
@@ -1907,6 +1922,8 @@
 	vsrm(n);
 
 	b->er = error;
+	if (fh != -1)
+		b->filehandle = fh;
 	return b;
 }
 
