X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fcanon-host.c;h=10886ab3c820c3457ce4866a5c7b9bc272028141;hb=68dd9ac979d5c3afd72f3d2fb0d6dcda32b3c384;hp=56e84a7d7449c99abc1953822813f71dad51a8e9;hpb=244d36cf55a04be72353ae32a03ab1d2dcc83626;p=gnulib.git diff --git a/lib/canon-host.c b/lib/canon-host.c index 56e84a7d7..10886ab3c 100644 --- a/lib/canon-host.c +++ b/lib/canon-host.c @@ -1,56 +1,45 @@ /* Host name canonicalization - Copyright (C) 2005 Free Software - Foundation, Inc. + Copyright (C) 2005-2011 Free Software Foundation, Inc. Written by Derek Price . - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2, or (at - your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include "canon-host.h" -#include "getaddrinfo.h" -#include "strdup.h" - - +#include +#include /* Store the last error for the single-threaded version of this function. */ static int last_cherror; - - /* Single-threaded of wrapper for canon_host_r. After a NULL return, error - messages may be retrieved via ch_strerror(). - */ + messages may be retrieved via ch_strerror(). */ char * canon_host (const char *host) { - return canon_host_r (host, &last_cherror); + return canon_host_r (host, &last_cherror); } - - -/* Returns a malloc'd string containing the canonical hostname associated with - HOST, or NULL if a canonical name cannot be determined. On NULL return, if - CHERROR is not NULL, *CHERROR will be set to an error code as returned by - getaddrinfo(). Error codes from CHERROR may be converted to a string - suitable for error messages by ch_strerror_r() or gai_strerror(). +/* Return a malloc'd string containing the canonical hostname associated with + HOST, or NULL if a canonical name cannot be determined. On NULL return, + if CHERROR is not NULL, set *CHERROR to an error code as returned by + getaddrinfo(). Use ch_strerror_r() or gai_strerror() to convert a *CHERROR + value to a string suitable for error messages. WARNINGS HOST must be a string representation of a resolvable name for this host. @@ -66,34 +55,36 @@ canon_host (const char *host) getaddrinfo spec , RFC 1034 , & RFC 2181 for more on what this confusing - term really refers to. - */ + term really refers to. */ char * canon_host_r (char const *host, int *cherror) { - char *retval = NULL; - static struct addrinfo hints; - struct addrinfo *res = NULL; - int status; - - hints.ai_flags = AI_CANONNAME; - status = getaddrinfo (host, NULL, &hints, &res); - if (!status) + char *retval = NULL; + static struct addrinfo hints; + struct addrinfo *res = NULL; + int status; + + hints.ai_flags = AI_CANONNAME; + status = getaddrinfo (host, NULL, &hints, &res); + if (!status) { - retval = strdup (res->ai_canonname); - freeaddrinfo (res); + /* http://lists.gnu.org/archive/html/bug-coreutils/2006-09/msg00300.html + says Darwin 7.9.0 getaddrinfo returns 0 but sets + res->ai_canonname to NULL. */ + retval = strdup (res->ai_canonname ? res->ai_canonname : host); + if (!retval && cherror) + *cherror = EAI_MEMORY; + freeaddrinfo (res); } - else if (cherror) - *cherror = status; + else if (cherror) + *cherror = status; - return retval; + return retval; } - - /* Return a string describing the last error encountered by canon_host. */ const char * ch_strerror (void) { - return gai_strerror (last_cherror); + return gai_strerror (last_cherror); }