Back to index

nagios-plugins  1.4.16
gethostname.c
Go to the documentation of this file.
00001 /* gethostname emulation for SysV and POSIX.1.
00002 
00003    Copyright (C) 1992, 2003, 2006, 2008, 2009, 2010 Free Software Foundation,
00004    Inc.
00005 
00006    This program is free software: you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 /* David MacKenzie <djm@gnu.ai.mit.edu>
00020    Windows port by Simon Josefsson <simon@josefsson.org> */
00021 
00022 #include <config.h>
00023 
00024 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
00025 /* Unix API.  */
00026 
00027 /* Specification.  */
00028 #include <unistd.h>
00029 
00030 #ifdef HAVE_UNAME
00031 # include <sys/utsname.h>
00032 #endif
00033 
00034 #include <string.h>
00035 
00036 /* Put up to LEN chars of the host name into NAME.
00037    Null terminate it if the name is shorter than LEN.
00038    Return 0 if ok, -1 if error.  */
00039 
00040 #include <stddef.h>
00041 
00042 int
00043 gethostname (char *name, size_t len)
00044 {
00045 #ifdef HAVE_UNAME
00046   struct utsname uts;
00047 
00048   if (uname (&uts) == -1)
00049     return -1;
00050   if (len > sizeof (uts.nodename))
00051     {
00052       /* More space than we need is available.  */
00053       name[sizeof (uts.nodename)] = '\0';
00054       len = sizeof (uts.nodename);
00055     }
00056   strncpy (name, uts.nodename, len);
00057 #else
00058   strcpy (name, "");            /* Hardcode your system name if you want.  */
00059 #endif
00060   return 0;
00061 }
00062 
00063 #else
00064 /* Native Windows API.  Which primitive to choose?
00065    - gethostname() requires linking with -lws2_32.
00066    - GetComputerName() does not return the right kind of hostname.
00067    - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname,
00068      but it is hard to use portably:
00069        - It requires defining _WIN32_WINNT to at least 0x0500.
00070        - With mingw, it also requires
00071          "#define GetComputerNameEx GetComputerNameExA".
00072        - With older versions of mingw, none of the declarations are present at
00073          all, not even of the enum value ComputerNameDnsHostname.
00074    So we use gethostname().  Linking with -lws2_32 is the least evil.  */
00075 
00076 #define WIN32_LEAN_AND_MEAN
00077 /* Get winsock2.h. */
00078 #include <unistd.h>
00079 
00080 /* Get INT_MAX.  */
00081 #include <limits.h>
00082 
00083 /* Get set_winsock_errno. */
00084 #include "w32sock.h"
00085 
00086 #include "sockets.h"
00087 
00088 #undef gethostname
00089 
00090 int
00091 rpl_gethostname (char *name, size_t len)
00092 {
00093   int r;
00094 
00095   if (len > INT_MAX)
00096     len = INT_MAX;
00097   gl_sockets_startup (SOCKETS_1_1);
00098   r = gethostname (name, (int) len);
00099   if (r < 0)
00100     set_winsock_errno ();
00101 
00102   return r;
00103 }
00104 
00105 #endif