indent cpp-directives
[gnulib.git] / lib / xgethostname.c
1 /* xgethostname.c -- return current hostname with unlimited length
2    Copyright (C) 1992 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* Written by Jim Meyering, meyering@comco.com */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <sys/types.h>
25
26 int gethostname ();
27 char *xmalloc ();
28 char *xrealloc ();
29
30 #ifndef INITIAL_HOSTNAME_LENGTH
31 # define INITIAL_HOSTNAME_LENGTH 33
32 #endif
33
34 char *
35 xgethostname ()
36 {
37   char *hostname;
38   size_t size;
39   int err;
40
41   size = INITIAL_HOSTNAME_LENGTH;
42   hostname = xmalloc (size);
43   while (1)
44     {
45       hostname[size - 1] = '\0';
46       err = gethostname (hostname, size);
47       if (err == 0 && hostname[size - 1] == '\0')
48         break;
49       size *= 2;
50       hostname = xrealloc (hostname, size);
51     }
52
53   return hostname;
54 }