BSD 4_4_Lite1 development
authorCSRG <csrg@ucbvax.Berkeley.EDU>
Tue, 12 Oct 1993 02:04:06 +0000 (18:04 -0800)
committerCSRG <csrg@ucbvax.Berkeley.EDU>
Tue, 12 Oct 1993 02:04:06 +0000 (18:04 -0800)
Work on file usr/src/contrib/nvi.1.14/PORT/clib/flock.c

Synthesized-from: CSRG/cd2/4.4BSD-Lite1

usr/src/contrib/nvi.1.14/PORT/clib/flock.c [new file with mode: 0644]

diff --git a/usr/src/contrib/nvi.1.14/PORT/clib/flock.c b/usr/src/contrib/nvi.1.14/PORT/clib/flock.c
new file mode 100644 (file)
index 0000000..0027373
--- /dev/null
@@ -0,0 +1,49 @@
+#include <sys/types.h>
+/*
+ * Include <sys/file.h>, not <fcntl.h>, the flock(2)
+ * #defines were found there on historical systems.
+ */
+#include <sys/file.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <compat.h>
+
+/*
+ * Use fcntl(2) locking to fake flock(2) locking.
+ *
+ * DO NOT USE fcntl(2) UNLESS YOU HAVE TO, THERE ARE SOME SYSTEMS
+ * (I.E. ULTRIX) WHERE LOCKS AREN'T RELEASED WHEN PROCESSES DIE.
+ */
+int
+flock(fd, operation)
+       int fd, operation;
+{
+       struct flock arg;
+
+       switch (operation & ~LOCK_NB) {
+       case LOCK_EX:
+               arg.l_type = F_WRLCK;
+               break;
+       case LOCK_SH:
+               arg.l_type = F_RDLCK;
+               break;
+       case LOCK_UN:
+               arg.l_type = F_UNLCK;
+               break;
+       default:
+               abort();
+       }
+
+       arg.l_start = arg.l_len = 0;
+       arg.l_pid = 0;
+       arg.l_whence = 0;               /* SEEK_SET */
+       
+       if (!fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &arg))
+               return (0);
+       if (errno == EACCES || errno == EAGAIN)
+               errno = EWOULDBLOCK;
+       return (-1);
+}