+#ifdef FLOCKFILE
+ /*
+ * Attempt to lock the file. We use an sharable lock if reading
+ * the file, and an exclusive lock if editting a file.
+ * The lock will be released when the file is no longer being
+ * referenced. At any time, the editor can have as many as
+ * three files locked, and with different lock statuses.
+ */
+ /*
+ * if this is either the saved or alternate file or current file,
+ * point to the appropriate descriptor and file lock status.
+ */
+ if (strcmp (file,savedfile) == 0) {
+ if (!io_savedfile) io_savedfile = dup(io) ;
+ lp = &lock_savedfile ; iop = &io_savedfile ;
+ } else if (strcmp (file,altfile) == 0) {
+ if (!io_altfile) io_altfile = dup(io) ;
+ lp = &lock_altfile ; iop = &io_altfile ;
+ } else {
+ /* throw away current lock, accquire new current lock */
+ if (io_curr) close (io_curr) ;
+ io_curr = dup(io) ;
+ lp = &lock_curr ; iop = &io_curr ;
+ lock_curr = 0 ;
+ }
+ if (c == 'r' || value(READONLY) || *lp == 0) {
+ /* if we have a lock already, don't bother */
+ if (!*lp) {
+ /* try for a shared lock */
+ if (flock(*iop, LOCK_SH|LOCK_NB) < 0
+ && errno == EWOULDBLOCK) {
+ ex_printf (
+ " [FILE BEING MODIFIED BY ANOTHER PROCESS]") ;
+ flush();
+ goto fail_lock ;
+ } else *lp = LOCK_SH ;
+ }
+ }
+ if ( c != 'r' && !value(READONLY) && *lp != LOCK_EX) {
+ /* if we are editting the file, upgrade to an exclusive lock. */
+ if (flock(*iop, LOCK_EX|LOCK_NB) < 0 && errno == EWOULDBLOCK) {
+ ex_printf (" [File open by another process]") ;
+ flush();
+ } else *lp = LOCK_EX ;
+ }
+fail_lock:
+#endif FLOCKFILE