reentrancy.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <sys/types.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #ifdef HAVE_DIRENT_H
  20. #include <dirent.h>
  21. #endif
  22. #include "php_reentrancy.h"
  23. #include "ext/standard/php_rand.h" /* for PHP_RAND_MAX */
  24. enum {
  25. LOCALTIME_R,
  26. CTIME_R,
  27. ASCTIME_R,
  28. GMTIME_R,
  29. NUMBER_OF_LOCKS
  30. };
  31. #if defined(PHP_NEED_REENTRANCY)
  32. #include <TSRM.h>
  33. static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS];
  34. #define local_lock(x) tsrm_mutex_lock(reentrant_locks[x])
  35. #define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x])
  36. #else
  37. #define local_lock(x)
  38. #define local_unlock(x)
  39. #endif
  40. #if defined(PHP_IRIX_TIME_R)
  41. #define HAVE_CTIME_R 1
  42. #define HAVE_ASCTIME_R 1
  43. PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
  44. {
  45. if (ctime_r(clock, buf) == buf)
  46. return (buf);
  47. return (NULL);
  48. }
  49. PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
  50. {
  51. if (asctime_r(tm, buf) == buf)
  52. return (buf);
  53. return (NULL);
  54. }
  55. #endif
  56. #if defined(PHP_HPUX_TIME_R)
  57. #define HAVE_LOCALTIME_R 1
  58. #define HAVE_CTIME_R 1
  59. #define HAVE_ASCTIME_R 1
  60. #define HAVE_GMTIME_R 1
  61. PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
  62. {
  63. if (localtime_r(timep, p_tm) == 0)
  64. return (p_tm);
  65. return (NULL);
  66. }
  67. PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
  68. {
  69. if (ctime_r(clock, buf, 26) != -1)
  70. return (buf);
  71. return (NULL);
  72. }
  73. PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
  74. {
  75. if (asctime_r(tm, buf, 26) != -1)
  76. return (buf);
  77. return (NULL);
  78. }
  79. PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
  80. {
  81. if (gmtime_r(timep, p_tm) == 0)
  82. return (p_tm);
  83. return (NULL);
  84. }
  85. #endif
  86. #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
  87. PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
  88. {
  89. struct tm *tmp;
  90. local_lock(LOCALTIME_R);
  91. tmp = localtime(timep);
  92. if (tmp) {
  93. memcpy(p_tm, tmp, sizeof(struct tm));
  94. tmp = p_tm;
  95. }
  96. local_unlock(LOCALTIME_R);
  97. return tmp;
  98. }
  99. #endif
  100. #if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME)
  101. PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
  102. {
  103. char *tmp;
  104. local_lock(CTIME_R);
  105. tmp = ctime(clock);
  106. if (tmp) {
  107. strcpy(buf, tmp);
  108. tmp = buf;
  109. }
  110. local_unlock(CTIME_R);
  111. return tmp;
  112. }
  113. #endif
  114. #if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME)
  115. PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
  116. {
  117. char *tmp;
  118. local_lock(ASCTIME_R);
  119. tmp = asctime(tm);
  120. if (tmp) {
  121. strcpy(buf, tmp);
  122. tmp = buf;
  123. }
  124. local_unlock(ASCTIME_R);
  125. return tmp;
  126. }
  127. #endif
  128. #if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME)
  129. PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
  130. {
  131. struct tm *tmp;
  132. local_lock(GMTIME_R);
  133. tmp = gmtime(timep);
  134. if (tmp) {
  135. memcpy(p_tm, tmp, sizeof(struct tm));
  136. tmp = p_tm;
  137. }
  138. local_unlock(GMTIME_R);
  139. return tmp;
  140. }
  141. #endif
  142. #if defined(PHP_NEED_REENTRANCY)
  143. void reentrancy_startup(void)
  144. {
  145. int i;
  146. for (i = 0; i < NUMBER_OF_LOCKS; i++) {
  147. reentrant_locks[i] = tsrm_mutex_alloc();
  148. }
  149. }
  150. void reentrancy_shutdown(void)
  151. {
  152. int i;
  153. for (i = 0; i < NUMBER_OF_LOCKS; i++) {
  154. tsrm_mutex_free(reentrant_locks[i]);
  155. }
  156. }
  157. #endif
  158. #ifndef HAVE_STRTOK_R
  159. /*
  160. * Copyright (c) 1998 Softweyr LLC. All rights reserved.
  161. *
  162. * strtok_r, from Berkeley strtok
  163. * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
  164. *
  165. * Copyright (c) 1988, 1993
  166. * The Regents of the University of California. All rights reserved.
  167. *
  168. * Redistribution and use in source and binary forms, with or without
  169. * modification, are permitted provided that the following conditions
  170. * are met:
  171. *
  172. * 1. Redistributions of source code must retain the above copyright
  173. * notices, this list of conditions and the following disclaimer.
  174. *
  175. * 2. Redistributions in binary form must reproduce the above copyright
  176. * notices, this list of conditions and the following disclaimer in the
  177. * documentation and/or other materials provided with the distribution.
  178. *
  179. * 3. All advertising materials mentioning features or use of this software
  180. * must display the following acknowledgement:
  181. *
  182. * This product includes software developed by Softweyr LLC, the
  183. * University of California, Berkeley, and its contributors.
  184. *
  185. * 4. Neither the name of the University nor the names of its contributors
  186. * may be used to endorse or promote products derived from this software
  187. * without specific prior written permission.
  188. *
  189. * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
  190. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  191. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  192. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
  193. * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  194. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  195. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  196. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  197. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  198. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  199. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  200. */
  201. #include <stddef.h>
  202. PHPAPI char *
  203. php_strtok_r(char *s, const char *delim, char **last)
  204. {
  205. char *spanp;
  206. int c, sc;
  207. char *tok;
  208. if (s == NULL && (s = *last) == NULL)
  209. {
  210. return NULL;
  211. }
  212. /*
  213. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  214. */
  215. cont:
  216. c = *s++;
  217. for (spanp = (char *)delim; (sc = *spanp++) != 0; )
  218. {
  219. if (c == sc)
  220. {
  221. goto cont;
  222. }
  223. }
  224. if (c == 0) /* no non-delimiter characters */
  225. {
  226. *last = NULL;
  227. return NULL;
  228. }
  229. tok = s - 1;
  230. /*
  231. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  232. * Note that delim must have one NUL; we stop if we see that, too.
  233. */
  234. for (;;)
  235. {
  236. c = *s++;
  237. spanp = (char *)delim;
  238. do
  239. {
  240. if ((sc = *spanp++) == c)
  241. {
  242. if (c == 0)
  243. {
  244. s = NULL;
  245. }
  246. else
  247. {
  248. char *w = s - 1;
  249. *w = '\0';
  250. }
  251. *last = s;
  252. return tok;
  253. }
  254. }
  255. while (sc != 0);
  256. }
  257. /* NOTREACHED */
  258. }
  259. #endif