libc_r.h 3.8 KB

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