Declare strdup only if it's not defined.
[gnulib.git] / lib / canon-host.c
1 /* Host name canonicalization
2
3    Copyright (C) 1995, 1999, 2000 Free Software Foundation, Inc.
4
5    Written by Miles Bader <miles@gnu.ai.mit.edu>
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 # include <string.h>
31 #endif
32 #ifdef HAVE_NETDB_H
33 # include <netdb.h>
34 #endif
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.h>
37 #endif
38
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #ifdef HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45
46 #ifndef strdup
47 char *strdup ();
48 #endif
49 void free ();
50
51 /* Returns the canonical hostname associated with HOST (allocated in a static
52    buffer), or 0 if it can't be determined.  */
53 char *
54 canon_host (const char *host)
55 {
56 #ifdef HAVE_GETHOSTBYNAME
57   struct hostent *he = gethostbyname (host);
58
59   if (he)
60     {
61 # ifdef HAVE_GETHOSTBYADDR
62       char *addr = 0;
63
64       /* Try and get an ascii version of the numeric host address.  */
65       switch (he->h_addrtype)
66         {
67 #  ifdef HAVE_INET_NTOA
68         case AF_INET:
69           addr = inet_ntoa (*(struct in_addr *) he->h_addr);
70           break;
71 #  endif /* HAVE_INET_NTOA */
72         }
73
74       if (addr && strcmp (he->h_name, addr) == 0)
75         {
76           /* gethostbyname has returned a string representation of the IP
77              address, for example, "127.0.0.1".  So now, look up the host
78              name via the address.  Although it may seem reasonable to look
79              up the host name via the address, we must not pass `he->h_addr'
80              directly to gethostbyaddr because on some systems he->h_addr
81              is located in a static library buffer that is reused in the
82              gethostbyaddr call.  Make a copy and use that instead.  */
83           char *h_addr_copy = strdup (he->h_addr);
84           if (h_addr_copy == NULL)
85             he = NULL;
86           else
87             {
88               he = gethostbyaddr (h_addr_copy, he->h_length, he->h_addrtype);
89               free (h_addr_copy);
90             }
91         }
92 # endif /* HAVE_GETHOSTBYADDR */
93
94       if (he)
95         return (char *) (he->h_name);
96     }
97 #endif /* HAVE_GETHOSTBYNAME */
98   return 0;
99 }
100
101 #ifdef TEST_CANON_HOST
102 int
103 main (int argc, char **argv)
104 {
105   int i;
106   for (i = 1; i < argc; i++)
107     {
108       char *s = canon_host (argv[i]);
109       printf ("%s: %s\n", argv[i], (s ? s : "<undef>"));
110     }
111   exit (0);
112 }
113 #endif /* TEST_CANON_HOST */