tweak comment
[coreutils.git] / lib / xgethostname.c
blob1139a1b19c2703492951147671652f56c4bc249d
1 /* xgethostname.c -- return current hostname with unlimited length
2 Copyright (C) 1992, 1996 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* written by Jim Meyering */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <sys/types.h>
26 #include <errno.h>
27 #ifndef errno
28 extern int errno;
29 #endif
31 #include "error.h"
33 #ifndef EXIT_FAILURE
34 # define EXIT_FAILURE 1
35 #endif
37 int gethostname ();
38 char *xmalloc ();
39 char *xrealloc ();
41 #ifndef INITIAL_HOSTNAME_LENGTH
42 # define INITIAL_HOSTNAME_LENGTH 34
43 #endif
45 char *
46 xgethostname ()
48 char *hostname;
49 size_t size;
50 int err;
52 size = INITIAL_HOSTNAME_LENGTH;
53 hostname = xmalloc (size);
54 while (1)
56 /* Use size - 2 here rather than size - 1 to work around the bug
57 in SunOS5.5's gethostname whereby it NUL-terminates HOSTNAME
58 even when the name is longer than the supplied buffer. */
59 int k = size - 2;
61 errno = 0;
62 hostname[k] = '\0';
63 err = gethostname (hostname, size);
64 if (err == 0 && hostname[k] == '\0')
65 break;
66 #ifdef ENAMETOOLONG
67 else if (err != 0 && errno != ENAMETOOLONG && errno != 0)
68 error (EXIT_FAILURE, errno, "gethostname");
69 #endif
70 size *= 2;
71 hostname = xrealloc (hostname, size);
74 return hostname;