+/*
+ * Convert fopen() mode argument to open() bits
+ */
+
+static int
+str2accbits(const char *mode)
+{
+ if (strcmp (mode, "r") == 0)
+ return O_RDONLY;
+ else if (strcmp (mode, "r+") == 0)
+ return O_RDWR;
+ else if (strcmp (mode, "w") == 0)
+ return O_WRONLY | O_CREAT | O_TRUNC;
+ else if (strcmp (mode, "w+") == 0)
+ return O_RDWR | O_CREAT | O_TRUNC;
+ else if (strcmp (mode, "a") == 0)
+ return O_WRONLY | O_CREAT | O_APPEND;
+ else if (strcmp (mode, "a+") == 0)
+ return O_RDWR | O_CREAT | O_APPEND;
+ else {
+ errno = EINVAL;
+ return -1;
+ }
+}
+
+/*
+ * Internal routine to switch between different locking types.
+ */
+
+static int
+lkopen (const char *file, int access, mode_t mode, enum locktype ltype,
+ int *failed_to_lock)
+{
+ switch (ltype) {
+
+ case FCNTL_LOCKING:
+ return lkopen_fcntl(file, access, mode, failed_to_lock);
+
+ case DOT_LOCKING:
+ return lkopen_dot(file, access, mode, failed_to_lock);
+
+#ifdef HAVE_FLOCK
+ case FLOCK_LOCKING:
+ return lkopen_flock(file, access, mode, failed_to_lock);
+#endif /* HAVE_FLOCK */
+
+#ifdef HAVE_LOCKF
+ case LOCKF_LOCKING:
+ return lkopen_lockf(file, access, mode, failed_to_lock);
+#endif /* HAVE_FLOCK */
+
+ default:
+ adios(NULL, "Internal locking error: unsupported lock type used!");
+ }
+
+ return -1;
+}
+
+
+/*
+ * Routine to clean up the dot locking file
+ */
+
+static void
+lkclose_dot (int fd, const char *file)
+{
+ struct lockinfo lkinfo;
+