1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Sascha Schumann <sascha@schumann.cx> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #include "php.h"
22 #include <errno.h>
23 #include "ext/standard/flock_compat.h"
24
25 #if HAVE_STRUCT_FLOCK
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/file.h>
29 #endif
30
31 #ifdef PHP_WIN32
32 #include <io.h>
33 #include "config.w32.h"
34 #endif
35
36 #ifdef NETWARE
37 #include <netinet/in.h>
38 #endif
39
40 #ifndef HAVE_FLOCK
flock(int fd,int operation)41 PHPAPI int flock(int fd, int operation)
42 {
43 return php_flock(fd, operation);
44 }
45 #endif /* !defined(HAVE_FLOCK) */
46
php_flock(int fd,int operation)47 PHPAPI int php_flock(int fd, int operation)
48 #if HAVE_STRUCT_FLOCK /* {{{ */
49 {
50 struct flock flck;
51 int ret;
52
53 flck.l_start = flck.l_len = 0;
54 flck.l_whence = SEEK_SET;
55
56 if (operation & LOCK_SH)
57 flck.l_type = F_RDLCK;
58 else if (operation & LOCK_EX)
59 flck.l_type = F_WRLCK;
60 else if (operation & LOCK_UN)
61 flck.l_type = F_UNLCK;
62 else {
63 errno = EINVAL;
64 return -1;
65 }
66
67 ret = fcntl(fd, operation & LOCK_NB ? F_SETLK : F_SETLKW, &flck);
68
69 if (operation & LOCK_NB && ret == -1 &&
70 (errno == EACCES || errno == EAGAIN))
71 errno = EWOULDBLOCK;
72
73 if (ret != -1) ret = 0;
74
75 return ret;
76 }
77 /* }}} */
78 #elif defined(PHP_WIN32) /* {{{ */
79 /*
80 * Program: Unix compatibility routines
81 *
82 * Author: Mark Crispin
83 * Networks and Distributed Computing
84 * Computing & Communications
85 * University of Washington
86 * Administration Building, AG-44
87 * Seattle, WA 98195
88 * Internet: MRC@CAC.Washington.EDU
89 *
90 * Date: 14 September 1996
91 * Last Edited: 14 August 1997
92 *
93 * Copyright 1997 by the University of Washington
94 *
95 * Permission to use, copy, modify, and distribute this software and its
96 * documentation for any purpose and without fee is hereby granted, provided
97 * that the above copyright notice appears in all copies and that both the
98 * above copyright notice and this permission notice appear in supporting
99 * documentation, and that the name of the University of Washington not be
100 * used in advertising or publicity pertaining to distribution of the software
101 * without specific, written prior permission. This software is made available
102 * "as is", and
103 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
104 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
105 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
106 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
107 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
108 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
109 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
110 *
111 */
112 /* DEDICATION
113
114 * This file is dedicated to my dog, Unix, also known as Yun-chan and
115 * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast. Unix
116 * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
117 * a two-month bout with cirrhosis of the liver.
118 *
119 * He was a dear friend, and I miss him terribly.
120 *
121 * Lift a leg, Yunie. Luv ya forever!!!!
122 */
123 {
124 HANDLE hdl = (HANDLE) _get_osfhandle(fd);
125 DWORD low = 1, high = 0;
126 OVERLAPPED offset =
127 {0, 0, 0, 0, NULL};
128 if (hdl < 0)
129 return -1; /* error in file descriptor */
130 /* bug for bug compatible with Unix */
131 UnlockFileEx(hdl, 0, low, high, &offset);
132 switch (operation & ~LOCK_NB) { /* translate to LockFileEx() op */
133 case LOCK_EX: /* exclusive */
134 if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
135 ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
136 0, low, high, &offset))
137 return 0;
138 break;
139 case LOCK_SH: /* shared */
140 if (LockFileEx(hdl, ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
141 0, low, high, &offset))
142 return 0;
143 break;
144 case LOCK_UN: /* unlock */
145 return 0; /* always succeeds */
146 default: /* default */
147 break;
148 }
149 /* Under Win32 MT library, errno is not a variable but a function call,
150 * which cannot be assigned to.
151 */
152 #if !defined(PHP_WIN32)
153 errno = EINVAL; /* bad call */
154 #endif
155 return -1;
156 }
157 /* }}} */
158 #else
159 #warning no proper flock support for your site
160 {
161 errno = 0;
162 return 0;
163 }
164 #endif
165
166 #ifndef PHP_WIN32
167 #if !(HAVE_INET_ATON)
168 /* {{{ inet_aton
169 * Check whether "cp" is a valid ascii representation
170 * of an Internet address and convert to a binary address.
171 * Returns 1 if the address is valid, 0 if not.
172 * This replaces inet_addr, the return value from which
173 * cannot distinguish between failure and a local broadcast address.
174 */
inet_aton(const char * cp,struct in_addr * ap)175 int inet_aton(const char *cp, struct in_addr *ap)
176 {
177 int dots = 0;
178 register unsigned long acc = 0, addr = 0;
179
180 do {
181 register char cc = *cp;
182
183 switch (cc) {
184 case '0':
185 case '1':
186 case '2':
187 case '3':
188 case '4':
189 case '5':
190 case '6':
191 case '7':
192 case '8':
193 case '9':
194 acc = acc * 10 + (cc - '0');
195 break;
196
197 case '.':
198 if (++dots > 3) {
199 return 0;
200 }
201 /* Fall through */
202
203 case '\0':
204 if (acc > 255) {
205 return 0;
206 }
207 addr = addr << 8 | acc;
208 acc = 0;
209 break;
210
211 default:
212 return 0;
213 }
214 } while (*cp++) ;
215
216 /* Normalize the address */
217 if (dots < 3) {
218 addr <<= 8 * (3 - dots) ;
219 }
220
221 /* Store it if requested */
222 if (ap) {
223 ap->s_addr = htonl(addr);
224 }
225
226 return 1;
227 }
228 /* }}} */
229 #endif /* !HAVE_INET_ATON */
230 #endif
231
232 /*
233 * Local variables:
234 * tab-width: 4
235 * c-basic-offset: 4
236 * End:
237 * vim600: sw=4 ts=4 fdm=marker
238 * vim<600: sw=4 ts=4
239 */
240