xref: /dflybsd-src/contrib/cvs-1.12/lib/xgethostname.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* xgethostname.c -- return current hostname with unlimited length
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino    Copyright (C) 1992, 1996, 2000, 2001, 2003, 2004, 2005 Free Software
4*86d7f5d3SJohn Marino    Foundation, Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
7*86d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
8*86d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
9*86d7f5d3SJohn Marino    any later version.
10*86d7f5d3SJohn Marino 
11*86d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*86d7f5d3SJohn Marino    GNU General Public License for more details.
15*86d7f5d3SJohn Marino 
16*86d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
17*86d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
18*86d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19*86d7f5d3SJohn Marino 
20*86d7f5d3SJohn Marino /* written by Jim Meyering */
21*86d7f5d3SJohn Marino 
22*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
23*86d7f5d3SJohn Marino # include <config.h>
24*86d7f5d3SJohn Marino #endif
25*86d7f5d3SJohn Marino 
26*86d7f5d3SJohn Marino /* Specification.  */
27*86d7f5d3SJohn Marino #include "xgethostname.h"
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino #include <stdlib.h>
30*86d7f5d3SJohn Marino #include <errno.h>
31*86d7f5d3SJohn Marino 
32*86d7f5d3SJohn Marino #if HAVE_UNISTD_H
33*86d7f5d3SJohn Marino # include <unistd.h>
34*86d7f5d3SJohn Marino #endif
35*86d7f5d3SJohn Marino 
36*86d7f5d3SJohn Marino #include "xalloc.h"
37*86d7f5d3SJohn Marino 
38*86d7f5d3SJohn Marino #ifndef ENAMETOOLONG
39*86d7f5d3SJohn Marino # define ENAMETOOLONG 0
40*86d7f5d3SJohn Marino #endif
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino #ifndef INITIAL_HOSTNAME_LENGTH
43*86d7f5d3SJohn Marino # define INITIAL_HOSTNAME_LENGTH 34
44*86d7f5d3SJohn Marino #endif
45*86d7f5d3SJohn Marino 
46*86d7f5d3SJohn Marino /* Return the current hostname in malloc'd storage.
47*86d7f5d3SJohn Marino    If malloc fails, exit.
48*86d7f5d3SJohn Marino    Upon any other failure, return NULL and set errno.  */
49*86d7f5d3SJohn Marino char *
xgethostname(void)50*86d7f5d3SJohn Marino xgethostname (void)
51*86d7f5d3SJohn Marino {
52*86d7f5d3SJohn Marino   char *hostname = NULL;
53*86d7f5d3SJohn Marino   size_t size = INITIAL_HOSTNAME_LENGTH;
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino   while (1)
56*86d7f5d3SJohn Marino     {
57*86d7f5d3SJohn Marino       /* Use SIZE_1 here rather than SIZE to work around the bug in
58*86d7f5d3SJohn Marino 	 SunOS 5.5's gethostname whereby it NUL-terminates HOSTNAME
59*86d7f5d3SJohn Marino 	 even when the name is as long as the supplied buffer.  */
60*86d7f5d3SJohn Marino       size_t size_1;
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino       hostname = x2realloc (hostname, &size);
63*86d7f5d3SJohn Marino       size_1 = size - 1;
64*86d7f5d3SJohn Marino       hostname[size_1 - 1] = '\0';
65*86d7f5d3SJohn Marino       errno = 0;
66*86d7f5d3SJohn Marino 
67*86d7f5d3SJohn Marino       if (gethostname (hostname, size_1) == 0)
68*86d7f5d3SJohn Marino 	{
69*86d7f5d3SJohn Marino 	  if (! hostname[size_1 - 1])
70*86d7f5d3SJohn Marino 	    break;
71*86d7f5d3SJohn Marino 	}
72*86d7f5d3SJohn Marino       else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL
73*86d7f5d3SJohn Marino 	       /* OSX/Darwin does this when the buffer is not large enough */
74*86d7f5d3SJohn Marino 	       && errno != ENOMEM)
75*86d7f5d3SJohn Marino 	{
76*86d7f5d3SJohn Marino 	  int saved_errno = errno;
77*86d7f5d3SJohn Marino 	  free (hostname);
78*86d7f5d3SJohn Marino 	  errno = saved_errno;
79*86d7f5d3SJohn Marino 	  return NULL;
80*86d7f5d3SJohn Marino 	}
81*86d7f5d3SJohn Marino     }
82*86d7f5d3SJohn Marino 
83*86d7f5d3SJohn Marino   return hostname;
84*86d7f5d3SJohn Marino }
85