123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #include <sys/types.h>
- #include <string.h>
- #include <errno.h>
- #ifdef HAVE_DIRENT_H
- #include <dirent.h>
- #endif
- #include "php_reentrancy.h"
- #include "ext/standard/php_rand.h"
- enum {
- LOCALTIME_R,
- CTIME_R,
- ASCTIME_R,
- GMTIME_R,
- NUMBER_OF_LOCKS
- };
- #if defined(PHP_NEED_REENTRANCY)
- #include <TSRM.h>
- static MUTEX_T reentrant_locks[NUMBER_OF_LOCKS];
- #define local_lock(x) tsrm_mutex_lock(reentrant_locks[x])
- #define local_unlock(x) tsrm_mutex_unlock(reentrant_locks[x])
- #else
- #define local_lock(x)
- #define local_unlock(x)
- #endif
- #if defined(PHP_IRIX_TIME_R)
- #define HAVE_CTIME_R 1
- #define HAVE_ASCTIME_R 1
- PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
- {
- if (ctime_r(clock, buf) == buf)
- return (buf);
- return (NULL);
- }
- PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
- {
- if (asctime_r(tm, buf) == buf)
- return (buf);
- return (NULL);
- }
- #endif
- #if defined(PHP_HPUX_TIME_R)
- #define HAVE_LOCALTIME_R 1
- #define HAVE_CTIME_R 1
- #define HAVE_ASCTIME_R 1
- #define HAVE_GMTIME_R 1
- PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
- {
- if (localtime_r(timep, p_tm) == 0)
- return (p_tm);
- return (NULL);
- }
- PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
- {
- if (ctime_r(clock, buf, 26) != -1)
- return (buf);
- return (NULL);
- }
- PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
- {
- if (asctime_r(tm, buf, 26) != -1)
- return (buf);
- return (NULL);
- }
- PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
- {
- if (gmtime_r(timep, p_tm) == 0)
- return (p_tm);
- return (NULL);
- }
- #endif
- #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
- PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)
- {
- struct tm *tmp;
- local_lock(LOCALTIME_R);
- tmp = localtime(timep);
- if (tmp) {
- memcpy(p_tm, tmp, sizeof(struct tm));
- tmp = p_tm;
- }
- local_unlock(LOCALTIME_R);
- return tmp;
- }
- #endif
- #if !defined(HAVE_CTIME_R) && defined(HAVE_CTIME)
- PHPAPI char *php_ctime_r(const time_t *clock, char *buf)
- {
- char *tmp;
- local_lock(CTIME_R);
- tmp = ctime(clock);
- if (tmp) {
- strcpy(buf, tmp);
- tmp = buf;
- }
- local_unlock(CTIME_R);
- return tmp;
- }
- #endif
- #if !defined(HAVE_ASCTIME_R) && defined(HAVE_ASCTIME)
- PHPAPI char *php_asctime_r(const struct tm *tm, char *buf)
- {
- char *tmp;
- local_lock(ASCTIME_R);
- tmp = asctime(tm);
- if (tmp) {
- strcpy(buf, tmp);
- tmp = buf;
- }
- local_unlock(ASCTIME_R);
- return tmp;
- }
- #endif
- #if !defined(HAVE_GMTIME_R) && defined(HAVE_GMTIME)
- PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
- {
- struct tm *tmp;
- local_lock(GMTIME_R);
- tmp = gmtime(timep);
- if (tmp) {
- memcpy(p_tm, tmp, sizeof(struct tm));
- tmp = p_tm;
- }
- local_unlock(GMTIME_R);
- return tmp;
- }
- #endif
- #if defined(PHP_NEED_REENTRANCY)
- void reentrancy_startup(void)
- {
- int i;
- for (i = 0; i < NUMBER_OF_LOCKS; i++) {
- reentrant_locks[i] = tsrm_mutex_alloc();
- }
- }
- void reentrancy_shutdown(void)
- {
- int i;
- for (i = 0; i < NUMBER_OF_LOCKS; i++) {
- tsrm_mutex_free(reentrant_locks[i]);
- }
- }
- #endif
- #ifndef HAVE_STRTOK_R
- #include <stddef.h>
- PHPAPI char *
- php_strtok_r(char *s, const char *delim, char **last)
- {
- char *spanp;
- int c, sc;
- char *tok;
- if (s == NULL && (s = *last) == NULL)
- {
- return NULL;
- }
-
- cont:
- c = *s++;
- for (spanp = (char *)delim; (sc = *spanp++) != 0; )
- {
- if (c == sc)
- {
- goto cont;
- }
- }
- if (c == 0)
- {
- *last = NULL;
- return NULL;
- }
- tok = s - 1;
-
- for (;;)
- {
- c = *s++;
- spanp = (char *)delim;
- do
- {
- if ((sc = *spanp++) == c)
- {
- if (c == 0)
- {
- s = NULL;
- }
- else
- {
- char *w = s - 1;
- *w = '\0';
- }
- *last = s;
- return tok;
- }
- }
- while (sc != 0);
- }
-
- }
- #endif
|