-
Notifications
You must be signed in to change notification settings - Fork 97
/
flock.c
50 lines (38 loc) · 1.31 KB
/
flock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*---------------------------------------------------------------------------*\
NAME
flock.c - flock(2), emulated (more or less) in terms of
lockf(3)
BUGS
Doesn't support shared locks.
LICENSE
This source code is released under a BSD-style. See the LICENSE
file for details.
Copyright (c) 2011-2015 Brian M. Clapper, [email protected]
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
Includes
\*---------------------------------------------------------------------------*/
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif /* HAVE_SYS_FILE_H */
#include "config.h"
#include "flock.h"
/*---------------------------------------------------------------------------*\
Public Routines
\*---------------------------------------------------------------------------*/
int flock(int fd, int op)
{
int cmd = 0;
errno = 0;
if (op & LOCK_UN)
cmd = F_ULOCK;
else if (op & LOCK_SH) /* can't emulate shared lock with lockf() */
errno = EINVAL;
else if (op & LOCK_NB)
cmd = F_TLOCK;
else
cmd = F_LOCK;
return lockf(fd, cmd, 0);
}