2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 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_0.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
+----------------------------------------------------------------------+
19
/* $Id: reentrancy.c,v 1.40.2.2 2005/07/01 08:00:44 hyanantha Exp $ */
21
#include <sys/types.h>
29
#include "win32/readdir.h"
32
#include "php_reentrancy.h"
33
#include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */
44
#if defined(PHP_NEED_REENTRANCY)
48
static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS];
50
#define local_lock(x) tsrm_mutex_lock(reentrant_locks[x])
51
#define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x])
56
#define local_unlock(x)
60
#if defined(PHP_IRIX_TIME_R)
62
#define HAVE_CTIME_R 1
63
#define HAVE_ASCTIME_R 1
65
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
67
if (ctime_r(clock, buf, 26) == buf)
72
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
74
if (asctime_r(tm, buf, 26) == buf)
81
#if defined(PHP_HPUX_TIME_R)
83
#define HAVE_LOCALTIME_R 1
84
#define HAVE_CTIME_R 1
85
#define HAVE_ASCTIME_R 1
86
#define HAVE_GMTIME_R 1
88
PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
90
if (localtime_r(timep, p_tm) == 0)
95
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
97
if (ctime_r(clock, buf, 26) != -1)
102
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
104
if (asctime_r(tm, buf, 26) != -1)
109
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
111
if (gmtime_r(timep, p_tm) == 0)
118
#if defined(__BEOS__)
120
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
122
/* Modified according to LibC definition */
123
if (((struct tm*)gmtime_r(timep, p_tm)) == p_tm)
130
#if !defined(HAVE_POSIX_READDIR_R)
132
PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry,
133
struct dirent **result)
135
#if defined(HAVE_OLD_READDIR_R)
138
/* We cannot rely on the return value of readdir_r
139
as it differs between various platforms
140
(HPUX returns 0 on success whereas Solaris returns non-zero)
142
entry->d_name[0] = '\0';
143
readdir_r(dirp, entry);
145
if (entry->d_name[0] == '\0') {
156
local_lock(READDIR_R);
162
if (!ptr && errno != 0)
166
memcpy(entry, ptr, sizeof(*ptr));
170
local_unlock(READDIR_R);
178
#if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
180
PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
184
local_lock(LOCALTIME_R);
186
tmp = localtime(timep);
188
memcpy(p_tm, tmp, sizeof(struct tm));
192
local_unlock(LOCALTIME_R);
199
#if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME)
201
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
210
local_unlock(CTIME_R);
217
#if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME)
219
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
223
local_lock(ASCTIME_R);
228
local_unlock(ASCTIME_R);
235
#if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME)
237
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
241
local_lock(GMTIME_R);
245
memcpy(p_tm, tmp, sizeof(struct tm));
249
local_unlock(GMTIME_R);
256
#if defined(PHP_NEED_REENTRANCY)
258
void reentrancy_startup(void)
262
for (i = 0; i < NUMBER_OF_LOCKS; i++) {
263
reentrant_locks[i] = tsrm_mutex_alloc();
267
void reentrancy_shutdown(void)
271
for (i = 0; i < NUMBER_OF_LOCKS; i++) {
272
tsrm_mutex_free(reentrant_locks[i]);
281
* Copyright (c) 1990, 1993
282
* The Regents of the University of California. All rights reserved.
284
* Redistribution and use in source and binary forms, with or without
285
* modification, are permitted provided that the following conditions
287
* 1. Redistributions of source code must retain the above copyright
288
* notice, this list of conditions and the following disclaimer.
289
* 2. Redistributions in binary form must reproduce the above copyright
290
* notice, this list of conditions and the following disclaimer in the
291
* documentation and/or other materials provided with the distribution.
292
* 3. All advertising materials mentioning features or use of this software
293
* must display the following acknowledgement:
294
* This product includes software developed by the University of
295
* California, Berkeley and its contributors.
296
* 4. Neither the name of the University nor the names of its contributors
297
* may be used to endorse or promote products derived from this software
298
* without specific prior written permission.
300
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
301
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
302
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
303
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
304
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
305
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
306
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312
* Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
315
#include <sys/types.h>
319
do_rand(unsigned long *ctx)
321
return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)PHP_RAND_MAX + 1));
326
php_rand_r(unsigned int *ctx)
328
u_long val = (u_long) *ctx;
329
*ctx = do_rand(&val);
336
#ifndef HAVE_STRTOK_R
339
* Copyright (c) 1998 Softweyr LLC. All rights reserved.
341
* strtok_r, from Berkeley strtok
342
* Oct 13, 1998 by Wes Peters <wes@softweyr.com>
344
* Copyright (c) 1988, 1993
345
* The Regents of the University of California. All rights reserved.
347
* Redistribution and use in source and binary forms, with or without
348
* modification, are permitted provided that the following conditions
351
* 1. Redistributions of source code must retain the above copyright
352
* notices, this list of conditions and the following disclaimer.
354
* 2. Redistributions in binary form must reproduce the above copyright
355
* notices, this list of conditions and the following disclaimer in the
356
* documentation and/or other materials provided with the distribution.
358
* 3. All advertising materials mentioning features or use of this software
359
* must display the following acknowledgement:
361
* This product includes software developed by Softweyr LLC, the
362
* University of California, Berkeley, and its contributors.
364
* 4. Neither the name of the University nor the names of its contributors
365
* may be used to endorse or promote products derived from this software
366
* without specific prior written permission.
368
* THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
369
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
370
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
371
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
372
* REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
373
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
374
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
375
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
376
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
377
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
378
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
384
php_strtok_r(char *s, const char *delim, char **last)
390
if (s == NULL && (s = *last) == NULL)
396
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
400
for (spanp = (char *)delim; (sc = *spanp++) != 0; )
408
if (c == 0) /* no non-delimiter characters */
416
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
417
* Note that delim must have one NUL; we stop if we see that, too.
422
spanp = (char *)delim;
425
if ((sc = *spanp++) == c)
452
* vim600: sw=4 ts=4 fdm=marker