Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 451 lines (359 sloc) 10.667 kb
a646916 Add/update copyright headers
Sascha Schumann authored
1 /*
2 +----------------------------------------------------------------------+
dbeb415 @andigutmans - A belated happy holidays and PHP 5
andigutmans authored
3 | PHP Version 5 |
a646916 Add/update copyright headers
Sascha Schumann authored
4 +----------------------------------------------------------------------+
e4ca0ed @felipensp - Year++
felipensp authored
5 | Copyright (c) 1997-2012 The PHP Group |
a646916 Add/update copyright headers
Sascha Schumann authored
6 +----------------------------------------------------------------------+
5bd9322 bump year and license version
foobar authored
7 | This source file is subject to version 3.01 of the PHP license, |
a646916 Add/update copyright headers
Sascha Schumann authored
8 | that is bundled with this package in the file LICENSE, and is |
f68c7ff updating license information in the headers.
James Cox authored
9 | available through the world-wide-web at the following url: |
5bd9322 bump year and license version
foobar authored
10 | http://www.php.net/license/3_01.txt |
a646916 Add/update copyright headers
Sascha Schumann authored
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 +----------------------------------------------------------------------+
90613d2 Maintain headers.
Sebastian Bergmann authored
15 | Author: Sascha Schumann <sascha@schumann.cx> |
a646916 Add/update copyright headers
Sascha Schumann authored
16 +----------------------------------------------------------------------+
17 */
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
18
8e3f23e ws fixes + missing $Id$ tags, headers added
foobar authored
19 /* $Id$ */
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
20
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
21 #include <sys/types.h>
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
22 #include <string.h>
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
23 #include <errno.h>
24 #ifdef HAVE_DIRENT_H
25 #include <dirent.h>
26 #endif
27
143b02d Fix for missing RAND_MAX on SunOS 4.1
Sascha Schumann authored
28 #include "php_reentrancy.h"
ece0a73 Fixed bug #25170 (Problem with generation of random numbers on solaris)
Ilia Alshanetsky authored
29 #include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */
143b02d Fix for missing RAND_MAX on SunOS 4.1
Sascha Schumann authored
30
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
31 enum {
32 LOCALTIME_R,
33 CTIME_R,
34 ASCTIME_R,
35 GMTIME_R,
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
36 READDIR_R,
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
37 NUMBER_OF_LOCKS
38 };
39
1f9a496 Define macros only, if one of the locking _r functions is used
Sascha Schumann authored
40 #if defined(PHP_NEED_REENTRANCY)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
41
42 #include <TSRM.h>
43
44 static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS];
45
46 #define local_lock(x) tsrm_mutex_lock(reentrant_locks[x])
47 #define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x])
48
49 #else
50
51 #define local_lock(x)
52 #define local_unlock(x)
53
54 #endif
55
ff5a9de Support for IRIX-style asctime_r/ctime_r.
Sascha Schumann authored
56 #if defined(PHP_IRIX_TIME_R)
57
58 #define HAVE_CTIME_R 1
59 #define HAVE_ASCTIME_R 1
60
61 PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
62 {
992d32c Fixed bug #55014 (Compile failure due to improper use of ctime_r()).
Ilia Alshanetsky authored
63 if (ctime_r(clock, buf) == buf)
ff5a9de Support for IRIX-style asctime_r/ctime_r.
Sascha Schumann authored
64 return (buf);
65 return (NULL);
66 }
67
68 PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
69 {
992d32c Fixed bug #55014 (Compile failure due to improper use of ctime_r()).
Ilia Alshanetsky authored
70 if (asctime_r(tm, buf) == buf)
ff5a9de Support for IRIX-style asctime_r/ctime_r.
Sascha Schumann authored
71 return (buf);
72 return (NULL);
73 }
74
75 #endif
76
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
77 #if defined(PHP_HPUX_TIME_R)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
78
ff40315 Avoid building two versions of the time-related reentrant functions.
Sascha Schumann authored
79 #define HAVE_LOCALTIME_R 1
80 #define HAVE_CTIME_R 1
81 #define HAVE_ASCTIME_R 1
82 #define HAVE_GMTIME_R 1
83
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
84 PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
85 {
86 if (localtime_r(timep, p_tm) == 0)
87 return (p_tm);
88 return (NULL);
89 }
90
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
91 PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
92 {
93 if (ctime_r(clock, buf, 26) != -1)
94 return (buf);
95 return (NULL);
96 }
97
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
98 PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
99 {
100 if (asctime_r(tm, buf, 26) != -1)
101 return (buf);
102 return (NULL);
103 }
104
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
105 PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
106 {
107 if (gmtime_r(timep, p_tm) == 0)
108 return (p_tm);
109 return (NULL);
110 }
111
112 #endif
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
113
464d8ca Work around a daft mistake in the beos headers...
David Reid authored
114 #if defined(__BEOS__)
115
116 PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
117 {
118 /* Modified according to LibC definition */
119 if (((struct tm*)gmtime_r(timep, p_tm)) == p_tm)
120 return (p_tm);
121 return (NULL);
122 }
123
124 #endif /* BEOS */
125
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
126 #if !defined(HAVE_POSIX_READDIR_R)
127
128 PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry,
129 struct dirent **result)
130 {
25df65b Support old-style readdir_r()
Sascha Schumann authored
131 #if defined(HAVE_OLD_READDIR_R)
453b4c7 Make old readdir_r code compatible with HPUX and Solaris.
Sascha Schumann authored
132 int ret = 0;
25df65b Support old-style readdir_r()
Sascha Schumann authored
133
453b4c7 Make old readdir_r code compatible with HPUX and Solaris.
Sascha Schumann authored
134 /* We cannot rely on the return value of readdir_r
135 as it differs between various platforms
136 (HPUX returns 0 on success whereas Solaris returns non-zero)
137 */
138 entry->d_name[0] = '\0';
139 readdir_r(dirp, entry);
140
141 if (entry->d_name[0] == '\0') {
25df65b Support old-style readdir_r()
Sascha Schumann authored
142 *result = NULL;
453b4c7 Make old readdir_r code compatible with HPUX and Solaris.
Sascha Schumann authored
143 ret = errno;
e5a997f @- Fixed a bug which made readdir() unusable in some systems. (Jani)
foobar authored
144 } else {
145 *result = entry;
faf4f76 If no further directory exists, php_readdir_r() returns success and sets
Sascha Schumann authored
146 }
453b4c7 Make old readdir_r code compatible with HPUX and Solaris.
Sascha Schumann authored
147 return ret;
25df65b Support old-style readdir_r()
Sascha Schumann authored
148 #else
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
149 struct dirent *ptr;
150 int ret = 0;
151
152 local_lock(READDIR_R);
153
154 errno = 0;
155
156 ptr = readdir(dirp);
3bfe29f Support HP-UX 10.x non-standard time-related reentrant functions
Sascha Schumann authored
157
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
158 if (!ptr && errno != 0)
159 ret = errno;
160
8a86e37 The behaviour for result == NULL || entry == NULL is undefined.
Sascha Schumann authored
161 if (ptr)
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
162 memcpy(entry, ptr, sizeof(*ptr));
163
8a86e37 The behaviour for result == NULL || entry == NULL is undefined.
Sascha Schumann authored
164 *result = ptr;
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
165
166 local_unlock(READDIR_R);
167
168 return ret;
25df65b Support old-style readdir_r()
Sascha Schumann authored
169 #endif
2aaa538 Use reentrant version of readdir. If the target platform does not suppor...
Sascha Schumann authored
170 }
171
172 #endif
173
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
174 #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
175
f2a236f Missed localtime_r/ctime_r. Prefix them with php
Sascha Schumann authored
176 PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
177 {
178 struct tm *tmp;
179
180 local_lock(LOCALTIME_R);
181
182 tmp = localtime(timep);
9d2738c @zsuraski - Fix crash bug in localtime_r. If locatime() returns NULL then also ret...
zsuraski authored
183 if (tmp) {
184 memcpy(p_tm, tmp, sizeof(struct tm));
185 tmp = p_tm;
186 }
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
187
188 local_unlock(LOCALTIME_R);
189
9d2738c @zsuraski - Fix crash bug in localtime_r. If locatime() returns NULL then also ret...
zsuraski authored
190 return tmp;
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
191 }
192
193 #endif
194
195 #if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME)
196
f2a236f Missed localtime_r/ctime_r. Prefix them with php
Sascha Schumann authored
197 PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
198 {
199 char *tmp;
200
201 local_lock(CTIME_R);
202
203 tmp = ctime(clock);
204 strcpy(buf, tmp);
205
206 local_unlock(CTIME_R);
207
208 return buf;
209 }
210
211 #endif
212
213 #if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME)
214
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
215 PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
216 {
217 char *tmp;
218
219 local_lock(ASCTIME_R);
220
221 tmp = asctime(tm);
222 strcpy(buf, tmp);
223
224 local_unlock(ASCTIME_R);
225
226 return buf;
227 }
228
229 #endif
230
231 #if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME)
232
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
233 PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
234 {
235 struct tm *tmp;
236
237 local_lock(GMTIME_R);
238
239 tmp = gmtime(timep);
a37a9db fix bug in php_gmtime_r
Daniel Beulshausen authored
240 if (tmp) {
241 memcpy(p_tm, tmp, sizeof(struct tm));
242 tmp = p_tm;
243 }
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
244
245 local_unlock(GMTIME_R);
246
a37a9db fix bug in php_gmtime_r
Daniel Beulshausen authored
247 return tmp;
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
248 }
249
250 #endif
251
524f0b1 - move strtok_r into reentrancy.c
Sascha Schumann authored
252 #if defined(PHP_NEED_REENTRANCY)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
253
ec18b99 Fix typo (shutdown -> startup)
Sascha Schumann authored
254 void reentrancy_startup(void)
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
255 {
256 int i;
257
258 for (i = 0; i < NUMBER_OF_LOCKS; i++) {
3d092da unposixfy calls to TSRM
Sascha Schumann authored
259 reentrant_locks[i] = tsrm_mutex_alloc();
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
260 }
261 }
262
263 void reentrancy_shutdown(void)
264 {
265 int i;
266
267 for (i = 0; i < NUMBER_OF_LOCKS; i++) {
3d092da unposixfy calls to TSRM
Sascha Schumann authored
268 tsrm_mutex_free(reentrant_locks[i]);
35b30a8 Add reentrant versions of ctime, localtime, gmtime, asctime.
Sascha Schumann authored
269 }
270 }
271
272 #endif
524f0b1 - move strtok_r into reentrancy.c
Sascha Schumann authored
273
274 #ifndef HAVE_RAND_R
275
276 /*-
277 * Copyright (c) 1990, 1993
278 * The Regents of the University of California. All rights reserved.
279 *
280 * Redistribution and use in source and binary forms, with or without
281 * modification, are permitted provided that the following conditions
282 * are met:
283 * 1. Redistributions of source code must retain the above copyright
284 * notice, this list of conditions and the following disclaimer.
285 * 2. Redistributions in binary form must reproduce the above copyright
286 * notice, this list of conditions and the following disclaimer in the
287 * documentation and/or other materials provided with the distribution.
288 * 3. All advertising materials mentioning features or use of this software
289 * must display the following acknowledgement:
290 * This product includes software developed by the University of
291 * California, Berkeley and its contributors.
292 * 4. Neither the name of the University nor the names of its contributors
293 * may be used to endorse or promote products derived from this software
294 * without specific prior written permission.
295 *
296 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
297 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
298 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
299 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
302 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
303 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306 * SUCH DAMAGE.
307 *
308 * Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
309 */
310
311 #include <sys/types.h>
312 #include <stdlib.h>
313
314 static int
315 do_rand(unsigned long *ctx)
316 {
ece0a73 Fixed bug #25170 (Problem with generation of random numbers on solaris)
Ilia Alshanetsky authored
317 return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)PHP_RAND_MAX + 1));
524f0b1 - move strtok_r into reentrancy.c
Sascha Schumann authored
318 }
319
320
cfc7e27 Add PHPAPI for Windows.
Sascha Schumann authored
321 PHPAPI int
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
322 php_rand_r(unsigned int *ctx)
524f0b1 - move strtok_r into reentrancy.c
Sascha Schumann authored
323 {
324 u_long val = (u_long) *ctx;
325 *ctx = do_rand(&val);
326 return (int) *ctx;
327 }
328
329 #endif
330
331
332 #ifndef HAVE_STRTOK_R
333
334 /*
335 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
336 *
337 * strtok_r, from Berkeley strtok
338 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
339 *
340 * Copyright (c) 1988, 1993
341 * The Regents of the University of California. All rights reserved.
342 *
343 * Redistribution and use in source and binary forms, with or without
344 * modification, are permitted provided that the following conditions
345 * are met:
346 *
347 * 1. Redistributions of source code must retain the above copyright
348 * notices, this list of conditions and the following disclaimer.
349 *
350 * 2. Redistributions in binary form must reproduce the above copyright
351 * notices, this list of conditions and the following disclaimer in the
352 * documentation and/or other materials provided with the distribution.
353 *
354 * 3. All advertising materials mentioning features or use of this software
355 * must display the following acknowledgement:
356 *
357 * This product includes software developed by Softweyr LLC, the
358 * University of California, Berkeley, and its contributors.
359 *
360 * 4. Neither the name of the University nor the names of its contributors
361 * may be used to endorse or promote products derived from this software
362 * without specific prior written permission.
363 *
364 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
365 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
366 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
367 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
368 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
369 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
370 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
371 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
372 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
373 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
374 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
375 */
376
377 #include <stddef.h>
378
cfc7e27 Add PHPAPI for Windows.
Sascha Schumann authored
379 PHPAPI char *
d0f3764 Change reentrancy API to always use the php prefix.
Sascha Schumann authored
380 php_strtok_r(char *s, const char *delim, char **last)
524f0b1 - move strtok_r into reentrancy.c
Sascha Schumann authored
381 {
382 char *spanp;
383 int c, sc;
384 char *tok;
385
386 if (s == NULL && (s = *last) == NULL)
387 {
388 return NULL;
389 }
390
391 /*
392 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
393 */
394 cont:
395 c = *s++;
396 for (spanp = (char *)delim; (sc = *spanp++) != 0; )
397 {
398 if (c == sc)
399 {
400 goto cont;
401 }
402 }
403
404 if (c == 0) /* no non-delimiter characters */
405 {
406 *last = NULL;
407 return NULL;
408 }
409 tok = s - 1;
410
411 /*
412 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
413 * Note that delim must have one NUL; we stop if we see that, too.
414 */
415 for (;;)
416 {
417 c = *s++;
418 spanp = (char *)delim;
419 do
420 {
421 if ((sc = *spanp++) == c)
422 {
423 if (c == 0)
424 {
425 s = NULL;
426 }
427 else
428 {
429 char *w = s - 1;
430 *w = '\0';
431 }
432 *last = s;
433 return tok;
434 }
435 }
436 while (sc != 0);
437 }
438 /* NOTREACHED */
439 }
440
441 #endif
25c3a3a @rlerdorf vim-6 does folding - clean up a bunch of missing folding tags plus
rlerdorf authored
442
443 /*
444 * Local variables:
445 * tab-width: 4
446 * c-basic-offset: 4
447 * End:
78747bd - Don't wrap lines... this is annoying while coding.
Derick Rethans authored
448 * vim600: sw=4 ts=4 fdm=marker
449 * vim<600: sw=4 ts=4
25c3a3a @rlerdorf vim-6 does folding - clean up a bunch of missing folding tags plus
rlerdorf authored
450 */
Something went wrong with that request. Please try again.