+\f/*
+** LOCKFILE -- lock a file using flock or (shudder) lockf
+**
+** Parameters:
+** fd -- the file descriptor of the file.
+** filename -- the file name (for error messages).
+** type -- type of the lock. Bits can be:
+** LOCK_EX -- exclusive lock.
+** LOCK_NB -- non-blocking.
+**
+** Returns:
+** TRUE if the lock was acquired.
+** FALSE otherwise.
+*/
+
+bool
+lockfile(fd, filename, type)
+ int fd;
+ char *filename;
+ int type;
+{
+# ifdef LOCKF
+ int action;
+ struct flock lfd;
+
+ if (bitset(LOCK_EX, type)
+ lfd.l_type = F_WRLCK;
+ else
+ lfd.l_type = F_RDLCK;
+
+ if (bitset(LOCK_NB, type))
+ action = F_SETLK;
+ else
+ action = F_SETLKW;
+
+ lfd.l_whence = lfd.l_start = lfd.l_len = 0;
+
+ if (fcntl(fd, action, &lfd) >= 0)
+ return TRUE;
+
+ if (!bitset(LOCK_NB, type) || (errno != EACCES && errno != EAGAIN))
+ syserr("cannot lockf(%s)", filename);
+# else
+ if (flock(fd, type) >= 0)
+ return TRUE;
+
+ if (!bitset(LOCK_NB, type) || errno != EWOULDBLOCK)
+ syserr("cannot flock(%s)", filename);
+# endif
+ return FALSE;
+}