home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / fingercl.zip / hostname.c < prev    next >
C/C++ Source or Header  |  1997-08-29  |  2KB  |  65 lines

  1. /* hostname.c -- Wrapper for gethostname(). */
  2.  
  3. /* Copyright (C) 1992  Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Finger.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU 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
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <config.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <netdb.h>
  27. #include <general.h>
  28.  
  29.  
  30. /* Return host name. Assuming the host name doesn't change between
  31.    calls, we only call gethostname() once. */
  32. char *
  33. xgethostname ()
  34. {
  35.   static char *hostname = NULL;
  36.  
  37.   /* This is just an initial guess */
  38.   static int hostname_size = 128;
  39.  
  40.   /* Deja Vu  */
  41.   if (hostname)
  42.     return hostname;
  43.  
  44.  
  45.   /* Obtain host name */
  46.   hostname = xmalloc (hostname_size);
  47.   for (;;)
  48.     {
  49.       hostname[hostname_size - 1] = 0;
  50.  
  51.       if (gethostname (hostname, hostname_size) < 0)
  52.         {
  53.           free (hostname);
  54.           hostname = NULL;
  55.           return NULL;
  56.         }
  57.  
  58.       if (!hostname[hostname_size - 1])
  59.         return hostname;
  60.  
  61.       hostname_size *= 2;
  62.       hostname = xrealloc (hostname, hostname_size);
  63.     }
  64. }
  65.