add test for Struct.new(0).
[ruby-svn.git] / missing / flock.c
blobb02f8bf832cd1c3f41855ca9c2f664e24e9392d8
1 #include "ruby/config.h"
3 #if defined _WIN32
4 #elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
6 /* These are the flock() constants. Since this sytems doesn't have
7 flock(), the values of the constants are probably not available.
8 */
9 # ifndef LOCK_SH
10 # define LOCK_SH 1
11 # endif
12 # ifndef LOCK_EX
13 # define LOCK_EX 2
14 # endif
15 # ifndef LOCK_NB
16 # define LOCK_NB 4
17 # endif
18 # ifndef LOCK_UN
19 # define LOCK_UN 8
20 # endif
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <errno.h>
26 int
27 flock(int fd, int operation)
29 struct flock lock;
31 switch (operation & ~LOCK_NB) {
32 case LOCK_SH:
33 lock.l_type = F_RDLCK;
34 break;
35 case LOCK_EX:
36 lock.l_type = F_WRLCK;
37 break;
38 case LOCK_UN:
39 lock.l_type = F_UNLCK;
40 break;
41 default:
42 errno = EINVAL;
43 return -1;
45 lock.l_whence = SEEK_SET;
46 lock.l_start = lock.l_len = 0L;
48 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
51 #elif defined(HAVE_LOCKF)
53 #include <unistd.h>
54 #include <errno.h>
56 /* Emulate flock() with lockf() or fcntl(). This is just to increase
57 portability of scripts. The calls might not be completely
58 interchangeable. What's really needed is a good file
59 locking module.
62 # ifndef F_ULOCK
63 # define F_ULOCK 0 /* Unlock a previously locked region */
64 # endif
65 # ifndef F_LOCK
66 # define F_LOCK 1 /* Lock a region for exclusive use */
67 # endif
68 # ifndef F_TLOCK
69 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
70 # endif
71 # ifndef F_TEST
72 # define F_TEST 3 /* Test a region for other processes locks */
73 # endif
75 /* These are the flock() constants. Since this sytems doesn't have
76 flock(), the values of the constants are probably not available.
78 # ifndef LOCK_SH
79 # define LOCK_SH 1
80 # endif
81 # ifndef LOCK_EX
82 # define LOCK_EX 2
83 # endif
84 # ifndef LOCK_NB
85 # define LOCK_NB 4
86 # endif
87 # ifndef LOCK_UN
88 # define LOCK_UN 8
89 # endif
91 int
92 flock(int fd, int operation)
94 switch (operation) {
96 /* LOCK_SH - get a shared lock */
97 case LOCK_SH:
98 rb_notimplement();
99 return -1;
100 /* LOCK_EX - get an exclusive lock */
101 case LOCK_EX:
102 return lockf (fd, F_LOCK, 0);
104 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
105 case LOCK_SH|LOCK_NB:
106 rb_notimplement();
107 return -1;
108 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
109 case LOCK_EX|LOCK_NB:
110 return lockf (fd, F_TLOCK, 0);
112 /* LOCK_UN - unlock */
113 case LOCK_UN:
114 return lockf (fd, F_ULOCK, 0);
116 /* Default - can't decipher operation */
117 default:
118 errno = EINVAL;
119 return -1;
122 #else
124 flock(int fd, int operation)
126 rb_notimplement();
127 return -1;
129 #endif