From 02eec644310f0b262270c7966f31f79239118287 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Thu, 6 Jun 1996 15:29:59 +0000 Subject: *** empty log message *** Thu Jun 6 07:32:14 1996 Miles Bader * resolv/gethnamaddr.c (struct hstorage): Make NAME field a pointer. (_gethtbyname2): Remove ALIAS variable. Initialize NAME field of SELF and TARGET to 0. Add SELF_NAME_SIZE static variable. Add loop to call gethostname until the space we allocated is enough. Use malloced strings instead of fixed size buffers. , : New includes. * sysdeps/generic/sys/socket.h (PF_INET6, AF_INET6): New macros. * sysdeps/mach/hurd/errlist.c (_sys_errlist): Add EILSEQ. (_sys_nerr): Initialize to 107. Update _HURD_ERRNOS consistency check. * stdlib/canonicalize.c (canonicalize): Use pathconf for PATH_MAX, not sysconf. * login/login.c [!PATH_MAX] (PATH_MAX): Define to be 1024 if not already defined. * sysdeps/mach/hurd/setitimer.c (timer_thread): Supply SIGCODE argument to __msg_sig_post_request. * hurd/hurdmalloc.c: Changes to bring in line with the hurd libthreads/malloc.c: (more_memory): Use assert_perror instead of MACH_CALL. "cthread_internals.h": Include removed. (realloc): Use LOG2_MIN_SIZE. (LOG2_MIN_SIZE): New macro. (realloc): Don't bother allocating a new block if the new size request fits in the old one and doesn't waste any space. Only free the old block if we successfully got a new one. [MCHECK] (struct header): New type. (union header): Only define if !MCHECK. (HEADER_SIZE, HEADER_NEXT, HEADER_FREE, HEADER_CHECK): New macros. [MCHECK] (MIN_SIZE): Add correct definition for this case. (more_memory, malloc, free, realloc): Use above macros, and add appropiate checks & frobs in MCHECK case. --- resolv/gethnamaddr.c | 60 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 14 deletions(-) (limited to 'resolv/gethnamaddr.c') diff --git a/resolv/gethnamaddr.c b/resolv/gethnamaddr.c index 18f4764428..161060ebd6 100644 --- a/resolv/gethnamaddr.c +++ b/resolv/gethnamaddr.c @@ -65,6 +65,8 @@ static char rcsid[] = "$Id$"; #include #include +#include +#include #include #include #include @@ -823,7 +825,7 @@ _gethtent() } struct hstorage { - char name[MAXHOSTNAMELEN + 1]; /* canonical name */ + char *name; /* canonical name */ char ** alp; /* address list pointer */ char * abp; /* address buffer pointer */ char * addr_list[MAXADDRS + 1]; /* address list storage */ @@ -891,20 +893,53 @@ _gethtbyname2(name, af) * interface changes to make these functions * reentrant. */ - static char * aliases[2]; - static char alias[MAXHOSTNAMELEN + 1]; - static struct hstorage self, target; + static char *aliases[2] = {0}; + static struct hstorage self = {0}, target = {0}; + static size_t self_name_size = 0; /* Allocated in self.name. */ static struct hostent ht; int found; + if (aliases[0]) + free (aliases[0]); /* Malloced in a prev call. */ aliases[0] = aliases[1] = 0; - gethostname(self.name, sizeof(self.name)); + /* Get current host name in a large enough buffer. */ + do { + errno = 0; + + if (self.name) + { + self_name_size += self_name_size; + self.name = + realloc (self.name, self_name_size); + } else { + self_name_size = 128; /* Initial guess */ + self.name = malloc (self_name_size); + } + + if (! self.name) + { + errno = ENOMEM; + return 0; + } + } while ((gethostname(self.name, self_name_size) == 0 + && !memchr (self.name, '\0', self_name_size)) + || errno == ENAMETOOLONG); + + if (errno) + /* gethostname failed, abort. */ + { + free (self.name); + self.name = 0; + } + self.alp = self.addr_list; self.abp = self.addr_buf; - strncpy(target.name, name, MAXHOSTNAMELEN); - target.name[MAXHOSTNAMELEN] = '\0'; + if (target.name) + free (target.name); + target.name = strdup (name); + target.alp = target.addr_list; target.abp = target.addr_buf; @@ -920,13 +955,10 @@ _gethtbyname2(name, af) if (strcasecmp(*cp, name) == 0) { found = 1; if (!aliases[0]) { - strcpy(target.name, - p->h_name); - strncpy(alias, name, - MAXHOSTNAMELEN); - alias[MAXHOSTNAMELEN] - = '\0'; - aliases[0] = alias; + aliases[0] = + target.name; + target.name = + strdup (p->h_name); } break; } -- cgit 1.4.1