1/*
2 * $Id: lock.c,v 1.1.1.1 2008/10/15 03:30:50 james26_jang Exp $
3 *
4 * Copyright (C) 1997 Lars Fenneberg
5 *
6 * See the file COPYRIGHT for the respective terms and conditions.
7 * If the file is missing contact me at lf@elemental.net
8 * and I'll send you a copy.
9 *
10 */
11
12#include "config.h"
13#include "includes.h"
14
15#if defined(HAVE_FLOCK)
16
17int do_lock_exclusive(int fd)
18{
19	return flock(fd, LOCK_EX|LOCK_NB);
20}
21
22int do_unlock(int fd)
23{
24	return flock(fd, LOCK_UN);
25}
26
27#elif defined(HAVE_FCNTL)
28
29int do_lock_exclusive(int fd)
30{
31	flock_t fl;
32	int res;
33
34	memset((void *)&fl, 0, sizeof(fl));
35
36	fl.l_type = F_WRLCK;
37	fl.l_whence = fl.l_start = 0;
38	fl.l_len = 0; /* 0 means "to end of file" */
39
40	res = fcntl(fd, F_SETLK, &fl);
41
42	if ((res == -1) && (errno == EAGAIN))
43		errno = EWOULDBLOCK;
44
45	return res;
46}
47
48int do_unlock(int fd)
49{
50	flock_t fl;
51
52	memset((void *)&fl, 0, sizeof(fl));
53
54	fl.l_type = F_UNLCK;
55	fl.l_whence = fl.l_start = 0;
56	fl.l_len = 0; /* 0 means "to end of file" */
57
58	return fcntl(fd, F_SETLK, &fl);
59}
60
61#else
62YOU_LOOSE "need either flock(2) or fcntl(2)"
63#endif
64