File: reentrant.c

package info (click to toggle)
aolserver 3.4.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 22,692 kB
  • ctags: 33,612
  • sloc: ansic: 171,340; tcl: 10,218; sh: 3,821; cpp: 2,779; makefile: 2,041; yacc: 1,648; perl: 456; php: 13
file content (425 lines) | stat: -rw-r--r-- 8,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * The contents of this file are subject to the AOLserver Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://aolserver.com/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is AOLserver Code and related documentation
 * distributed by AOL.
 * 
 * The Initial Developer of the Original Code is America Online,
 * Inc. Portions created by AOL are Copyright (C) 1999 America Online,
 * Inc. All Rights Reserved.
 *
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU General Public License (the "GPL"), in which case the
 * provisions of GPL are applicable instead of those above.  If you wish
 * to allow use of your version of this file only under the terms of the
 * GPL and not to allow others to use your version of this file under the
 * License, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the GPL.
 * If you do not delete the provisions above, a recipient may use your
 * version of this file under either the License or the GPL.
 */


/* 
 * reentrant.c --
 *
 *	Reentrant versions of common system utilities using per-thread
 *	data buffers.  See the corresponding manual page for details.
 */

static const char *RCSID = "@(#) $Header: /cvsroot/aolserver/aolserver/thread/reentrant.c,v 1.6.2.1 2001/04/03 20:23:25 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__;

#include "thread.h"

/*
 * The following structure maintains state for the
 * reentrant wrappers.
 */

typedef struct Tls {
    char	    	naBuf[16];
#ifndef WIN32
    char	       *stBuf;
    struct tm   	gtBuf;
    struct tm   	ltBuf;
    char		ctBuf[27];
    char		asBuf[27];
    struct {
	struct dirent ent;
	char name[PATH_MAX+1];
    } rdBuf;
#endif
} Tls;

static Tls *GetTls(void);

#ifdef MACOSX
#define NO_REENTRANT	1
char *strtok_r(char *s, const char *delim, char **last);
#endif

struct dirent *
ns_readdir(DIR * dir)
{
    struct dirent *ent;

#if defined(WIN32) || defined(__FreeBSD__)
    ent = readdir(dir);

#else
    Tls *tlsPtr = GetTls();

#if defined(NO_REENTRANT)
    static Ns_Mutex lock;

    Ns_MutexLock(&lock);
    ent = readdir(dir);
    if (ent != NULL) {
	memcpy(&tlsPtr->rdBuf.ent, ent,
	       sizeof(*ent) - sizeof(ent->d_name) + ent->d_namlen + 1);
	ent = &tlsPtr->rdBuf.ent;
    }
    Ns_MutexUnlock(&lock);

#elif defined(__hp10)
    ent = &tlsPtr->rdBuf.ent;
    if (readdir_r(dir, ent) != 0) {
        ent = NULL;
    }

#else
    ent = &tlsPtr->rdBuf.ent; 
    if (readdir_r(dir, ent, &ent) != 0) {
	ent = NULL;
    }

#endif
#endif

    return ent;
}


struct tm *
ns_localtime(const time_t * clock)
{
    struct tm *ptm;

#ifdef WIN32
    ptm = localtime(clock);

#else
    Tls *tlsPtr = GetTls();

#if defined(NO_REENTRANT)
    static Ns_Mutex lock;

    Ns_MutexLock(&lock);
    ptm = localtime(clock);
    if (ptm != NULL) {
	tlsPtr->ltBuf = *ptm;
	ptm = &tlsPtr->ltBuf;
    }
    Ns_MutexUnlock(&lock);

#elif defined(__hp10)
    ptm = &tlsPtr->ltBuf;
    if (localtime_r(clock, ptm) != 0) {
	ptm = NULL;
    }

#else
    ptm = localtime_r(clock, &tlsPtr->ltBuf);

#endif
#endif

   return ptm;
}


struct tm *
ns_gmtime(const time_t * clock)
{
    struct tm *ptm;

#ifdef WIN32
    ptm = gmtime(clock);

#else
    Tls *tlsPtr = GetTls();

#if defined(NO_REENTRANT)
    static Ns_Mutex lock;

    Ns_MutexLock(&lock);
    ptm = gmtime(clock);
    if (ptm != NULL) {
	tlsPtr->gtBuf = *ptm;
	ptm = &tlsPtr->gtBuf;
    }
    Ns_MutexUnlock(&lock);

#elif defined(__hp10)
    ptm = &tlsPtr->gtBuf;
    if (gmtime_r(clock, &tlsPtr->gtBuf) != 0) {
	ptm = NULL;
    }

#else
    ptm = gmtime_r(clock, &tlsPtr->gtBuf);

#endif
#endif

    return ptm;
}


char *
ns_ctime(const time_t * clock)
{
    char *ct;

#ifdef WIN32
    ct = ctime(clock);

#else
    Tls *tlsPtr = GetTls();

#if defined(NO_REENTRANT)
    static Ns_Mutex lock;

    Ns_MutexLock(&lock);
    ct = ctime(clock);
    if (ct != NULL) {
	strcpy(tlsPtr->ctBuf, ct);
	ct = tlsPtr->ctBuf;
    }
    Ns_MutexUnlock(&lock);

#elif defined(__hp10)
    ct = tlsPtr->ctBuf;
    if (ctime_r(clock, ct, 27) != 0) {
	ct = NULL;
    }

#else
    ct = ctime_r(clock, tlsPtr->ctBuf);

#endif
#endif

    return ct;
}


char *
ns_asctime(const struct tm *tmPtr)
{
    char *at;

#ifdef WIN32
    at = asctime(tmPtr);

#else
    Tls *tlsPtr = GetTls();

#if defined(NO_REENTRANT)
    static Ns_Mutex lock;

    Ns_MutexLock(&lock);
    at = asctime(tmPtr);
    if (at != NULL) {
	strcpy(tlsPtr->asBuf, at);
	at = tlsPtr->asBuf;
    }
    Ns_MutexUnlock(&lock);

#elif defined(__hp10)
    at = tlsPtr->asBuf;
    if (asctime_r(tmPtr, at, 27) != 0) {
	at = NULL;
    }

#else
    at = asctime_r(tmPtr, tlsPtr->asBuf);

#endif
#endif
    return at;
}


char *
ns_strtok(char *s1, const char *s2)
{
#ifdef WIN32
    return strtok(s1, s2);
#else
    Tls *tlsPtr = GetTls();

    return strtok_r(s1, s2, &tlsPtr->stBuf);
#endif
}


char *
ns_inet_ntoa(struct in_addr addr)
{
    Tls *tlsPtr = GetTls();
    union {
    	unsigned long l;
    	unsigned char b[4];
    } u;
    
    u.l = (unsigned long) addr.s_addr;
    sprintf(tlsPtr->naBuf, "%u.%u.%u.%u", u.b[0], u.b[1], u.b[2], u.b[3]);
    return tlsPtr->naBuf;
}


static Tls *
GetTls(void)
{
    static Ns_Tls tls;
    static volatile int initialized;
    Tls *tlsPtr;

    if (!initialized) {
	Ns_MasterLock();
	if (!initialized) {
	    Ns_TlsAlloc(&tls, NsFree);
	    initialized = 1;
	}
	Ns_MasterUnlock();
    }
    tlsPtr = Ns_TlsGet(&tls);
    if (tlsPtr == NULL) {
	tlsPtr = NsAlloc(sizeof(Tls));
	Ns_TlsSet(&tls, tlsPtr);
    }
    return tlsPtr;
}



#ifdef MACOSX

/*
 * Copyright (c) 1998 Softweyr LLC.  All rights reserved.
 *
 * strtok_r, from Berkeley strtok
 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
 *
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notices, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notices, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *
 *	This product includes software developed by Softweyr LLC, the
 *      University of California, Berkeley, and its contributors.
 *
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SOFTWEYR LLC, THE
 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stddef.h>
#include <string.h>

char *
strtok_r(char *s, const char *delim, char **last)
{
    char *spanp;
    int c, sc;
    char *tok;

    if (s == NULL && (s = *last) == NULL)
    {
	return NULL;
    }

    /*
     * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
     */
cont:
    c = *s++;
    for (spanp = (char *)delim; (sc = *spanp++) != 0; )
    {
	if (c == sc)
	{
	    goto cont;
	}
    }

    if (c == 0)		/* no non-delimiter characters */
    {
	*last = NULL;
	return NULL;
    }
    tok = s - 1;

    /*
     * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
     * Note that delim must have one NUL; we stop if we see that, too.
     */
    for (;;)
    {
	c = *s++;
	spanp = (char *)delim;
	do
	{
	    if ((sc = *spanp++) == c)
	    {
		if (c == 0)
		{
		    s = NULL;
		}
		else
		{
		    char *w = s - 1;
		    *w = '\0';
		}
		*last = s;
		return tok;
	    }
	}
	while (sc != 0);
    }
    /* NOTREACHED */
}

#endif