1 /* $XConsortium: Xos_r.h /main/5 1996/12/18 16:29:14 lehors $ */
3 Copyright (c) 1996 X Consortium
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
26 /* $XFree86: xc/include/Xos_r.h,v 1.3 1997/01/18 07:17:11 dawes Exp $ */
29 * Various and sundry Thread-Safe functions used by X11, Motif, and CDE.
31 * Use this file in MT-safe code where you would have included
32 * <dirent.h> for readdir()
33 * <grp.h> for getgrgid() or getgrnam()
34 * <netdb.h> for gethostbyname(), gethostbyaddr(), or getservbyname()
35 * <pwd.h> for getpwnam() or getpwuid()
36 * <string.h> for strtok()
37 * <time.h> for asctime(), ctime(), localtime(), or gmtime()
38 * <unistd.h> for getlogin() or ttyname()
39 * or their thread-safe analogs.
41 * If you are on a platform that defines XTHREADS but does not have
42 * MT-safe system API (e.g. UnixWare) you must define _Xos_processLock
43 * and _Xos_processUnlock macros before including this header. If
44 * you are on OSF/1 V3.2 and plan to use readdir(), you must also define
45 * _Xos_isThreadsInitialized. For convenience XOS_USE_XLIB_LOCKING or
46 * XOS_USE_XT_LOCKING may be defined to obtain either Xlib-only or
47 * Xt-based versions of these macros. These macros won't result in
48 * truly thread-safe calls, but they are better than nothing. If you
49 * do not want locking in this situation define XOS_USE_NO_LOCKING.
51 * NOTE: On systems lacking approriate _r functions Gethostbyname(),
52 * Gethostbyaddr(), and Getservbyname() do NOT copy the host or
55 * NOTE: On systems lacking appropriate _r functions Getgrgid() and
56 * Getgrnam() do NOT copy the list of group members!
58 * This header is nominally intended to simplify porting X11, Motif, and
59 * CDE; it may be useful to other people too. The structure below is
60 * complicated, mostly because P1003.1c (the IEEE POSIX Threads spec)
61 * went through lots of drafts, and some vendors shipped systems based
62 * on draft API that were changed later. Unfortunately POSIX did not
63 * provide a feature-test macro for distinguishing each of the drafts.
67 * This header has several parts. Search for "Effective prototypes"
68 * to locate the beginning of a section.
71 /* This header can be included multiple times with different defines! */
76 # include <X11/Xfuncs.h>
82 # define _POSIX_SOURCE
95 # if defined(XOS_USE_XLIB_LOCKING)
96 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
97 typedef struct _LockInfoRec *LockInfoPtr;
98 extern LockInfoPtr _Xglobal_lock;
100 # ifndef _Xos_isThreadInitialized
101 # define _Xos_isThreadInitialized (_Xglobal_lock)
103 # if defined(XTHREADS_WARN) || defined(XTHREADS_FILE_LINE)
104 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
105 # include <X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
106 extern void (*_XLockMutex_fn)(
107 # if NeedFunctionPrototypes
108 LockInfoPtr /* lock */, char * /* file */, int /* line */
111 extern void (*_XUnlockMutex_fn)(
112 # if NeedFunctionPrototypes
113 LockInfoPtr /* lock */, char * /* file */, int /* line */
117 # ifndef _Xos_processLock
118 # define _Xos_processLock \
119 (_XLockMutex_fn ? (*_XLockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
121 # ifndef _Xos_processUnlock
122 # define _Xos_processUnlock \
123 (_XUnlockMutex_fn ? (*_XUnlockMutex_fn)(_Xglobal_lock,__FILE__,__LINE__) : 0)
126 # ifndef XAllocIDs /* Xlibint.h does not have multiple include protection */
127 # include <X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
128 extern void (*_XLockMutex_fn)(
129 # if NeedFunctionPrototypes
130 LockInfoPtr /* lock */
133 extern void (*_XUnlockMutex_fn)(
134 # if NeedFunctionPrototypes
135 LockInfoPtr /* lock */
139 # ifndef _Xos_processLock
140 # define _Xos_processLock \
141 (_XLockMutex_fn ? ((*_XLockMutex_fn)(_Xglobal_lock), 0) : 0)
143 # ifndef _Xos_processUnlock
144 # define _Xos_processUnlock \
145 (_XUnlockMutex_fn ? ((*_XUnlockMutex_fn)(_Xglobal_lock), 0) : 0)
148 # elif defined(XOS_USE_XT_LOCKING)
149 extern void (*_XtProcessLock)();
150 # ifndef _XtintrinsicP_h
151 # include <X11/Xfuncproto.h> /* for NeedFunctionPrototypes */
152 extern void XtProcessLock(
153 # if NeedFunctionPrototypes
157 extern void XtProcessUnlock(
158 # if NeedFunctionPrototypes
163 # ifndef _Xos_isThreadInitialized
164 # define _Xos_isThreadInitialized _XtProcessLock
166 # ifndef _Xos_processLock
167 # define _Xos_processLock XtProcessLock()
169 # ifndef _Xos_processUnlock
170 # define _Xos_processUnlock XtProcessUnlock()
172 # elif defined(XOS_USE_NO_LOCKING)
173 # ifndef _Xos_isThreadInitialized
174 # define _Xos_isThreadInitialized 0
176 # ifndef _Xos_processLock
177 # define _Xos_processLock 0
179 # ifndef _Xos_processUnlock
180 # define _Xos_processUnlock 0
184 #endif /* !defined WIN32 */
187 * Solaris 2.5 has SVR4 thread-safe API, but defines the POSIX
188 * thread-safe feature test macro. Fix the feature test macro.
190 #if defined(sun) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
191 # undef _POSIX_THREAD_SAFE_FUNCTIONS
195 /***** <pwd.h> wrappers *****/
198 * Effective prototypes for <pwd.h> wrappers:
200 * #define X_INCLUDE_PWD_H
201 * #define XOS_USE_..._LOCKING
202 * #include <X11/Xos_r.h>
204 * typedef ... _Xgetpwparams;
206 * struct passwd* _XGetpwnam(const char *name, _Xgetpwparams);
207 * struct passwd* _XGetpwuid(uid_t uid, _Xgetpwparams);
210 #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
212 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_PWDAPI)
213 # define XOS_USE_MTSAFE_PWDAPI 1
217 #if !defined(X_INCLUDE_PWD_H) || defined(_XOS_INCLUDED_PWD_H)
220 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
221 /* Use regular, unsafe API. */
222 # if defined(X_NOT_POSIX) && !defined(i386) && !defined(SYSV)
223 extern struct passwd *getpwuid(), *getpwnam();
225 typedef int _Xgetpwparams; /* dummy */
226 # define _XGetpwuid(u,p) getpwuid((u))
227 # define _XGetpwnam(u,p) getpwnam((u))
229 #elif !defined(XOS_USE_MTSAFE_PWDAPI) || defined(XNO_MTSAFE_PWDAPI)
230 /* UnixWare 2.0, or other systems with thread support but no _r API. */
237 # define _Xpw_copyPasswd(p) \
238 (memcpy(&(p).pws, (p).pwp, sizeof(struct passwd)), \
239 ((p).pws.pw_name = (p).pwbuf), \
240 ((p).len = strlen((p).pwp->pw_name)), \
241 strcpy((p).pws.pw_name, (p).pwp->pw_name), \
242 ((p).pws.pw_passwd = (p).pws.pw_name + (p).len + 1), \
243 ((p).len = strlen((p).pwp->pw_passwd)), \
244 strcpy((p).pws.pw_passwd,(p).pwp->pw_passwd), \
245 ((p).pws.pw_age = (p).pws.pw_passwd + (p).len + 1), \
246 ((p).len = strlen((p).pwp->pw_age)), \
247 strcpy((p).pws.pw_age, (p).pwp->pw_age), \
248 ((p).pws.pw_comment = (p).pws.pw_age + (p).len + 1), \
249 ((p).len = strlen((p).pwp->pw_comment)), \
250 strcpy((p).pws.pw_comment, (p).pwp->pw_comment), \
251 ((p).pws.pw_gecos = (p).pws.pw_comment + (p).len + 1), \
252 ((p).len = strlen((p).pwp->pw_gecos)), \
253 strcpy((p).pws.pw_gecos, (p).pwp->pw_gecos), \
254 ((p).pws.pw_dir = (p).pws.pw_comment + (p).len + 1), \
255 ((p).len = strlen((p).pwp->pw_dir)), \
256 strcpy((p).pws.pw_dir, (p).pwp->pw_dir), \
257 ((p).pws.pw_shell = (p).pws.pw_dir + (p).len + 1), \
258 ((p).len = strlen((p).pwp->pw_shell)), \
259 strcpy((p).pws.pw_shell, (p).pwp->pw_shell), \
260 ((p).pwp = &(p).pws), \
262 # define _XGetpwuid(u,p) \
263 ( (_Xos_processLock), \
264 (((p).pwp = getpwuid((u))) ? _Xpw_copyPasswd(p) : 0), \
265 (_Xos_processUnlock), \
267 # define _XGetpwnam(u,p) \
268 ( (_Xos_processLock), \
269 (((p).pwp = getpwnam((u))) ? _Xpw_copyPasswd(p) : 0), \
270 (_Xos_processUnlock), \
273 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
274 /* SVR4 threads, AIX 4.2.0 and earlier and OSF/1 3.2 and earlier pthreads */
275 # if defined(Lynx) && !defined(LINE_MAX)
276 # define LINE_MAX 2048 /* what Xthreads.h does */
280 char pwbuf[LINE_MAX];
282 # if defined(_POSIX_REENTRANT_FUNCTIONS) || !defined(SVR4) || defined(Lynx)
284 # define _XGetpwuid(u,p) \
285 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
286 # define _XGetpwnam(u,p) \
287 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
289 # define _XGetpwuid(u,p) \
290 ((getpwuid_r(&(p).pws,(u),(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
291 # define _XGetpwnam(u,p) \
292 ((getpwnam_r(&(p).pws,(u),(p).pwbuf,sizeof((p).pwbuf)) == -1) ? NULL : &(p).pws)
295 # define _XGetpwuid(u,p) \
296 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
297 # define _XGetpwnam(u,p) \
298 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf)) == NULL) ? NULL : &(p).pws)
301 #else /* _POSIX_THREAD_SAFE_FUNCTIONS */
302 /* Digital UNIX 4.0, but not (beta) T4.0-1 */
303 # if defined(__osf__)
304 /* OSF/1 V4.0 <pwd.h> doesn't declare the _P routines, breaking under C++. */
305 extern int _Pgetpwuid_r(uid_t, struct passwd *, char *, size_t, struct passwd **);
306 extern int _Pgetpwnam_r(const char *, struct passwd *, char *, size_t, struct passwd **);
310 char pwbuf[LINE_MAX];
313 typedef int _Xgetpwret;
314 # define _XGetpwuid(u,p) \
315 ((getpwuid_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == -1) ? \
317 # define _XGetpwnam(u,p) \
318 ((getpwnam_r((u),&(p).pws,(p).pwbuf,sizeof((p).pwbuf),&(p).pwp) == -1) ? \
320 #endif /* X_INCLUDE_PWD_H */
322 #if defined(X_INCLUDE_PWD_H) && !defined(_XOS_INCLUDED_PWD_H)
323 # define _XOS_INCLUDED_PWD_H
327 /***** <netdb.h> wrappers *****/
330 * Effective prototypes for <netdb.h> wrappers:
332 * NOTE: On systems lacking the appropriate _r functions Gethostbyname(),
333 * Gethostbyaddr(), and Getservbyname() do NOT copy the host or
336 * #define X_INCLUDE_NETDB_H
337 * #define XOS_USE_..._LOCKING
338 * #include <X11/Xos_r.h>
340 * typedef ... _Xgethostbynameparams;
341 * typedef ... _Xgetservbynameparams;
343 * struct hostent* _XGethostbyname(const char* name,_Xgethostbynameparams);
344 * struct hostent* _XGethostbyaddr(const char* addr, int len, int type,
345 * _Xgethostbynameparams);
346 * struct servent* _XGetservbyname(const char* name, const char* proto,
347 * _Xgetservbynameparams);
350 #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H) \
353 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_NETDBAPI)
354 # define XOS_USE_MTSAFE_NETDBAPI 1
358 #if !defined(X_INCLUDE_NETDB_H) || defined(_XOS_INCLUDED_NETDB_H)
361 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
362 /* Use regular, unsafe API. */
363 typedef int _Xgethostbynameparams; /* dummy */
364 typedef int _Xgetservbynameparams; /* dummy */
365 # define _XGethostbyname(h,hp) gethostbyname((h))
366 # define _XGethostbyaddr(a,al,t,hp) gethostbyaddr((a),(al),(t))
367 # define _XGetservbyname(s,p,sp) getservbyname((s),(p))
369 #elif !defined(XOS_USE_MTSAFE_NETDBAPI) || defined(XNO_MTSAFE_NETDBAPI)
370 /* UnixWare 2.0, or other systems with thread support but no _r API. */
371 /* WARNING: The h_addr_list and s_aliases values are *not* copied! */
375 char h_name[MAXHOSTNAMELEN];
376 struct hostent *hptr;
377 } _Xgethostbynameparams;
382 struct servent *sptr;
383 } _Xgetservbynameparams;
384 # define _Xg_copyHostent(hp) \
385 (memcpy(&(hp).hent, (hp).hptr, sizeof(struct hostent)), \
386 strcpy((hp).h_name, (hp).hptr->h_name), \
387 ((hp).hent.h_name = (hp).h_name), \
388 ((hp).hptr = &(hp).hent), \
390 # define _Xg_copyServent(sp) \
391 (memcpy(&(sp).sent, (sp).sptr, sizeof(struct servent)), \
392 strcpy((sp).s_name, (sp).sptr->s_name), \
393 ((sp).sent.s_name = (sp).s_name), \
394 strcpy((sp).s_proto, (sp).sptr->s_proto), \
395 ((sp).sent.s_proto = (sp).s_proto), \
396 ((sp).sptr = &(sp).sent), \
398 # define _XGethostbyname(h,hp) \
399 ((_Xos_processLock), \
400 (((hp).hptr = gethostbyname((h))) ? _Xg_copyHostent(hp) : 0), \
401 (_Xos_processUnlock), \
403 # define _XGethostbyaddr(a,al,t,hp) \
404 ((_Xos_processLock), \
405 (((hp).hptr = gethostbyaddr((a),(al),(t))) ? _Xg_copyHostent(hp) : 0), \
406 (_Xos_processUnlock), \
408 # define _XGetservbyname(s,p,sp) \
409 ((_Xos_processLock), \
410 (((sp).sptr = getservbyname((s),(p))) ? _Xg_copyServent(sp) : 0), \
411 (_Xos_processUnlock), \
414 #elif defined(XUSE_NETDB_R_API)
416 * POSIX does not specify _r equivalents for <netdb.h> API, but some
417 * vendors provide them anyway. Use them only when explicitly asked.
419 # ifdef _POSIX_REENTRANT_FUNCTIONS
420 # ifndef _POSIX_THREAD_SAFE_FUNCTIONS
421 # if defined(AIXV3) || defined(AIXV4) || defined(__osf__)
422 # define X_POSIX_THREAD_SAFE_FUNCTIONS 1
426 # ifdef _POSIX_THREAD_SAFE_FUNCTIONS
427 # define X_POSIX_THREAD_SAFE_FUNCTIONS 1
430 # ifndef X_POSIX_THREAD_SAFE_FUNCTIONS
435 } _Xgethostbynameparams;
439 } _Xgetservbynameparams;
440 # define _XGethostbyname(h,hp) \
441 gethostbyname_r((h),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
442 # define _XGethostbyaddr(a,al,t,hp) \
443 gethostbyaddr_r((a),(al),(t),&(hp).hent,(hp).hbuf,sizeof((hp).hbuf),&(hp).herr)
444 # define _XGetservbyname(s,p,sp) \
445 getservbyname_r((s),(p),&(sp).sent,(sp).sbuf,sizeof((sp).sbuf))
449 struct hostent_data hdata;
450 } _Xgethostbynameparams;
453 struct servent_data sdata;
454 } _Xgetservbynameparams;
455 # define _XGethostbyname(h,hp) \
456 (bzero((char*)&(hp).hdata,sizeof((hp).hdata)), \
457 ((gethostbyname_r((h),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
458 # define _XGethostbyaddr(a,al,t,hp) \
459 (bzero((char*)&(hp).hdata,sizeof((hp).hdata)), \
460 ((gethostbyaddr_r((a),(al),(t),&(hp).hent,&(hp).hdata) == -1) ? NULL : &(hp).hent))
461 # define _XGetservbyname(s,p,sp) \
462 (bzero((char*)&(sp).sdata,sizeof((sp).sdata)), \
463 ((getservbyname_r((s),(p),&(sp).sent,&(sp).sdata) == -1) ? NULL : &(sp).sent) )
465 # ifdef X_POSIX_THREAD_SAFE_FUNCTIONS
466 # undef X_POSIX_THREAD_SAFE_FUNCTIONS
470 /* The regular API is assumed to be MT-safe under POSIX. */
471 typedef int _Xgethostbynameparams; /* dummy */
472 typedef int _Xgetservbynameparams; /* dummy */
473 # define _XGethostbyname(h,hp) gethostbyname((h))
474 # define _XGethostbyaddr(a,al,t,hp) gethostbyaddr((a),(al),(t))
475 # define _XGetservbyname(s,p,sp) getservbyname((s),(p))
476 #endif /* X_INCLUDE_NETDB_H */
478 #if defined(X_INCLUDE_NETDB_H) && !defined(_XOS_INCLUDED_NETDB_H)
479 # define _XOS_INCLUDED_NETDB_H
483 /***** <dirent.h> wrappers *****/
486 * Effective prototypes for <dirent.h> wrappers:
488 * #define X_INCLUDE_DIRENT_H
489 * #define XOS_USE_..._LOCKING
490 * #include <X11/Xos_r.h>
492 * typedef ... _Xreaddirparams;
494 * struct dirent *_XReaddir(DIR *dir_pointer, _Xreaddirparams);
497 #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
498 # include <sys/types.h>
499 # if !defined(X_NOT_POSIX) || defined(SYSV) || defined(USG)
502 # include <sys/dir.h>
504 # define dirent direct
507 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_DIRENTAPI)
508 # define XOS_USE_MTSAFE_DIRENTAPI 1
512 #if !defined(X_INCLUDE_DIRENT_H) || defined(_XOS_INCLUDED_DIRENT_H)
515 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
516 /* Use regular, unsafe API. */
517 typedef int _Xreaddirparams; /* dummy */
518 # define _XReaddir(d,p) readdir(d)
520 #elif !defined(XOS_USE_MTSAFE_DIRENTAPI) || defined(XNO_MTSAFE_DIRENTAPI)
521 /* Systems with thread support but no _r API. */
523 struct dirent *result;
524 struct dirent dir_entry;
525 # ifdef _POSIX_PATH_MAX
526 char buf[_POSIX_PATH_MAX];
527 # elif defined(NAME_MAX)
534 # define _XReaddir(d,p) \
535 ( (_Xos_processLock), \
536 (((p).result = readdir((d))) ? \
537 (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
538 ((p).result = &(p).dir_entry), 0) : \
540 (_Xos_processUnlock), \
545 struct dirent *result;
546 struct dirent dir_entry;
547 # ifdef _POSIX_PATH_MAX
548 char buf[_POSIX_PATH_MAX];
549 # elif defined(NAME_MAX)
556 # if defined(AIXV3) || defined(AIXV4) || defined(_POSIX_THREAD_SAFE_FUNCTIONS)
557 /* AIX defines the draft POSIX symbol, but uses the final API. */
558 /* POSIX final API, returns (int)0 on success. */
559 # if defined(__osf__)
560 /* OSF/1 V4.0 <dirent.h> doesn't declare _Preaddir_r, breaking under C++. */
561 extern int _Preaddir_r(DIR *, struct dirent *, struct dirent **);
563 # define _XReaddir(d,p) \
564 (readdir_r((d), &((p).dir_entry), &((p).result)) ? NULL : (p).result)
565 # elif defined(_POSIX_REENTRANT_FUNCTIONS) && defined(__osf__)
567 * OSF/1 V3.2 readdir_r() will SEGV if the main program is not
568 * explicitly linked with -lc_r. The library REQUIREDLIBS don't help.
569 * Assume that if threads have been initialized we're linked properly.
571 # define _XReaddir(d,p) \
572 ( (_Xos_isThreadInitialized) ? \
573 (readdir_r((d), &((p).dir_entry)) ? NULL : &((p).dir_entry)) : \
574 ((_Xos_processLock), \
575 (((p).result = readdir((d))) ? \
576 (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
577 ((p).result = &(p).dir_entry), 0) : \
579 (_Xos_processUnlock), \
581 # elif defined(_POSIX_REENTRANT_FUNCTIONS)
582 /* POSIX draft API, returns (int)0 on success. */
583 # define _XReaddir(d,p) \
584 (readdir_r((d),&((p).dir_entry)) ? NULL : &((p).dir_entry))
586 /* Pre-POSIX API, returns non-NULL on success. */
587 # define _XReaddir(d,p) (readdir_r((d), &(p).dir_entry))
589 /* We have no idea what is going on. Fake it all using process locks. */
590 # define _XReaddir(d,p) \
591 ( (_Xos_processLock), \
592 (((p).result = readdir((d))) ? \
593 (memcpy(&((p).dir_entry), (p).result, (p).result->d_reclen), \
594 ((p).result = &(p).dir_entry), 0) : \
596 (_Xos_processUnlock), \
599 #endif /* X_INCLUDE_DIRENT_H */
601 #if defined(X_INCLUDE_DIRENT_H) && !defined(_XOS_INCLUDED_DIRENT_H)
602 # define _XOS_INCLUDED_DIRENT_H
606 /***** <unistd.h> wrappers *****/
609 * Effective prototypes for <unistd.h> wrappers:
611 * #define X_INCLUDE_UNISTD_H
612 * #define XOS_USE_..._LOCKING
613 * #include <X11/Xos_r.h>
615 * typedef ... _Xgetloginparams;
616 * typedef ... _Xttynameparams;
618 * char *_XGetlogin(_Xgetloginparams);
619 * char *_XTtyname(int, _Xttynameparams);
622 #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
623 /* <unistd.h> already included by <X11/Xos.h> */
624 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_UNISTDAPI)
625 # define XOS_USE_MTSAFE_UNISTDAPI 1
629 #if !defined(X_INCLUDE_UNISTD_H) || defined(_XOS_INCLUDED_UNISTD_H)
632 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
633 /* Use regular, unsafe API. */
634 typedef int _Xgetloginparams; /* dummy */
635 typedef int _Xttynameparams; /* dummy */
636 # define _XGetlogin(p) getlogin()
637 # define _XTtyname(f) ttyname((f))
639 #elif !defined(XOS_USE_MTSAFE_UNISTDAPI) || defined(XNO_MTSAFE_UNISTDAPI)
640 /* Systems with thread support but no _r API. */
643 # if defined(MAXLOGNAME)
644 char buf[MAXLOGNAME];
645 # elif defined(LOGIN_NAME_MAX)
646 char buf[LOGIN_NAME_MAX];
654 char buf[TTY_NAME_MAX];
655 # elif defined(_POSIX_TTY_NAME_MAX)
656 char buf[_POSIX_TTY_NAME_MAX];
657 # elif defined(_POSIX_PATH_MAX)
658 char buf[_POSIX_PATH_MAX];
664 # define _XGetlogin(p) \
665 ( (_Xos_processLock), \
666 (((p).result = getlogin()) ? \
667 (strncpy((p).buf, (p).result, sizeof((p).buf)), \
668 ((p).buf[sizeof((p).buf)-1] = '\0'), \
669 ((p).result = (p).buf), 0) : 0), \
670 (_Xos_processUnlock), \
672 #define _XTtyname(f,p) \
673 ( (_Xos_processLock), \
674 (((p).result = ttyname(f)) ? \
675 (strncpy((p).buf, (p).result, sizeof((p).buf)), \
676 ((p).buf[sizeof((p).buf)-1] = '\0'), \
677 ((p).result = (p).buf), 0) : 0), \
678 (_Xos_processUnlock), \
681 #elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(_POSIX_REENTRANT_FUNCTIONS)
684 * extern int getlogin_r(char *, size_t);
685 * extern int ttyname_r(int, char *, size_t);
688 # if defined(MAXLOGNAME)
689 char buf[MAXLOGNAME];
690 # elif defined(LOGIN_NAME_MAX)
691 char buf[LOGIN_NAME_MAX];
698 char buf[TTY_NAME_MAX];
699 # elif defined(_POSIX_TTY_NAME_MAX)
700 char buf[_POSIX_TTY_NAME_MAX];
701 # elif defined(_POSIX_PATH_MAX)
702 char buf[_POSIX_PATH_MAX];
708 # define _XGetlogin(p) (getlogin_r((p).buf, sizeof((p).buf)) ? NULL : (p).buf)
709 # define _XTtyname(f,p) \
710 (ttyname_r((f), (p).buf, sizeof((p).buf)) ? NULL : (p).buf)
715 * extern char *getlogin_r(char *, size_t);
716 * extern char *ttyname_r(int, char *, size_t);
719 # if defined(MAXLOGNAME)
720 char buf[MAXLOGNAME];
721 # elif defined(LOGIN_NAME_MAX)
722 char buf[LOGIN_NAME_MAX];
729 char buf[TTY_NAME_MAX];
730 # elif defined(_POSIX_TTY_NAME_MAX)
731 char buf[_POSIX_TTY_NAME_MAX];
732 # elif defined(_POSIX_PATH_MAX)
733 char buf[_POSIX_PATH_MAX];
739 # define _XGetlogin(p) getlogin_r((p).buf, sizeof((p).buf))
740 # define _XTtyname(f,p) ttyname_r((f), (p).buf, sizeof((p).buf))
741 #endif /* X_INCLUDE_UNISTD_H */
743 #if defined(X_INCLUDE_UNISTD_H) && !defined(_XOS_INCLUDED_UNISTD_H)
744 # define _XOS_INCLUDED_UNISTD_H
748 /***** <string.h> wrappers *****/
751 * Effective prototypes for <string.h> wrappers:
753 * #define X_INCLUDE_STRING_H
754 * #define XOS_USE_..._LOCKING
755 * #include <X11/Xos_r.h>
757 * typedef ... _Xstrtokparams;
759 * char *_XStrtok(char *, const char*, _Xstrtokparams);
762 #if defined(X_INCLUDE_STRING_H) && !defined(_XOS_INCLUDED_STRING_H)
763 /* <string.h> has already been included by <X11/Xos.h> */
764 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_STRINGAPI)
765 # define XOS_USE_MTSAFE_STRINGAPI 1
769 #if !defined(X_INCLUDE_STRING_H) || defined(_XOS_INCLUDED_STRING_H)
772 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
773 /* Use regular, unsafe API. */
774 typedef int _Xstrtokparams; /* dummy */
775 # define _XStrtok(s1,s2,p) strtok((s1),(s2))
777 #elif !defined(XOS_USE_MTSAFE_STRINGAPI) || defined(XNO_MTSAFE_STRINGAPI)
778 /* Systems with thread support but no _r API. */
779 typedef char *_Xstrtokparams;
780 # define _XStrtok(s1,s2,p) \
781 ( (_Xos_processLock), \
782 ((p) = strtok((s1),(s2))), \
783 (_Xos_processUnlock), \
787 /* POSIX or pre-POSIX API. */
788 typedef char * _Xstrtokparams;
789 # define _XStrtok(s1,s2,p) strtok_r((s1),(s2),&(p))
790 #endif /* X_INCLUDE_STRING_H */
793 /***** <time.h> wrappers *****/
796 * Effective prototypes for <time.h> wrappers:
798 * #define X_INCLUDE_TIME_H
799 * #define XOS_USE_..._LOCKING
800 * #include <X11/Xos_r.h>
802 * typedef ... _Xatimeparams;
803 * typedef ... _Xctimeparams;
804 * typedef ... _Xgtimeparams;
805 * typedef ... _Xltimeparams;
807 * char *_XAsctime(const struct tm *, _Xatimeparams);
808 * char *_XCtime(const time_t *, _Xctimeparams);
809 * struct tm *_XGmtime(const time_t *, _Xgtimeparams);
810 * struct tm *_XLocaltime(const time_t *, _Xltimeparams);
813 #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
815 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_TIMEAPI)
816 # define XOS_USE_MTSAFE_TIMEAPI 1
820 #if !defined(X_INCLUDE_TIME_H) || defined(_XOS_INCLUDED_TIME_H)
823 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
824 /* Use regular, unsafe API. */
825 typedef int _Xatimeparams; /* dummy */
826 # define _XAsctime(t,p) asctime((t))
827 typedef int _Xctimeparams; /* dummy */
828 # define _XCtime(t,p) ctime((t))
829 typedef int _Xgtimeparams; /* dummy */
830 # define _XGmtime(t,p) gmtime((t))
831 typedef int _Xltimeparams; /* dummy */
832 # define _XLocaltime(t,p) localtime((t))
834 #elif !defined(XOS_USE_MTSAFE_TIMEAPI) || defined(XNO_MTSAFE_TIMEAPI)
835 /* Systems with thread support but no _r API. */
843 } _Xctimeparams, _Xatimeparams;
847 } _Xgtimeparams, _Xltimeparams;
848 # define _XAsctime(t,p) \
849 ( (_Xos_processLock), \
850 (((p).result = asctime((t))) ? \
851 (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
853 (_Xos_processUnlock), \
855 # define _XCtime(t,p) \
856 ( (_Xos_processLock), \
857 (((p).result = ctime((t))) ? \
858 (strncpy((p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
860 (_Xos_processUnlock), \
862 # define _XGmtime(t,p) \
863 ( (_Xos_processLock), \
864 (((p).result = gmtime(t)) ? \
865 (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
867 (_Xos_processUnlock), \
869 # define _XLocaltime(t,p) \
870 ( (_Xos_processLock), \
871 (((p).result = localtime(t)) ? \
872 (memcpy(&(p).buf, (p).result, sizeof((p).buf)), (p).result = &(p).buf) : \
874 (_Xos_processUnlock), \
877 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (defined(__osf__) || defined(hpV4))
878 /* Returns (int)0 on success. OSF/1 v3.2, HP/UX 10
880 * extern int asctime_r(const struct tm *timeptr, char *buffer, int buflen);
881 * extern int ctime_r(const time_t *timer, char *buffer, int buflen);
882 * extern int gmtime_r(const time_t *timer, struct tm *result);
883 * extern int localtime_r(const time_t *timer, struct tm *result);
886 typedef char _Xatimeparams[TIMELEN];
887 typedef char _Xctimeparams[TIMELEN];
889 typedef char _Xatimeparams[26];
890 typedef char _Xctimeparams[26];
892 typedef struct tm _Xgtimeparams;
893 typedef struct tm _Xltimeparams;
894 # define _XAsctime(t,p) (asctime_r((t),(p),sizeof((p))) ? NULL : (p))
895 # define _XCtime(t,p) (ctime_r((t),(p),sizeof((p))) ? NULL : (p))
896 # define _XGmtime(t,p) (gmtime_r((t),&(p)) ? NULL : &(p))
897 # define _XLocaltime(t,p) (localtime_r((t),&(p)) ? NULL : &(p))
899 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && defined(sun)
900 /* Returns NULL on failure. Solaris 2.5
902 * extern char *asctime_r(const struct tm *tm,char *buf, int buflen);
903 * extern char *ctime_r(const time_t *clock, char *buf, int buflen);
904 * extern struct tm *gmtime_r(const time_t *clock, struct tm *res);
905 * extern struct tm *localtime_r(const time_t *clock, struct tm *res);
908 typedef char _Xatimeparams[TIMELEN];
909 typedef char _Xctimeparams[TIMELEN];
911 typedef char _Xatimeparams[26];
912 typedef char _Xctimeparams[26];
914 typedef struct tm _Xgtimeparams;
915 typedef struct tm _Xltimeparams;
916 # define _XAsctime(t,p) asctime_r((t),(p),sizeof((p)))
917 # define _XCtime(t,p) ctime_r((t),(p),sizeof((p)))
918 # define _XGmtime(t,p) gmtime_r((t),&(p))
919 # define _XLocaltime(t,p) localtime_r((t),&(p))
921 #else /* defined(_POSIX_THREAD_SAFE_FUNCTIONS) */
922 /* POSIX final API. OSF/1 v4.0, AIX, etc.
924 * extern char *asctime_r(const struct tm *timeptr, char *buffer);
925 * extern char *ctime_r(const time_t *timer, char *buffer);
926 * extern struct tm *gmtime_r(const time_t *timer, struct tm *result);
927 * extern struct tm *localtime_r(const time_t *timer, struct tm *result);
929 # if defined(__osf__)
930 /* OSF/1 V4.0 <time.h> doesn't declare the _P routines, breaking under C++. */
931 extern char *_Pasctime_r(const struct tm *, char *);
932 extern char *_Pctime_r(const time_t *, char *);
933 extern struct tm *_Plocaltime_r(const time_t *, struct tm *);
936 typedef char _Xatimeparams[TIMELEN];
937 typedef char _Xctimeparams[TIMELEN];
939 typedef char _Xatimeparams[26];
940 typedef char _Xctimeparams[26];
942 typedef struct tm _Xgtimeparams;
943 typedef struct tm _Xltimeparams;
944 # define _XAsctime(t,p) asctime_r((t),(p))
945 # define _XCtime(t,p) ctime_r((t),(p))
946 # define _XGmtime(t,p) gmtime_r((t),&(p))
947 # define _XLocaltime(t,p) localtime_r((t),&(p))
948 #endif /* X_INCLUDE_TIME_H */
950 #if defined(X_INCLUDE_TIME_H) && !defined(_XOS_INCLUDED_TIME_H)
951 # define _XOS_INCLUDED_TIME_H
955 /***** <grp.h> wrappers *****/
958 * Effective prototypes for <grp.h> wrappers:
960 * NOTE: On systems lacking appropriate _r functions Getgrgid() and
961 * Getgrnam() do NOT copy the list of group members!
963 * Remember that fgetgrent(), setgrent(), getgrent(), and endgrent()
964 * are not included in POSIX.
966 * #define X_INCLUDE_GRP_H
967 * #define XOS_USE_..._LOCKING
968 * #include <X11/Xos_r.h>
970 * typedef ... _Xgetgrparams;
972 * struct group *_XGetgrgid(gid_t, _Xgetgrparams);
973 * struct group *_XGetgrnam(const char *, _Xgetgrparams);
976 #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
978 # if defined(XUSE_MTSAFE_API) || defined(XUSE_MTSAFE_GRPAPI)
979 # define XOS_USE_MTSAFE_GRPAPI 1
983 #if !defined(X_INCLUDE_GRP_H) || defined(_XOS_INCLUDED_GRP_H)
986 #elif !defined(XTHREADS) && !defined(X_FORCE_USE_MTSAFE_API)
987 /* Use regular, unsafe API. */
988 typedef int _Xgetgrparams; /* dummy */
989 #define _XGetgrgid(g,p) getgrgid((g))
990 #define _XGetgrnam(n,p) getgrnam((n))
992 #elif !defined(XOS_USE_MTSAFE_GRPAPI) || defined(XNO_MTSAFE_GRPAPI)
993 /* Systems with thread support but no _r API. UnixWare 2.0. */
997 char buf[LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
999 char buf[1024]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1005 /* Copy the gr_passwd field too. */
1006 # define _Xgrp_copyGroup(p) \
1007 ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
1008 ((p).grp.gr_name = (p).buf), \
1009 ((p).len = strlen((p).pgrp->gr_name)), \
1010 strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
1011 ((p).grp.gr_passwd = (p).grp.gr_name + (p).len + 1), \
1012 ((p).pgrp = &(p).grp), \
1015 # define _Xgrp_copyGroup(p) \
1016 ( memcpy(&(p).grp, (p).pgrp, sizeof(struct group)), \
1017 ((p).grp.gr_name = (p).buf), \
1018 strcpy((p).grp.gr_name, (p).pgrp->gr_name), \
1019 ((p).pgrp = &(p).grp), \
1022 #define _XGetgrgid(g,p) \
1023 ( (_Xos_processLock), \
1024 (((p).pgrp = getgrgid((g))) ? _Xgrp_copyGroup(p) : 0), \
1025 (_Xos_processUnlock), \
1027 #define _XGetgrnam(n,p) \
1028 ( (_Xos_processLock), \
1029 (((p).pgrp = getgrnam((n))) ? _Xgrp_copyGroup(p) : 0), \
1030 (_Xos_processUnlock), \
1033 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS) && (defined(sun) || defined(__osf__))
1034 /* Non-POSIX API. Solaris, DEC v3.2.
1036 * extern struct group *getgrgid_r(gid_t, struct group *, char *, int);
1037 * extern struct group *getgrnam_r(const char *, struct group *, char *, int);
1042 char buf[LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1044 char buf[1024]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1047 #define _XGetgrgid(g,p) getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf))
1048 #define _XGetgrnam(n,p) getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf))
1050 #elif !defined(_POSIX_THREAD_SAFE_FUNCTIONS)
1051 /* Non-POSIX API. HP/UX 10, AIX 4.
1053 * extern int getgrgid_r(gid_t, struct group *, char *, int);
1054 * extern int getgrnam_r(const char *, struct group *, char *, int);
1059 char buf[LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1061 char buf[1024]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1064 #define _XGetgrgid(g,p) \
1065 ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
1066 #define _XGetgrnam(n,p) \
1067 ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf)) ? NULL : &(p).grp))
1070 /* POSIX final API. DEC v4.0, IRIX 6.2.
1072 * int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
1073 * int getgrnam_r(const char *, struct group *, char *, size_t, struct group **);
1075 # if defined(__osf__)
1076 /* OSF/1 V4.0 <grp.h> doesn't declare the _P routines, breaking under C++. */
1077 extern int _Pgetgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
1078 extern int _Pgetgrnam_r(const char *, struct group *, char *, size_t, struct group **);
1083 char buf[LINE_MAX]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1085 char buf[1024]; /* Should be sysconf(_SC_GETGR_R_SIZE_MAX)? */
1087 struct group *result;
1090 #define _XGetgrgid(g,p) \
1091 ((getgrgid_r((g), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
1093 #define _XGetgrnam(n,p) \
1094 ((getgrnam_r((n), &(p).grp, (p).buf, sizeof((p).buf), &(p).result) ? \
1098 #if defined(X_INCLUDE_GRP_H) && !defined(_XOS_INCLUDED_GRP_H)
1099 # define _XOS_INCLUDED_GRP_H
1104 } /* Close scope of 'extern "C"' declaration which encloses file. */