*_r -functions

From: Date: Sun, 28 Nov 1999 02:24:00 +0000
Subject: *_r -functions
Groups: php.version4 
Request: Send a blank email to php-version4+get-6984@lists.php.net to get a copy of this message
Could someone with more knowledge please check that I have understood the idea right? I thought it better to ask before committing anything and breaking all the hell loose in repository. Besides, after looking mysql's source for some hints, it came clear that there are 2 quite different apis for even this one function (and then, the HPUX-way...), which I should have taken care of. I'm waiting in horror what situation is with getproto*, getserv* etc. - Jouni --- php4/configure.in Sat Nov 27 20:14:44 1999 +++ php4t/configure.in Sat Nov 27 23:17:34 1999 @@ -297,6 +297,7 @@ cuserid \ flock \ gcvt \ +gethostbyname_r \ getlogin \ gethostbyaddr \ gettimeofday \ --- php4/reentrancy.c Sun Nov 28 03:33:27 1999 +++ php4t/reentrancy.c Sun Nov 28 03:41:44 1999 @@ -25,6 +25,7 @@ CTIME_R, ASCTIME_R, GMTIME_R, + HOSTBYNAME_R, NUMBER_OF_LOCKS }; @@ -113,6 +114,25 @@ local_unlock(GMTIME_R); return p_tm; +} + +#endif + +#if !defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYNAME) + +int gethostbyname_r (const char *host, struct hostent *hostbuf, char *tmphostbuf, + size_t tmphostbuflen, struct hostent **hostp, int *hosterr) +{ + struct hostent *tmp; + + local_lock(HOSTBYNAME_R); + + tmp = gethostbyname(host); + memcpy(hostp, &tmp, sizeof(struct hostent)); + + local_unlock(HOSTBYNAME_R); + + return 0; } #endif --- php4/php_reentrancy.h Sat Nov 27 19:11:20 1999 +++ php4t/php_reentrancy.h Sun Nov 28 03:43:38 1999 @@ -23,6 +23,7 @@ #include "php.h" #include <time.h> +#include <netdb.h> /* currently, PHP does not check for these functions, but assumes that they are available on all systems. */ @@ -31,7 +32,14 @@ #define HAVE_GMTIME 1 #define HAVE_ASCTIME 1 #define HAVE_CTIME 1 +#define HAVE_GETHOSTBYNAME 1 + +#if !defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYNAME) +#define PHP_NEED_REENTRANCY 1 +int gethostbyname_r (const char *host, struct hostent *hostbuf, char *tmphostbuf, + size_t tmphostbuflen, struct hostent **hostp, int *hosterr); +#endif #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME) #define PHP_NEED_REENTRANCY 1 --- php4/ext/standard/dns.c Fri Oct 1 19:42:15 1999 +++ php4t/ext/standard/dns.c Sun Nov 28 01:50:30 1999 @@ -38,6 +38,7 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> +#include <errno.h> #ifdef _OSD_POSIX #undef STATUS #undef T_UNSPEC @@ -144,17 +145,28 @@ char *_php3_gethostbyname(char *name) { - struct hostent *hp; + struct hostent hostbuf, *hp; struct in_addr in; + size_t buflen = 1024; + char *tmpbuf; + int result, herror; + + tmpbuf = (char *)emalloc(buflen); - hp = gethostbyname(name); - if (!hp || !hp->h_addr_list) { + while ((result = gethostbyname_r(name, &hostbuf, tmpbuf, buflen, &hp, &herror)) == ERANGE) { + buflen *= 2; + tmpbuf = (char *)erealloc(tmpbuf, buflen); + } + + if (result || !hp || !hp->h_addr_list) { #if DEBUG php_error(E_WARNING, "Unable to resolve %s\n", name); #endif return estrdup(name); } + memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr)); + efree(tmpbuf); return estrdup(inet_ntoa(in)); }

« previous php.version4 (#6984) next »