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