LCOV - code coverage report
Current view: top level - shishi/gl - xgethostname.c (source / functions) Hit Total Coverage
Test: GNU Shishi Lines: 11 17 64.7 %
Date: 2010-05-20 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 12 16.7 %

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

Generated by: LCOV version 1.8