|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* libc_r.h -- macros, defines, etc. to make using reentrant libc calls */ |
|
7 /* a bit easier. This was initially done for AIX pthreads, */ |
|
8 /* but should be usable for anyone... */ |
|
9 |
|
10 /* Most of these use locally defined space instead of static library space. */ |
|
11 /* Because of this, we use the _INIT_R to declare/allocate space (stack), */ |
|
12 /* and the plain routines to actually do it..._WARNING_: avoid allocating */ |
|
13 /* memory wherever possible. Memory allocation is fairly expensive, at */ |
|
14 /* least on AIX...use arrays instead (which allocate from the stack.) */ |
|
15 /* I know the names are a bit strange, but I wanted to be fairly certain */ |
|
16 /* that we didn't have any namespace corruption...in general, the inits are */ |
|
17 /* R_<name>_INIT_R(), and the actual calls are R_<name>_R(). */ |
|
18 |
|
19 #ifndef _LIBC_R_H |
|
20 #define _LIBC_R_H |
|
21 |
|
22 /************/ |
|
23 /* strtok */ |
|
24 /************/ |
|
25 #define R_STRTOK_INIT_R() \ |
|
26 char *r_strtok_r=NULL |
|
27 |
|
28 #define R_STRTOK_R(return,source,delim) \ |
|
29 return=strtok_r(source,delim,&r_strtok_r) |
|
30 |
|
31 #define R_STRTOK_NORET_R(source,delim) \ |
|
32 strtok_r(source,delim,&r_strtok_r) |
|
33 |
|
34 /**************/ |
|
35 /* strerror */ |
|
36 /**************/ |
|
37 #define R_MAX_STRERROR_LEN_R 8192 /* Straight from limits.h */ |
|
38 |
|
39 #define R_STRERROR_INIT_R() \ |
|
40 char r_strerror_r[R_MAX_STRERROR_LEN_R] |
|
41 |
|
42 #define R_STRERROR_R(val) \ |
|
43 strerror_r(val,r_strerror_r,R_MAX_STRERROR_LEN_R) |
|
44 |
|
45 /*****************/ |
|
46 /* time things */ |
|
47 /*****************/ |
|
48 #define R_ASCTIME_INIT_R() \ |
|
49 char r_asctime_r[26] |
|
50 |
|
51 #define R_ASCTIME_R(val) \ |
|
52 asctime_r(val,r_asctime_r) |
|
53 |
|
54 #define R_CTIME_INIT_R() \ |
|
55 char r_ctime_r[26] |
|
56 |
|
57 #define R_CTIME_R(val) \ |
|
58 ctime_r(val,r_ctime_r) |
|
59 |
|
60 #define R_GMTIME_INIT_R() \ |
|
61 struct tm r_gmtime_r |
|
62 |
|
63 #define R_GMTIME_R(time) \ |
|
64 gmtime_r(time,&r_gmtime_r) |
|
65 |
|
66 #define R_LOCALTIME_INIT_R() \ |
|
67 struct tm r_localtime_r |
|
68 |
|
69 #define R_LOCALTIME_R(val) \ |
|
70 localtime_r(val,&r_localtime_r) |
|
71 |
|
72 /***********/ |
|
73 /* crypt */ |
|
74 /***********/ |
|
75 #include <crypt.h> |
|
76 #define R_CRYPT_INIT_R() \ |
|
77 CRYPTD r_cryptd_r; \ |
|
78 bzero(&r_cryptd_r,sizeof(CRYPTD)) |
|
79 |
|
80 #define R_CRYPT_R(pass,salt) \ |
|
81 crypt_r(pass,salt,&r_cryptd_r) |
|
82 |
|
83 /**************/ |
|
84 /* pw stuff */ |
|
85 /**************/ |
|
86 #define R_MAX_PW_LEN_R 1024 |
|
87 /* The following must be after the last declaration, but */ |
|
88 /* before the first bit of code... */ |
|
89 #define R_GETPWNAM_INIT_R(pw_ptr) \ |
|
90 struct passwd r_getpwnam_pw_r; \ |
|
91 char r_getpwnam_line_r[R_MAX_PW_LEN_R]; \ |
|
92 pw_ptr = &r_getpwnam_pw_r |
|
93 |
|
94 #define R_GETPWNAM_R(name) \ |
|
95 getpwnam_r(name,&r_getpwnam_pw_r,r_getpwnam_line_r,R_MAX_PW_LEN_R) |
|
96 |
|
97 /*******************/ |
|
98 /* gethost stuff */ |
|
99 /*******************/ |
|
100 #define R_GETHOSTBYADDR_INIT_R() \ |
|
101 struct hostent r_gethostbyaddr_r; \ |
|
102 struct hostent_data r_gethostbyaddr_data_r |
|
103 |
|
104 #define R_GETHOSTBYADDR_R(addr,len,type,xptr_ent) \ |
|
105 bzero(&r_gethostbyaddr_r,sizeof(struct hostent)); \ |
|
106 bzero(&r_gethostbyaddr_data_r,sizeof(struct hostent_data)); \ |
|
107 xptr_ent = &r_gethostbyaddr_r; \ |
|
108 if (gethostbyaddr_r(addr,len,type, \ |
|
109 &r_gethostbyaddr_r,&r_gethostbyaddr_data_r) == -1) { \ |
|
110 xptr_ent = NULL; \ |
|
111 } |
|
112 |
|
113 #define R_GETHOSTBYNAME_INIT_R() \ |
|
114 struct hostent r_gethostbyname_r; \ |
|
115 struct hostent_data r_gethostbyname_data_r |
|
116 |
|
117 #define R_GETHOSTBYNAME_R(name,xptr_ent) \ |
|
118 bzero(&r_gethostbyname_r,sizeof(struct hostent)); \ |
|
119 bzero(&r_gethostbyname_data_r,sizeof(struct hostent_data)); \ |
|
120 xptr_ent = &r_gethostbyname_r; \ |
|
121 if (gethostbyname_r(name, \ |
|
122 &r_gethostbyname_r,&r_gethostbyname_data_r) == -1) { \ |
|
123 xptr_ent = NULL; \ |
|
124 } |
|
125 |
|
126 #endif /* _LIBC_R_H */ |