~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to main/reentrancy.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
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
   +----------------------------------------------------------------------+
 
17
 */
 
18
 
 
19
/* $Id: reentrancy.c,v 1.40.2.2 2005/07/01 08:00:44 hyanantha Exp $ */
 
20
 
 
21
#include <sys/types.h>
 
22
#include <string.h>
 
23
#include <errno.h>
 
24
#ifdef HAVE_DIRENT_H
 
25
#include <dirent.h>
 
26
#endif
 
27
 
 
28
#ifdef PHP_WIN32
 
29
#include "win32/readdir.h"
 
30
#endif
 
31
 
 
32
#include "php_reentrancy.h"
 
33
#include "ext/standard/php_rand.h"                   /* for PHP_RAND_MAX */
 
34
 
 
35
enum {
 
36
        LOCALTIME_R,
 
37
        CTIME_R,
 
38
        ASCTIME_R,
 
39
        GMTIME_R,
 
40
        READDIR_R,
 
41
        NUMBER_OF_LOCKS
 
42
};
 
43
 
 
44
#if defined(PHP_NEED_REENTRANCY)
 
45
 
 
46
#include <TSRM.h>
 
47
 
 
48
static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS];
 
49
 
 
50
#define local_lock(x) tsrm_mutex_lock(reentrant_locks[x])
 
51
#define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x])
 
52
 
 
53
#else
 
54
 
 
55
#define local_lock(x)
 
56
#define local_unlock(x)
 
57
 
 
58
#endif
 
59
 
 
60
#if defined(PHP_IRIX_TIME_R)
 
61
 
 
62
#define HAVE_CTIME_R 1
 
63
#define HAVE_ASCTIME_R 1
 
64
 
 
65
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
 
66
{
 
67
        if (ctime_r(clock, buf, 26) == buf)
 
68
                return (buf);
 
69
        return (NULL);
 
70
}
 
71
 
 
72
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
 
73
{
 
74
        if (asctime_r(tm, buf, 26) == buf)
 
75
                return (buf);
 
76
        return (NULL);
 
77
}
 
78
 
 
79
#endif
 
80
 
 
81
#if defined(PHP_HPUX_TIME_R)
 
82
 
 
83
#define HAVE_LOCALTIME_R 1
 
84
#define HAVE_CTIME_R 1
 
85
#define HAVE_ASCTIME_R 1
 
86
#define HAVE_GMTIME_R 1
 
87
 
 
88
PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
 
89
{
 
90
        if (localtime_r(timep, p_tm) == 0)
 
91
                return (p_tm);
 
92
        return (NULL);
 
93
}
 
94
 
 
95
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
 
96
{
 
97
        if (ctime_r(clock, buf, 26) != -1)
 
98
                return (buf);
 
99
        return (NULL);
 
100
}
 
101
 
 
102
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
 
103
{
 
104
        if (asctime_r(tm, buf, 26) != -1)
 
105
                return (buf);
 
106
        return (NULL);
 
107
}
 
108
 
 
109
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
 
110
{
 
111
        if (gmtime_r(timep, p_tm) == 0)
 
112
                return (p_tm);
 
113
        return (NULL);
 
114
}
 
115
 
 
116
#endif
 
117
 
 
118
#if defined(__BEOS__)
 
119
 
 
120
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
 
121
{
 
122
    /* Modified according to LibC definition */
 
123
        if (((struct tm*)gmtime_r(timep, p_tm)) == p_tm)
 
124
                return (p_tm);
 
125
        return (NULL);
 
126
}
 
127
 
 
128
#endif /* BEOS */
 
129
 
 
130
#if !defined(HAVE_POSIX_READDIR_R)
 
131
 
 
132
PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry, 
 
133
                struct dirent **result)
 
134
{
 
135
#if defined(HAVE_OLD_READDIR_R)
 
136
        int ret = 0;
 
137
        
 
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)
 
141
         */
 
142
        entry->d_name[0] = '\0';
 
143
        readdir_r(dirp, entry);
 
144
        
 
145
        if (entry->d_name[0] == '\0') {
 
146
                *result = NULL;
 
147
                ret = errno;
 
148
        } else {
 
149
                *result = entry;
 
150
        }
 
151
        return ret;
 
152
#else
 
153
        struct dirent *ptr;
 
154
        int ret = 0;
 
155
 
 
156
        local_lock(READDIR_R);
 
157
        
 
158
        errno = 0;
 
159
        
 
160
        ptr = readdir(dirp);
 
161
        
 
162
        if (!ptr && errno != 0)
 
163
                ret = errno;
 
164
 
 
165
        if (ptr)
 
166
                memcpy(entry, ptr, sizeof(*ptr));
 
167
 
 
168
        *result = ptr;
 
169
 
 
170
        local_unlock(READDIR_R);
 
171
 
 
172
        return ret;
 
173
#endif
 
174
}
 
175
 
 
176
#endif
 
177
 
 
178
#if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
 
179
 
 
180
PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
 
181
{
 
182
        struct tm *tmp;
 
183
        
 
184
        local_lock(LOCALTIME_R);
 
185
 
 
186
        tmp = localtime(timep);
 
187
        if (tmp) {
 
188
                memcpy(p_tm, tmp, sizeof(struct tm));
 
189
                tmp = p_tm;
 
190
        }
 
191
        
 
192
        local_unlock(LOCALTIME_R);
 
193
 
 
194
        return tmp;
 
195
}
 
196
 
 
197
#endif
 
198
 
 
199
#if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME)
 
200
 
 
201
PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
 
202
{
 
203
        char *tmp;
 
204
        
 
205
        local_lock(CTIME_R);
 
206
 
 
207
        tmp = ctime(clock);
 
208
        strcpy(buf, tmp);
 
209
 
 
210
        local_unlock(CTIME_R);
 
211
        
 
212
        return buf;
 
213
}
 
214
 
 
215
#endif
 
216
 
 
217
#if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME)
 
218
 
 
219
PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
 
220
{
 
221
        char *tmp;
 
222
        
 
223
        local_lock(ASCTIME_R);
 
224
 
 
225
        tmp = asctime(tm);
 
226
        strcpy(buf, tmp);
 
227
 
 
228
        local_unlock(ASCTIME_R);
 
229
 
 
230
        return buf;
 
231
}
 
232
 
 
233
#endif
 
234
 
 
235
#if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME)
 
236
 
 
237
PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
 
238
{
 
239
        struct tm *tmp;
 
240
        
 
241
        local_lock(GMTIME_R);
 
242
 
 
243
        tmp = gmtime(timep);
 
244
        if (tmp) {
 
245
                memcpy(p_tm, tmp, sizeof(struct tm));
 
246
                tmp = p_tm;
 
247
        }
 
248
        
 
249
        local_unlock(GMTIME_R);
 
250
 
 
251
        return tmp;
 
252
}
 
253
 
 
254
#endif
 
255
 
 
256
#if defined(PHP_NEED_REENTRANCY)
 
257
 
 
258
void reentrancy_startup(void)
 
259
{
 
260
        int i;
 
261
 
 
262
        for (i = 0; i < NUMBER_OF_LOCKS; i++) {
 
263
                reentrant_locks[i] = tsrm_mutex_alloc();
 
264
        }
 
265
}
 
266
 
 
267
void reentrancy_shutdown(void)
 
268
{
 
269
        int i;
 
270
 
 
271
        for (i = 0; i < NUMBER_OF_LOCKS; i++) {
 
272
                tsrm_mutex_free(reentrant_locks[i]);
 
273
        }
 
274
}
 
275
 
 
276
#endif
 
277
 
 
278
#ifndef HAVE_RAND_R
 
279
 
 
280
/*-
 
281
 * Copyright (c) 1990, 1993
 
282
 *      The Regents of the University of California.  All rights reserved.
 
283
 *
 
284
 * Redistribution and use in source and binary forms, with or without
 
285
 * modification, are permitted provided that the following conditions
 
286
 * are met:
 
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.
 
299
 *
 
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
 
310
 * SUCH DAMAGE.
 
311
 *
 
312
 * Posix rand_r function added May 1999 by Wes Peters <wes@softweyr.com>.
 
313
 */
 
314
 
 
315
#include <sys/types.h>
 
316
#include <stdlib.h>
 
317
 
 
318
static int
 
319
do_rand(unsigned long *ctx)
 
320
{
 
321
        return ((*ctx = *ctx * 1103515245 + 12345) % ((u_long)PHP_RAND_MAX + 1));
 
322
}
 
323
 
 
324
 
 
325
PHPAPI int
 
326
php_rand_r(unsigned int *ctx)
 
327
{
 
328
        u_long val = (u_long) *ctx;
 
329
        *ctx = do_rand(&val);
 
330
        return (int) *ctx;
 
331
}
 
332
 
 
333
#endif
 
334
 
 
335
 
 
336
#ifndef HAVE_STRTOK_R
 
337
 
 
338
/*
 
339
 * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
 
340
 *
 
341
 * strtok_r, from Berkeley strtok
 
342
 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
 
343
 *
 
344
 * Copyright (c) 1988, 1993
 
345
 *      The Regents of the University of California.  All rights reserved.
 
346
 *
 
347
 * Redistribution and use in source and binary forms, with or without
 
348
 * modification, are permitted provided that the following conditions
 
349
 * are met:
 
350
 *
 
351
 * 1. Redistributions of source code must retain the above copyright
 
352
 *    notices, this list of conditions and the following disclaimer.
 
353
 * 
 
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.
 
357
 * 
 
358
 * 3. All advertising materials mentioning features or use of this software
 
359
 *    must display the following acknowledgement:
 
360
 *
 
361
 *      This product includes software developed by Softweyr LLC, the
 
362
 *      University of California, Berkeley, and its contributors.
 
363
 *
 
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.
 
367
 *
 
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.
 
379
 */
 
380
 
 
381
#include <stddef.h>
 
382
 
 
383
PHPAPI char *
 
384
php_strtok_r(char *s, const char *delim, char **last)
 
385
{
 
386
    char *spanp;
 
387
    int c, sc;
 
388
    char *tok;
 
389
 
 
390
    if (s == NULL && (s = *last) == NULL)
 
391
    {
 
392
        return NULL;
 
393
    }
 
394
 
 
395
    /*
 
396
     * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
 
397
     */
 
398
cont:
 
399
    c = *s++;
 
400
    for (spanp = (char *)delim; (sc = *spanp++) != 0; )
 
401
    {
 
402
        if (c == sc)
 
403
        {
 
404
            goto cont;
 
405
        }
 
406
    }
 
407
 
 
408
    if (c == 0)         /* no non-delimiter characters */
 
409
    {
 
410
        *last = NULL;
 
411
        return NULL;
 
412
    }
 
413
    tok = s - 1;
 
414
 
 
415
    /*
 
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.
 
418
     */
 
419
    for (;;)
 
420
    {
 
421
        c = *s++;
 
422
        spanp = (char *)delim;
 
423
        do
 
424
        {
 
425
            if ((sc = *spanp++) == c)
 
426
            {
 
427
                if (c == 0)
 
428
                {
 
429
                    s = NULL;
 
430
                }
 
431
                else
 
432
                {
 
433
                    char *w = s - 1;
 
434
                    *w = '\0';
 
435
                }
 
436
                *last = s;
 
437
                return tok;
 
438
            }
 
439
        }
 
440
        while (sc != 0);
 
441
    }
 
442
    /* NOTREACHED */
 
443
}
 
444
 
 
445
#endif
 
446
 
 
447
/*
 
448
 * Local variables:
 
449
 * tab-width: 4
 
450
 * c-basic-offset: 4
 
451
 * End:
 
452
 * vim600: sw=4 ts=4 fdm=marker
 
453
 * vim<600: sw=4 ts=4
 
454
 */