Merge from debian.
[hcoop/debian/courier-authlib.git] / liblock / locklockf.c
1 /*
2 ** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 /* $Id: locklockf.c,v 1.4 2002/09/02 16:55:56 mrsam Exp $ */
7
8 #if HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 #include <sys/types.h>
12 #if HAVE_FCNTL_H
13 #include <fcntl.h>
14 #endif
15 #if HAVE_SYS_FCNTL_H
16 #include <sys/fcntl.h>
17 #endif
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #if HAVE_ERRNO_H
22 #include <errno.h>
23 #endif
24 #include "liblock.h"
25
26 int ll_lockfd(int fd, int ltype, LL_OFFSET_TYPE start, LL_OFFSET_TYPE len)
27 {
28 off_t p;
29
30 if (ltype & ll_whence_curpos)
31 p=lseek(fd, start, SEEK_CUR);
32 else if (ltype && ll_whence_end)
33 p=lseek(fd, start, SEEK_END);
34 else p=lseek(fd, start, SEEK_SET);
35
36 if (p < 0) return (-1);
37
38 if (lockf(fd, ltype & ll_unlock ? F_ULOCK:
39 ltype & ll_wait ? F_LOCK:F_TLOCK, len))
40 {
41 lseek(fd, p, SEEK_SET);
42 return (-1);
43 }
44 lseek(fd, SEEK_SET, p);
45 return (0);
46 }