OmniEvents
gethostname.h
Go to the documentation of this file.
1 /* Package : omniEvents
2  * gethostname.h Created : 2003/10/31
3  * Author : Alex Tingle
4  *
5  * Copyright (C) 2003 Alex Tingle.
6  *
7  * This file is part of the omniEvents application.
8  *
9  * omniEvents is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * omniEvents is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
30 #ifndef __GETHOSTNAME_H
31 #define __GETHOSTNAME_H
32 
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36 
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 
41 #ifdef SYS_UTSNAME_H
42 # include <sys/utsname.h>
43 #endif
44 
45 #ifdef __WIN32__
46 # include <winbase.h>
47 #endif
48 
49 #if defined(__VMS) && __CRTL_VER < 70000000
50 # include <omniVMS/utsname.hxx>
51 #endif
52 
53 #include <errno.h>
54 
55 /*
56  * Ensure that MAXHOSTNAMELEN is defined correctly.
57  */
58 
59 #if defined(__WIN32__) && !defined(MAXHOSTNAMELEN)
60 # define MAXHOSTNAMELEN MAX_COMPUTERNAME_LENGTH
61 #elif defined(__WIN32__) && defined(MAXHOSTNAMELEN)
62 # undef MAXHOSTNAMELEN
63 # define MAXHOSTNAMELEN MAX_COMPUTERNAME_LENGTH
64 #elif !defined(MAXHOSTNAMELEN)
65 # define MAXHOSTNAMELEN 256
66 /* Apparently on some AIX versions, MAXHOSTNAMELEN is too small (32) to
67  * reflect the true size a hostname can be. Check and fix the value. */
68 #elif defined(MAXHOSTNAMELEN) && (MAXHOSTNAMELEN < 64)
69 # undef MAXHOSTNAMELEN
70 # define MAXHOSTNAMELEN 256
71 #endif
72 
73 
74 #ifndef HAVE_GETHOSTNAME
75 inline int
76 gethostname(char* hostname, size_t len)
77 {
78  int result =-1;
79  if(len<1)
80  {
81  errno=EINVAL;
82  return result;
83  }
84  if(len>MAXHOSTNAMELEN)
85  {
86  len=MAXHOSTNAMELEN;
87  }
88 
89 #if defined(__WIN32__)
90  DWORD dwordlen = len;
91  if( GetComputerName((LPTSTR) hostname, &dwordlen) )
92  {
93  result=0;
94  }
95  else
96  {
97  errno=EFAULT;
98  }
99 #else
100  struct utsname un;
101  if( uname(&un)==0 && strlen(un.nodename)<len)
102  {
103  strcpy(hostname,un.nodename);
104  result=0;
105  }
106  else
107  {
108  errno=EFAULT;
109  }
110 #endif
111  return result;
112 }
113 #endif /* HAVE_GETHOSTNAME */
114 
115 #endif /* __GETHOSTNAME_H */
int gethostname(char *hostname, size_t len)
Definition: gethostname.h:76
#define MAXHOSTNAMELEN
Provides the function ‘int gethostname(char* hostname, size_t len)’ in a platform independent manner.
Definition: gethostname.h:65