Debian Codegraph @

nx-X11/include/Xos_r.h

raw
1 /*
2 Copyright 1996, 1998 The Open Group
3
4 Permission to use, copy, modify, distribute, and sell this software and its
5 documentation for any purpose is hereby granted without fee, provided that
6 the above copyright notice appear in all copies and that both that
7 copyright notice and this permission notice appear in supporting
8 documentation.
9
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20 Except as contained in this notice, the name of The Open Group shall not be
21 used in advertising or otherwise to promote the sale, use or other dealings
22 in this Software without prior written authorization from The Open Group.
23 */
24
25 /*
26 * Various and sundry Thread-Safe functions used by X11, Motif, and CDE.
27 *
28 * Use this file in MT-safe code where you would have included
29 * <dirent.h> for readdir()
30 * <grp.h> for getgrgid() or getgrnam()
31 * <netdb.h> for gethostbyname(), gethostbyaddr(), or getservbyname()
32 * <pwd.h> for getpwnam() or getpwuid()
33 * <string.h> for strtok()
34 * <time.h> for asctime(), ctime(), localtime(), or gmtime()
35 * <unistd.h> for getlogin() or ttyname()
36 * or their thread-safe analogs.
37 *
38 * If you are on a platform that defines XTHREADS but does not have
39 * MT-safe system API (e.g. UnixWare) you must define _Xos_processLock
40 * and _Xos_processUnlock macros before including this header.
41 * For convenience XOS_USE_XLIB_LOCKING or
42 * XOS_USE_XT_LOCKING may be defined to obtain either Xlib-only or
43 * Xt-based versions of these macros. These macros won't result in
44 * truly thread-safe calls, but they are better than nothing. If you
45 * do not want locking in this situation define XOS_USE_NO_LOCKING.
46 *
47 * NOTE: On systems lacking approriate _r functions Gethostbyname(),
48 * Gethostbyaddr(), and Getservbyname() do NOT copy the host or
49 * protocol lists!
50 *
51 * NOTE: On systems lacking appropriate _r functions Getgrgid() and
52 * Getgrnam() do NOT copy the list of group members!
53 *
54 * This header is nominally intended to simplify porting X11, Motif, and
55 * CDE; it may be useful to other people too. The structure below is
56 * complicated, mostly because P1003.1c (the IEEE POSIX Threads spec)
57 * went through lots of drafts, and some vendors shipped systems based
58 * on draft API that were changed later. Unfortunately POSIX did not
59 * provide a feature-test macro for distinguishing each of the drafts.
60 */
61
62 /*
63 * This header has several parts. Search for "Effective prototypes"
64 * to locate the beginning of a section.
65 */
66
67 /* This header can be included multiple times with different defines! */
68 #ifndef _XOS_R_H_
69 # define _XOS_R_H_
70
71 # include <nx-X11/Xos.h>
72 # include <nx-X11/Xfuncs.h>
73
74 # ifndef X_NOT_POSIX
75 # ifdef _POSIX_SOURCE
76 # include <limits.h>
77 # else
78 # define _POSIX_SOURCE
79 # include <limits.h>
80 # undef _POSIX_SOURCE
81 # endif
82 # ifndef LINE_MAX
83 # define X_LINE_MAX 2048
84 # else
85 # define X_LINE_MAX LINE_MAX
86 # endif
87 # endif
88 #endif /* _XOS_R_H */
89
90 #ifndef WIN32
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95
96 # if defined(XOS_USE_XLIB_LOCKING)
97 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
98 typedef struct _LockInfoRec *LockInfoPtr;
99 extern LockInfoPtr _Xglobal_lock;
100 # endif
101 # ifndef _Xos_isThreadInitialized
102 # define _Xos_isThreadInitialized (_Xglobal_lock)
103 # endif
104 # if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
105 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
106 # include <nx-X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
107 extern void (*_XLockMutex_fn)(
108 # if NeedFunctionPrototypes
109 LockInfoPtr /* lock */, char * /* file */, int /* line */
110 # endif
111 );
112 extern void (*_XUnlockMutex_fn)(
113 # if NeedFunctionPrototypes
114 LockInfoPtr /* lock */, char * /* file */, int /* line */
115 # endif
116 );
117 # endif
118 # ifndef _Xos_processLock
119 # define _Xos_processLock \
120 (_XLockMutex_fn ? (*_XLockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
121 # endif
122 # ifndef _Xos_processUnlock
123 # define _Xos_processUnlock \
124 (_XUnlockMutex_fn ? (*_XUnlockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
125 # endif
126 # else
127 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
128 # include <nx-X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
129 extern void (*_XLockMutex_fn)(
130 # if NeedFunctionPrototypes
131 LockInfoPtr /* lock */
132 # endif
133 );
134 extern void (*_XUnlockMutex_fn)(
135 # if NeedFunctionPrototypes
136 LockInfoPtr /* lock */
137 # endif
138 );
139 # endif
140 # ifndef _Xos_processLock
141 # define _Xos_processLock \
142 (_XLockMutex_fn ? ((*_XLockMutex_fn)(_Xglobal_lock), 0) : 0)
143 # endif
144 # ifndef _Xos_processUnlock
145 # define _Xos_processUnlock \
146 (_XUnlockMutex_fn ? ((*_XUnlockMutex_fn)(_Xglobal_lock), 0) : 0)
147 # endif
148 # endif
149 # elif defined(XOS_USE_XT_LOCKING)
150 # ifndef _XtThreadsI_h
151 extern void (*_XtProcessLock)(void);
152 # endif
153 # ifndef _XtintrinsicP_h
154 # include <nx-X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
155 extern void XtProcessLock(
156 # if NeedFunctionPrototypes
157 void
158 # endif
159 );
160 extern void XtProcessUnlock(
161 # if NeedFunctionPrototypes
162 void
163 # endif
164 );
165 # endif
166 # ifndef _Xos_isThreadInitialized
167 # define _Xos_isThreadInitialized _XtProcessLock
168 # endif
169 # ifndef _Xos_processLock
170 # define _Xos_processLock XtProcessLock()
171 # endif
172 # ifndef _Xos_processUnlock
173 # define _Xos_processUnlock XtProcessUnlock()
174 # endif
175 # elif defined(XOS_USE_NO_LOCKING)
176 # ifndef _Xos_isThreadInitialized
177 # define _Xos_isThreadInitialized 0
178 # endif
179 # ifndef _Xos_processLock
180 # define _Xos_processLock 0
181 # endif
182 # ifndef _Xos_processUnlock
183 # define _Xos_processUnlock 0
184 # endif
185 # endif
186
187 #endif /* !defined WIN32 */
188
189 /*
190 * Solaris defines the POSIX thread-safe feature test macro, but
191 * uses the older SVR4 thread-safe functions unless the POSIX ones
192 * are specifically requested. Fix the feature test macro.
193 */
194 #if defined(sun) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
195 (_POSIX_C_SOURCE - 0 < 199506L) && !defined(_POSIX_PTHREAD_SEMANTICS)
196 # undef _POSIX_THREAD_SAFE_FUNCTIONS
197 #endif
198
199
200 /***** <pwd.h> wrappers *****/
201
202 /*
203 * Effective prototypes for <pwd.h> wrappers:
204 *
205 * #define X_INCLUDE_PWD_H
206 * #define XOS_USE_..._LOCKING
207 * #include <nx-X11/Xos_r.h>
208 *
209 * typedef ... _Xgetpwparams;
210 *
211 * struct passwd* _XGetpwnam(const char *name, _Xgetpwparams);
212 * struct passwd* _XGetpwuid(uid_t uid, _Xgetpwparams);
213 */
214
215 #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
216 # include <pwd.h>
217 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_PWDAPI)
218 # define XOS_USE_MTSAFE_PWDAPI 1
219 # endif
220 #endif
221
222 #undef X_NEEDS_PWPARAMS
223 #if !defined(X_INCLUDE_PWD_H) || defined(_XOS_INCLUDED_PWD_H)
224 /* Do nothing */
225
226 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
227 /* Use regular, unsafe API. */
228 # if defined(X_NOT_POSIX) && !defined(i386) && !defined(SYSV)
229 extern struct passwd *getpwuid(), *getpwnam();
230 # endif
231 typedef int _Xgetpwparams; /* dummy */
232 # define _XGetpwuid(u,p) getpwuid((u))
233 # define _XGetpwnam(u,p) getpwnam((u))
234
235 #elif !defined(XOS_USE_MTSAFE_PWDAPI) || defined(XNO_MTSAFE_PWDAPI)
236 /* UnixWare 2.0, or other systems with thread support but no _r API. */
237 # define X_NEEDS_PWPARAMS
238 typedef struct {
239 struct passwd pws;
240 char pwbuf[1024];
241 struct passwd* pwp;
242 size_t len;
243 } _Xgetpwparams;
244
245 /*
246 * NetBSD and FreeBSD, at least, are missing several of the unixware passwd
247 * fields.
248 */
249
250 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
251 defined(__APPLE__) || defined(__DARWIN__) || defined(__DragonFly__)
252 static __inline__ void _Xpw_copyPasswd(_Xgetpwparams p)
253 {
254 memcpy(&(p).pws, (p).pwp, sizeof(struct passwd));
255
256 (p).pws.pw_name = (p).pwbuf;
257 (p).len = strlen((p).pwp->pw_name);
258 strcpy((p).pws.pw_name, (p).pwp->pw_name);
259
260 (p).pws.pw_passwd = (p).pws.pw_name + (p).len + 1;
261 (p).len = strlen((p).pwp->pw_passwd);
262 strcpy((p).pws.pw_passwd,(p).pwp->pw_passwd);
263
264 (p).pws.pw_class = (p).pws.pw_passwd + (p).len + 1;
265 (p).len = strlen((p).pwp->pw_class);
266 strcpy((p).pws.pw_class, (p).pwp->pw_class);
267
268 (p).pws.pw_gecos = (p).pws.pw_class + (p).len + 1;
269 (p).len = strlen((p).pwp->pw_gecos);
270 strcpy((p).pws.pw_gecos, (p).pwp->pw_gecos);
271
272 (p).pws.pw_dir = (p).pws.pw_gecos + (p).len + 1;
273 (p).len = strlen((p).pwp->pw_dir);
274 strcpy((p).pws.pw_dir, (p).pwp->pw_dir);
275
276 (p).pws.pw_shell = (p).pws.pw_dir + (p).len + 1;
277 (p).len = strlen((p).pwp->pw_shell);
278 strcpy((p).pws.pw_shell, (p).pwp->pw_shell);
279
280 (p).pwp = &(p).pws;
281 }
282
283 #else
284 # define _Xpw_copyPasswd(p) \
285 (memcpy(&(p).pws, (p).pwp, sizeof(struct passwd)), \
286 ((p).pws.pw_name = (p).pwbuf), \
287 ((p).len = strlen((p).pwp->pw_name)), \
288 strcpy((p).pws.pw_name, (p).pwp->pw_name), \
289 ((p).pws.pw_passwd = (p).pws.pw_name + (p).len + 1), \
290 ((p).len = strlen((p).pwp->pw_passwd)), \
291 strcpy((p).pws.pw_passwd,(p).pwp->pw_passwd), \
292 ((p).pws.pw_age = (p).pws.pw_passwd + (p).len + 1), \
293 ((p).len = strlen((p).pwp->pw_age)), \
294 strcpy((p).pws.pw_age, (p).pwp->pw_age), \
295 ((p).pws.pw_comment = (p).pws.pw_age + (p).len + 1), \
296 ((p).len = strlen((p).pwp->pw_comment)), \
297 strcpy((p).pws.pw_comment, (p).pwp->pw_comment), \
298 ((p).pws.pw_gecos = (p).pws.pw_comment + (p).len + 1), \
299 ((p).len = strlen((p).pwp->pw_gecos)), \
300 strcpy((p).pws.pw_gecos, (p).pwp->pw_gecos), \
301 ((p).pws.pw_dir = (p).pws.pw_comment + (p).len + 1), \
302 ((p).len = strlen((p).pwp->pw_dir)), \
303 strcpy((p).pws.pw_dir, (p).pwp->pw_dir), \
304 ((p).pws.pw_shell = (p).pws.pw_dir + (p).len + 1), \
305 ((p).len = strlen((p).pwp->pw_shell)), \
306 strcpy((p).pws.pw_shell, (p).pwp->pw_shell), \
307 ((p).pwp = &(p).pws), \
308 0 )
309 #endif
310 # define _XGetpwuid(u,p) \
311 ( (_Xos_processLock), \
312 (((p).pwp = getpwuid((u))) ? _Xpw_copyPasswd(p), 0 : 0), \
313 (_Xos_processUnlock), \
314 (p).pwp )
315 # define _XGetpwnam(u,p) \
316 ( (_Xos_processLock), \
317 (((p).pwp = getpwnam((u))) ? _Xpw_copyPasswd(p), 0 : 0), \
318 (_Xos_processUnlock), \
319 (p).pwp )
320
321 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(__APPLE__) && !defined(__DARWIN__)
322 /* SVR4 threads */
323 # define X_NEEDS_PWPARAMS
324 typedef struct {
325 struct passwd pws;
326 char pwbuf[X_LINE_MAX];
327 } _Xgetpwparams;
328 # if defined(_POSIX_REENTRANT_FUNCTIONS) || !defined(SVR4)
329 # define _XGetpwuid(u,p) \
330 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
331 # define _XGetpwnam(u,p) \
332 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
333 # else /* SVR4 */
334 # define _XGetpwuid(u,p) \
335 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
336 # define _XGetpwnam(u,p) \
337 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
338 # endif /* SVR4 */
339
340 #else /* _POSIX_THREAD_SAFE_FUNCTIONS */
341 /* Digital UNIX 4.0, but not (beta) T4.0-1 */
342 # define X_NEEDS_PWPARAMS
343 typedef struct {
344 struct passwd pws;
345 char pwbuf[X_LINE_MAX];
346 struct passwd* pwp;
347 } _Xgetpwparams;
348 typedef int _Xgetpwret;
349 # define _XGetpwuid(u,p) \
350 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == 0) ? \
351 (p).pwp : NULL)
352 # define _XGetpwnam(u,p) \
353 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == 0) ? \
354 (p).pwp : NULL)
355 #endif /* X_INCLUDE_PWD_H */
356
357 #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
358 # define _XOS_INCLUDED_PWD_H
359 #endif
360
361
362 /***** <netdb.h> wrappers *****/
363
364 /*
365 * Effective prototypes for <netdb.h> wrappers:
366 *
367 * NOTE: On systems lacking the appropriate _r functions Gethostbyname(),
368 * Gethostbyaddr(), and Getservbyname() do NOT copy the host or
369 * protocol lists!
370 *
371 * #define X_INCLUDE_NETDB_H
372 * #define XOS_USE_..._LOCKING
373 * #include <nx-X11/Xos_r.h>
374 *
375 * typedef ... _Xgethostbynameparams;
376 * typedef ... _Xgetservbynameparams;
377 *
378 * struct hostent* _XGethostbyname(const char* name,_Xgethostbynameparams);
379 * struct hostent* _XGethostbyaddr(const char* addr, int len, int type,
380 * _Xgethostbynameparams);
381 * struct servent* _XGetservbyname(const char* name, const char* proto,
382 * _Xgetservbynameparams);
383 */
384
385 #undef XTHREADS_NEEDS_BYNAMEPARAMS
386 #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H) \
387 && !defined(WIN32)
388 # include <netdb.h>
389 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_NETDBAPI)
390 # define XOS_USE_MTSAFE_NETDBAPI 1
391 # endif
392 #endif
393
394 #if !defined(X_INCLUDE_NETDB_H) || defined(_XOS_INCLUDED_NETDB_H)
395 /* Do nothing. */
396
397 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
398 /* Use regular, unsafe API. */
399 typedef int _Xgethostbynameparams; /* dummy */
400 typedef int _Xgetservbynameparams; /* dummy */
401 # define _XGethostbyname(h,hp) gethostbyname((h))
402 # define _XGethostbyaddr(a,al,t,hp) gethostbyaddr((a),(al),(t))
403 # define _XGetservbyname(s,p,sp) getservbyname((s),(p))
404
405 #elif !defined(XOS_USE_MTSAFE_NETDBAPI) || defined(XNO_MTSAFE_NETDBAPI)
406 /* UnixWare 2.0, or other systems with thread support but no _r API. */
407 /* WARNING: The h_addr_list and s_aliases values are *not* copied! */
408
409 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
410 #include <sys/param.h>
411 #endif
412
413 typedef struct {
414 struct hostent hent;
415 char h_name[MAXHOSTNAMELEN];
416 struct hostent *hptr;
417 } _Xgethostbynameparams;
418 typedef struct {
419 struct servent sent;
420 char s_name[255];
421 char s_proto[255];
422 struct servent *sptr;
423 } _Xgetservbynameparams;
424
425 # define XTHREADS_NEEDS_BYNAMEPARAMS
426
427 # define _Xg_copyHostent(hp) \
428 (memcpy(&(hp).hent, (hp).hptr, sizeof(struct hostent)), \
429 strcpy((hp).h_name, (hp).hptr->h_name), \
430 ((hp).hent.h_name = (hp).h_name), \
431 ((hp).hptr = &(hp).hent), \
432 0 )
433 # define _Xg_copyServent(sp) \
434 (memcpy(&(sp).sent, (sp).sptr, sizeof(struct servent)), \
435 strcpy((sp).s_name, (sp).sptr->s_name), \
436 ((sp).sent.s_name = (sp).s_name), \
437 strcpy((sp).s_proto, (sp).sptr->s_proto), \
438 ((sp).sent.s_proto = (sp).s_proto), \
439 ((sp).sptr = &(sp).sent), \
440 0 )
441 # define _XGethostbyname(h,hp) \
442 ((_Xos_processLock), \
443 (((hp).hptr = gethostbyname((h))) ? _Xg_copyHostent(hp) : 0), \
444 (_Xos_processUnlock), \
445 (hp).hptr )
446 # define _XGethostbyaddr(a,al,t,hp) \
447 ((_Xos_processLock), \
448 (((hp).hptr = gethostbyaddr((a),(al),(t))) ? _Xg_copyHostent(hp) : 0), \
449 (_Xos_processUnlock), \
450 (hp).hptr )
451 # define _XGetservbyname(s,p,sp) \
452 ((_Xos_processLock), \
453 (((sp).sptr = getservbyname((s),(p))) ? _Xg_copyServent(sp) : 0), \
454 (_Xos_processUnlock), \
455 (sp).sptr )
456
457 #elif defined(XUSE_NETDB_R_API)
458 /*
459 * POSIX does not specify _r equivalents for <netdb.h> API, but some
460 * vendors provide them anyway. Use them only when explicitly asked.
461 */
462 # ifdef _POSIX_THREAD_SAFE_FUNCTIONS
463 # define X_POSIX_THREAD_SAFE_FUNCTIONS 1
464 # endif
465
466 # define XTHREADS_NEEDS_BYNAMEPARAMS
467
468 # ifndef X_POSIX_THREAD_SAFE_FUNCTIONS
469 typedef struct {
470 struct hostent hent;
471 char hbuf[X_LINE_MAX];
472 int herr;
473 } _Xgethostbynameparams;
474 typedef struct {
475 struct servent sent;
476 char sbuf[X_LINE_MAX];
477 } _Xgetservbynameparams;
478 # define _XGethostbyname(h,hp) \
479 gethostbyname_r((h),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
480 # define _XGethostbyaddr(a,al,t,hp) \
481 gethostbyaddr_r((a),(al),(t),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
482 # define _XGetservbyname(s,p,sp) \
483 getservbyname_r((s),(p),&(sp).sent,(sp).sbuf,sizeof((sp).sbuf))
484 # else
485 typedef struct {
486 struct hostent hent;
487 struct hostent_data hdata;
488 } _Xgethostbynameparams;
489 typedef struct {
490 struct servent sent;
491 struct servent_data sdata;
492 } _Xgetservbynameparams;
493 # define _XGethostbyname(h,hp) \
494 (bzero((char*)&(hp).hdata,sizeof((hp).hdata)), \
495 ((gethostbyname_r((h),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
496 # define _XGethostbyaddr(a,al,t,hp) \
497 (bzero((char*)&(hp).hdata,sizeof((hp).hdata)), \
498 ((gethostbyaddr_r((a),(al),(t),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
499 # define _XGetservbyname(s,p,sp) \
500 (bzero((char*)&(sp).sdata,sizeof((sp).sdata)), \
501 ((getservbyname_r((s),(p),&(sp).sent,&(sp).sdata) == -1) ? NULL : &(sp).sent) )
502 # endif
503 # ifdef X_POSIX_THREAD_SAFE_FUNCTIONS
504 # undef X_POSIX_THREAD_SAFE_FUNCTIONS
505 # endif
506
507 #else
508 /* The regular API is assumed to be MT-safe under POSIX. */
509 typedef int _Xgethostbynameparams; /* dummy */
510 typedef int _Xgetservbynameparams; /* dummy */
511 # define _XGethostbyname(h,hp) gethostbyname((h))
512 # define _XGethostbyaddr(a,al,t,hp) gethostbyaddr((a),(al),(t))
513 # define _XGetservbyname(s,p,sp) getservbyname((s),(p))
514 #endif /* X_INCLUDE_NETDB_H */
515
516 #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H)
517 # define _XOS_INCLUDED_NETDB_H
518 #endif
519
520
521 /***** <dirent.h> wrappers *****/
522
523 /*
524 * Effective prototypes for <dirent.h> wrappers:
525 *
526 * #define X_INCLUDE_DIRENT_H
527 * #define XOS_USE_..._LOCKING
528 * #include <nx-X11/Xos_r.h>
529 *
530 * typedef ... _Xreaddirparams;
531 *
532 * struct dirent *_XReaddir(DIR *dir_pointer, _Xreaddirparams);
533 */
534
535 #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
536 # include <sys/types.h>
537 # if !defined(X_NOT_POSIX) || defined(SYSV) || defined(USG)
538 # include <dirent.h>
539 # else
540 # include <sys/dir.h>
541 # ifndef dirent
542 # define dirent direct
543 # endif
544 # endif
545 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_DIRENTAPI)
546 # define XOS_USE_MTSAFE_DIRENTAPI 1
547 # endif
548 #endif
549
550 #if !defined(X_INCLUDE_DIRENT_H) || defined(_XOS_INCLUDED_DIRENT_H)
551 /* Do nothing. */
552
553 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
554 /* Use regular, unsafe API. */
555 typedef int _Xreaddirparams; /* dummy */
556 # define _XReaddir(d,p) readdir(d)
557
558 #elif !defined(XOS_USE_MTSAFE_DIRENTAPI) || defined(XNO_MTSAFE_DIRENTAPI)
559 /* Systems with thread support but no _r API. */
560 typedef struct {
561 struct dirent *result;
562 struct dirent dir_entry;
563 # ifdef _POSIX_PATH_MAX
564 char buf[_POSIX_PATH_MAX];
565 # elif defined(NAME_MAX)
566 char buf[NAME_MAX];
567 # else
568 char buf[255];
569 # endif
570 } _Xreaddirparams;
571
572 # define _XReaddir(d,p) \
573 ( (_Xos_processLock), \
574 (((p).result = readdir((d))) ? \
575 (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
576 ((p).result = &(p).dir_entry), 0) : \
577 0), \
578 (_Xos_processUnlock), \
579 (p).result )
580
581 #else
582 typedef struct {
583 struct dirent *result;
584 struct dirent dir_entry;
585 # ifdef _POSIX_PATH_MAX
586 char buf[_POSIX_PATH_MAX];
587 # elif defined(NAME_MAX)
588 char buf[NAME_MAX];
589 # else
590 char buf[255];
591 # endif
592 } _Xreaddirparams;
593
594 # if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || \
595 defined(__APPLE__) || defined(__DARWIN__)
596 /* POSIX final API, returns (int)0 on success. */
597 # define _XReaddir(d,p) \
598 (readdir_r((d), &((p).dir_entry), &((p).result)) ? NULL : (p).result)
599 # elif defined(_POSIX_REENTRANT_FUNCTIONS)
600 /* POSIX draft API, returns (int)0 on success. */
601 # define _XReaddir(d,p) \
602 (readdir_r((d),&((p).dir_entry)) ? NULL : &((p).dir_entry))
603 # elif defined(SVR4)
604 /* Pre-POSIX API, returns non-NULL on success. */
605 # define _XReaddir(d,p) (readdir_r((d), &(p).dir_entry))
606 # else
607 /* We have no idea what is going on. Fake it all using process locks. */
608 # define _XReaddir(d,p) \
609 ( (_Xos_processLock), \
610 (((p).result = readdir((d))) ? \
611 (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
612 ((p).result = &(p).dir_entry), 0) : \
613 0), \
614 (_Xos_processUnlock), \
615 (p).result )
616 # endif
617 #endif /* X_INCLUDE_DIRENT_H */
618
619 #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
620 # define _XOS_INCLUDED_DIRENT_H
621 #endif
622
623
624 /***** <unistd.h> wrappers *****/
625
626 /*
627 * Effective prototypes for <unistd.h> wrappers:
628 *
629 * #define X_INCLUDE_UNISTD_H
630 * #define XOS_USE_..._LOCKING
631 * #include <nx-X11/Xos_r.h>
632 *
633 * typedef ... _Xgetloginparams;
634 * typedef ... _Xttynameparams;
635 *
636 * char *_XGetlogin(_Xgetloginparams);
637 * char *_XTtyname(int, _Xttynameparams);
638 */
639
640 #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
641 /* <unistd.h> already included by <X11/Xos.h> */
642 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_UNISTDAPI)
643 # define XOS_USE_MTSAFE_UNISTDAPI 1
644 # endif
645 #endif
646
647 #if !defined(X_INCLUDE_UNISTD_H) || defined(_XOS_INCLUDED_UNISTD_H)
648 /* Do nothing. */
649
650 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
651 /* Use regular, unsafe API. */
652 typedef int _Xgetloginparams; /* dummy */
653 typedef int _Xttynameparams; /* dummy */
654 # define _XGetlogin(p) getlogin()
655 # define _XTtyname(f) ttyname((f))
656
657 #elif !defined(XOS_USE_MTSAFE_UNISTDAPI) || defined(XNO_MTSAFE_UNISTDAPI)
658 /* Systems with thread support but no _r API. */
659 typedef struct {
660 char *result;
661 # if defined(MAXLOGNAME)
662 char buf[MAXLOGNAME];
663 # elif defined(LOGIN_NAME_MAX)
664 char buf[LOGIN_NAME_MAX];
665 # else
666 char buf[64];
667 # endif
668 } _Xgetloginparams;
669 typedef struct {
670 char *result;
671 # ifdef TTY_NAME_MAX
672 char buf[TTY_NAME_MAX];
673 # elif defined(_POSIX_TTY_NAME_MAX)
674 char buf[_POSIX_TTY_NAME_MAX];
675 # elif defined(_POSIX_PATH_MAX)
676 char buf[_POSIX_PATH_MAX];
677 # else
678 char buf[256];
679 # endif
680 } _Xttynameparams;
681
682 # define _XGetlogin(p) \
683 ( (_Xos_processLock), \
684 (((p).result = getlogin()) ? \
685 (strncpy((p).buf, (p).result, sizeof((p).buf)), \
686 ((p).buf[sizeof((p).buf)-1] = '\0'), \
687 ((p).result = (p).buf), 0) : 0), \
688 (_Xos_processUnlock), \
689 (p).result )
690 #define _XTtyname(f,p) \
691 ( (_Xos_processLock), \
692 (((p).result = ttyname(f)) ? \
693 (strncpy((p).buf, (p).result, sizeof((p).buf)), \
694 ((p).buf[sizeof((p).buf)-1] = '\0'), \
695 ((p).result = (p).buf), 0) : 0), \
696 (_Xos_processUnlock), \
697 (p).result )
698
699 #elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(_POSIX_REENTRANT_FUNCTIONS)
700 /* POSIX API.
701 *
702 * extern int getlogin_r(char *, size_t);
703 * extern int ttyname_r(int, char *, size_t);
704 */
705 typedef struct {
706 # if defined(MAXLOGNAME)
707 char buf[MAXLOGNAME];
708 # elif defined(LOGIN_NAME_MAX)
709 char buf[LOGIN_NAME_MAX];
710 # else
711 char buf[64];
712 # endif
713 } _Xgetloginparams;
714 typedef struct {
715 # ifdef TTY_NAME_MAX
716 char buf[TTY_NAME_MAX];
717 # elif defined(_POSIX_TTY_NAME_MAX)
718 char buf[_POSIX_TTY_NAME_MAX];
719 # elif defined(_POSIX_PATH_MAX)
720 char buf[_POSIX_PATH_MAX];
721 # else
722 char buf[256];
723 # endif
724 } _Xttynameparams;
725
726 # define _XGetlogin(p) (getlogin_r((p).buf, sizeof((p).buf)) ? NULL : (p).buf)
727 # define _XTtyname(f,p) \
728 (ttyname_r((f), (p).buf, sizeof((p).buf)) ? NULL : (p).buf)
729
730 #else
731 /* Pre-POSIX API.
732 *
733 * extern char *getlogin_r(char *, size_t);
734 * extern char *ttyname_r(int, char *, size_t);
735 */
736 typedef struct {
737 # if defined(MAXLOGNAME)
738 char buf[MAXLOGNAME];
739 # elif defined(LOGIN_NAME_MAX)
740 char buf[LOGIN_NAME_MAX];
741 # else
742 char buf[64];
743 # endif
744 } _Xgetloginparams;
745 typedef struct {
746 # ifdef TTY_NAME_MAX
747 char buf[TTY_NAME_MAX];
748 # elif defined(_POSIX_TTY_NAME_MAX)
749 char buf[_POSIX_TTY_NAME_MAX];
750 # elif defined(_POSIX_PATH_MAX)
751 char buf[_POSIX_PATH_MAX];
752 # else
753 char buf[256];
754 # endif
755 } _Xttynameparams;
756
757 # define _XGetlogin(p) getlogin_r((p).buf, sizeof((p).buf))
758 # define _XTtyname(f,p) ttyname_r((f), (p).buf, sizeof((p).buf))
759 #endif /* X_INCLUDE_UNISTD_H */
760
761 #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
762 # define _XOS_INCLUDED_UNISTD_H
763 #endif
764
765
766 /***** <string.h> wrappers *****/
767
768 /*
769 * Effective prototypes for <string.h> wrappers:
770 *
771 * #define X_INCLUDE_STRING_H
772 * #define XOS_USE_..._LOCKING
773 * #include <nx-X11/Xos_r.h>
774 *
775 * typedef ... _Xstrtokparams;
776 *
777 * char *_XStrtok(char *, const char*, _Xstrtokparams);
778 */
779
780 #if defined(X_INCLUDE_STRING_H) && !defined(_XOS_INCLUDED_STRING_H)
781 /* <string.h> has already been included by <X11/Xos.h> */
782 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_STRINGAPI)
783 # define XOS_USE_MTSAFE_STRINGAPI 1
784 # endif
785 #endif
786
787 #if !defined(X_INCLUDE_STRING_H) || defined(_XOS_INCLUDED_STRING_H)
788 /* Do nothing. */
789
790 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
791 /* Use regular, unsafe API. */
792 typedef int _Xstrtokparams; /* dummy */
793 # define _XStrtok(s1,s2,p) \
794 ( p = 0, (void)p, strtok((s1),(s2)) )
795
796 #elif !defined(XOS_USE_MTSAFE_STRINGAPI) || defined(XNO_MTSAFE_STRINGAPI)
797 /* Systems with thread support but no _r API. */
798 typedef char *_Xstrtokparams;
799 # define _XStrtok(s1,s2,p) \
800 ( (_Xos_processLock), \
801 ((p) = strtok((s1),(s2))), \
802 (_Xos_processUnlock), \
803 (p) )
804
805 #else
806 /* POSIX or pre-POSIX API. */
807 typedef char * _Xstrtokparams;
808 # define _XStrtok(s1,s2,p) strtok_r((s1),(s2),&(p))
809 #endif /* X_INCLUDE_STRING_H */
810
811
812 /***** <time.h> wrappers *****/
813
814 /*
815 * Effective prototypes for <time.h> wrappers:
816 *
817 * #define X_INCLUDE_TIME_H
818 * #define XOS_USE_..._LOCKING
819 * #include <nx-X11/Xos_r.h>
820 *
821 * typedef ... _Xatimeparams;
822 * typedef ... _Xctimeparams;
823 * typedef ... _Xgtimeparams;
824 * typedef ... _Xltimeparams;
825 *
826 * char *_XAsctime(const struct tm *, _Xatimeparams);
827 * char *_XCtime(const time_t *, _Xctimeparams);
828 * struct tm *_XGmtime(const time_t *, _Xgtimeparams);
829 * struct tm *_XLocaltime(const time_t *, _Xltimeparams);
830 */
831
832 #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
833 # include <time.h>
834 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_TIMEAPI)
835 # define XOS_USE_MTSAFE_TIMEAPI 1
836 # endif
837 #endif
838
839 #if !defined(X_INCLUDE_TIME_H) || defined(_XOS_INCLUDED_TIME_H)
840 /* Do nothing. */
841
842 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
843 /* Use regular, unsafe API. */
844 typedef int _Xatimeparams; /* dummy */
845 # define _XAsctime(t,p) asctime((t))
846 typedef int _Xctimeparams; /* dummy */
847 # define _XCtime(t,p) ctime((t))
848 typedef int _Xgtimeparams; /* dummy */
849 # define _XGmtime(t,p) gmtime((t))
850 typedef int _Xltimeparams; /* dummy */
851 # define _XLocaltime(t,p) localtime((t))
852
853 #elif !defined(XOS_USE_MTSAFE_TIMEAPI) || defined(XNO_MTSAFE_TIMEAPI)
854 /* Systems with thread support but no _r API. */
855 typedef struct {
856 # ifdef TIMELEN
857 char buf[TIMELEN];
858 # else
859 char buf[26];
860 # endif
861 char *result;
862 } _Xctimeparams, _Xatimeparams;
863 typedef struct {
864 struct tm buf;
865 struct tm *result;
866 } _Xgtimeparams, _Xltimeparams;
867 # define _XAsctime(t,p) \
868 ( (_Xos_processLock), \
869 (((p).result = asctime((t))) ? \
870 (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
871 0), \
872 (_Xos_processUnlock), \
873 (p).result )
874 # define _XCtime(t,p) \
875 ( (_Xos_processLock), \
876 (((p).result = ctime((t))) ? \
877 (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
878 0), \
879 (_Xos_processUnlock), \
880 (p).result )
881 # define _XGmtime(t,p) \
882 ( (_Xos_processLock), \
883 (((p).result = gmtime(t)) ? \
884 (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
885 0), \
886 (_Xos_processUnlock), \
887 (p).result )
888 # define _XLocaltime(t,p) \
889 ( (_Xos_processLock), \
890 (((p).result = localtime(t)) ? \
891 (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
892 0), \
893 (_Xos_processUnlock), \
894 (p).result )
895
896 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(sun)
897 /* Returns NULL on failure. Solaris 2.5
898 *
899 * extern char *asctime_r(const struct tm *tm,char *buf, int buflen);
900 * extern char *ctime_r(const time_t *clock, char *buf, int buflen);
901 * extern struct tm *gmtime_r(const time_t *clock, struct tm *res);
902 * extern struct tm *localtime_r(const time_t *clock, struct tm *res);
903 */
904 # ifdef TIMELEN
905 typedef char _Xatimeparams[TIMELEN];
906 typedef char _Xctimeparams[TIMELEN];
907 # else
908 typedef char _Xatimeparams[26];
909 typedef char _Xctimeparams[26];
910 # endif
911 typedef struct tm _Xgtimeparams;
912 typedef struct tm _Xltimeparams;
913 # define _XAsctime(t,p) asctime_r((t),(p),sizeof((p)))
914 # define _XCtime(t,p) ctime_r((t),(p),sizeof((p)))
915 # define _XGmtime(t,p) gmtime_r((t),&(p))
916 # define _XLocaltime(t,p) localtime_r((t),&(p))
917
918 #else /* defined(_POSIX_THREAD_SAFE_FUNCTIONS) */
919 /* POSIX final API. OSF/1 v4.0, etc.
920 *
921 * extern char *asctime_r(const struct tm *timeptr, char *buffer);
922 * extern char *ctime_r(const time_t *timer, char *buffer);
923 * extern struct tm *gmtime_r(const time_t *timer, struct tm *result);
924 * extern struct tm *localtime_r(const time_t *timer, struct tm *result);
925 */
926 # ifdef TIMELEN
927 typedef char _Xatimeparams[TIMELEN];
928 typedef char _Xctimeparams[TIMELEN];
929 # else
930 typedef char _Xatimeparams[26];
931 typedef char _Xctimeparams[26];
932 # endif
933 typedef struct tm _Xgtimeparams;
934 typedef struct tm _Xltimeparams;
935 # define _XAsctime(t,p) asctime_r((t),(p))
936 # define _XCtime(t,p) ctime_r((t),(p))
937 # define _XGmtime(t,p) gmtime_r((t),&(p))
938 # define _XLocaltime(t,p) localtime_r((t),&(p))
939 #endif /* X_INCLUDE_TIME_H */
940
941 #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
942 # define _XOS_INCLUDED_TIME_H
943 #endif
944
945
946 /***** <grp.h> wrappers *****/
947
948 /*
949 * Effective prototypes for <grp.h> wrappers:
950 *
951 * NOTE: On systems lacking appropriate _r functions Getgrgid() and
952 * Getgrnam() do NOT copy the list of group members!
953 *
954 * Remember that fgetgrent(), setgrent(), getgrent(), and endgrent()
955 * are not included in POSIX.
956 *
957 * #define X_INCLUDE_GRP_H
958 * #define XOS_USE_..._LOCKING
959 * #include <nx-X11/Xos_r.h>
960 *
961 * typedef ... _Xgetgrparams;
962 *
963 * struct group *_XGetgrgid(gid_t, _Xgetgrparams);
964 * struct group *_XGetgrnam(const char *, _Xgetgrparams);
965 */
966
967 #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
968 # include <grp.h>
969 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_GRPAPI)
970 # define XOS_USE_MTSAFE_GRPAPI 1
971 # endif
972 #endif
973
974 #if !defined(X_INCLUDE_GRP_H) || defined(_XOS_INCLUDED_GRP_H)
975 /* Do nothing. */
976
977 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
978 /* Use regular, unsafe API. */
979 typedef int _Xgetgrparams; /* dummy */
980 #define _XGetgrgid(g,p) getgrgid((g))
981 #define _XGetgrnam(n,p) getgrnam((n))
982
983 #elif !defined(XOS_USE_MTSAFE_GRPAPI) || defined(XNO_MTSAFE_GRPAPI)
984 /* Systems with thread support but no _r API. UnixWare 2.0. */
985 typedef struct {
986 struct group grp;
987 char buf[X_LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
988 struct group *pgrp;
989 size_t len;
990 } _Xgetgrparams;
991 #ifdef SVR4
992 /* Copy the gr_passwd field too. */
993 # define _Xgrp_copyGroup(p) \
994 ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
995 ((p).grp.gr_name = (p).buf), \
996 ((p).len = strlen((p).pgrp->gr_name)), \
997 strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
998 ((p).grp.gr_passwd = (p).grp.gr_name + (p).len + 1), \
999 ((p).pgrp = &(p).grp), \
1000 0 )
1001 #else
1002 # define _Xgrp_copyGroup(p) \
1003 ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
1004 ((p).grp.gr_name = (p).buf), \
1005 strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
1006 ((p).pgrp = &(p).grp), \
1007 0 )
1008 #endif
1009 #define _XGetgrgid(g,p) \
1010 ( (_Xos_processLock), \
1011 (((p).pgrp = getgrgid((g))) ? _Xgrp_copyGroup(p) : 0), \
1012 (_Xos_processUnlock), \
1013 (p).pgrp )
1014 #define _XGetgrnam(n,p) \
1015 ( (_Xos_processLock), \
1016 (((p).pgrp = getgrnam((n))) ? _Xgrp_copyGroup(p) : 0), \
1017 (_Xos_processUnlock), \
1018 (p).pgrp )
1019
1020 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(sun)
1021 /* Non-POSIX API. Solaris, DEC v3.2.
1022 *
1023 * extern struct group *getgrgid_r(gid_t, struct group *, char *, int);
1024 * extern struct group *getgrnam_r(const char *, struct group *, char *, int);
1025 */
1026 typedef struct {
1027 struct group grp;
1028 char buf[X_LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1029 } _Xgetgrparams;
1030 #define _XGetgrgid(g,p) getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf))
1031 #define _XGetgrnam(n,p) getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf))
1032
1033 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
1034 /* Non-POSIX API. HP/UX 10.
1035 *
1036 * extern int getgrgid_r(gid_t, struct group *, char *, int);
1037 * extern int getgrnam_r(const char *, struct group *, char *, int);
1038 */
1039 typedef struct {
1040 struct group grp;
1041 char buf[X_LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1042 } _Xgetgrparams;
1043 #define _XGetgrgid(g,p) \
1044 ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
1045 #define _XGetgrnam(n,p) \
1046 ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
1047
1048 #else
1049 /* POSIX final API. DEC v4.0, IRIX 6.2.
1050 *
1051 * int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
1052 * int getgrnam_r(const char *, struct group *, char *, size_t, struct group **);
1053 */
1054 typedef struct {
1055 struct group grp;
1056 char buf[X_LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1057 struct group *result;
1058 } _Xgetgrparams;
1059
1060 #define _XGetgrgid(g,p) \
1061 ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
1062 NULL : (p).result))
1063 #define _XGetgrnam(n,p) \
1064 ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
1065 NULL : (p).result))
1066 #endif
1067
1068 #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
1069 # define _XOS_INCLUDED_GRP_H
1070 #endif
1071
1072
1073 #ifdef __cplusplus
1074 } /* Close scope of 'extern "C"' declaration which encloses file. */
1075 #endif