[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / missing / flock.c
blob0b76961762597e42d11803b5170f2fb1707d9c76
1 #include "ruby/internal/config.h"
2 #include "ruby/ruby.h"
4 #if defined _WIN32
5 #elif defined __wasi__
6 #include <errno.h>
8 int
9 flock(int fd, int operation)
11 errno = EINVAL;
12 return -1;
14 #elif defined HAVE_FCNTL && defined HAVE_FCNTL_H
16 /* These are the flock() constants. Since this systems doesn't have
17 flock(), the values of the constants are probably not available.
19 # ifndef LOCK_SH
20 # define LOCK_SH 1
21 # endif
22 # ifndef LOCK_EX
23 # define LOCK_EX 2
24 # endif
25 # ifndef LOCK_NB
26 # define LOCK_NB 4
27 # endif
28 # ifndef LOCK_UN
29 # define LOCK_UN 8
30 # endif
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
36 int
37 flock(int fd, int operation)
39 struct flock lock;
41 switch (operation & ~LOCK_NB) {
42 case LOCK_SH:
43 lock.l_type = F_RDLCK;
44 break;
45 case LOCK_EX:
46 lock.l_type = F_WRLCK;
47 break;
48 case LOCK_UN:
49 lock.l_type = F_UNLCK;
50 break;
51 default:
52 errno = EINVAL;
53 return -1;
55 lock.l_whence = SEEK_SET;
56 lock.l_start = lock.l_len = 0L;
58 return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
61 #elif defined(HAVE_LOCKF)
63 #include <unistd.h>
64 #include <errno.h>
66 /* Emulate flock() with lockf() or fcntl(). This is just to increase
67 portability of scripts. The calls might not be completely
68 interchangeable. What's really needed is a good file
69 locking module.
72 # ifndef F_ULOCK
73 # define F_ULOCK 0 /* Unlock a previously locked region */
74 # endif
75 # ifndef F_LOCK
76 # define F_LOCK 1 /* Lock a region for exclusive use */
77 # endif
78 # ifndef F_TLOCK
79 # define F_TLOCK 2 /* Test and lock a region for exclusive use */
80 # endif
81 # ifndef F_TEST
82 # define F_TEST 3 /* Test a region for other processes locks */
83 # endif
85 /* These are the flock() constants. Since this systems doesn't have
86 flock(), the values of the constants are probably not available.
88 # ifndef LOCK_SH
89 # define LOCK_SH 1
90 # endif
91 # ifndef LOCK_EX
92 # define LOCK_EX 2
93 # endif
94 # ifndef LOCK_NB
95 # define LOCK_NB 4
96 # endif
97 # ifndef LOCK_UN
98 # define LOCK_UN 8
99 # endif
102 flock(int fd, int operation)
104 switch (operation) {
106 /* LOCK_SH - get a shared lock */
107 case LOCK_SH:
108 rb_notimplement();
109 return -1;
110 /* LOCK_EX - get an exclusive lock */
111 case LOCK_EX:
112 return lockf (fd, F_LOCK, 0);
114 /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
115 case LOCK_SH|LOCK_NB:
116 rb_notimplement();
117 return -1;
118 /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
119 case LOCK_EX|LOCK_NB:
120 return lockf (fd, F_TLOCK, 0);
122 /* LOCK_UN - unlock */
123 case LOCK_UN:
124 return lockf (fd, F_ULOCK, 0);
126 /* Default - can't decipher operation */
127 default:
128 errno = EINVAL;
129 return -1;
132 #else
134 flock(int fd, int operation)
136 rb_notimplement();
137 return -1;
139 #endif