]> arthur.barton.de Git - netatalk.git/blob - libatalk/compat/flock.c
1a7f644c9a3400f1ceed120bf96a35a0dfbeb5c1
[netatalk.git] / libatalk / compat / flock.c
1 /*
2  * $Id: flock.c,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1996 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 static int      _flock_dummy;
13
14 #ifndef HAVE_FLOCK
15
16 #include <sys/types.h>
17 #ifdef HAVE_FCNTL_H
18 #include <fcntl.h>
19 #endif /* HAVE_FCNTL_H */
20 #include <errno.h>
21
22 #define LOCK_SH         1
23 #define LOCK_EX         2
24 #define LOCK_NB         4
25 #define LOCK_UN         8
26
27 int flock( fd, operation )
28     int         fd;
29     int         operation;
30 {
31     struct flock        l;
32     int                 rc, op;
33
34     if ( operation & LOCK_NB ) {
35         op = F_SETLK;
36     } else {
37         op = F_SETLKW;
38     }
39
40     if ( operation & LOCK_EX ) {
41         l.l_type = F_WRLCK;
42     }
43
44     if ( operation & LOCK_SH ) {
45         l.l_type = F_RDLCK;
46     }
47
48     if ( operation & LOCK_UN ) {
49         l.l_type = F_UNLCK;
50     }
51
52     l.l_whence = 0;
53     l.l_start = 0;
54     l.l_len = 0;
55
56     if (( rc = fcntl( fd, F_SETLK, &l )) < 0 ) {
57         if ( errno == EAGAIN || errno == EACCES ) {
58             errno = EWOULDBLOCK;
59         }
60     }
61     return( rc );
62 }
63 #endif /* !HAVE_FLOCK */