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