00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030 #ifndef __GETHOSTNAME_H
00031 #define __GETHOSTNAME_H
00032
00033 #ifdef HAVE_CONFIG_H
00034 # include "config.h"
00035 #endif
00036
00037 #ifdef HAVE_UNISTD_H
00038 # include <unistd.h>
00039 #endif
00040
00041 #ifdef SYS_UTSNAME_H
00042 # include <sys/utsname.h>
00043 #endif
00044
00045 #ifdef __WIN32__
00046 # include <winbase.h>
00047 #endif
00048
00049 #if defined(__VMS) && __CRTL_VER < 70000000
00050 # include <omniVMS/utsname.hxx>
00051 #endif
00052
00053 #include <errno.h>
00054
00055
00056
00057
00058
00059 #if defined(__WIN32__) && !defined(MAXHOSTNAMELEN)
00060 # define MAXHOSTNAMELEN MAX_COMPUTERNAME_LENGTH
00061 #elif defined(__WIN32__) && defined(MAXHOSTNAMELEN)
00062 # undef MAXHOSTNAMELEN
00063 # define MAXHOSTNAMELEN MAX_COMPUTERNAME_LENGTH
00064 #elif !defined(MAXHOSTNAMELEN)
00065 # define MAXHOSTNAMELEN 256
00066
00067
00068 #elif defined(MAXHOSTNAMELEN) && (MAXHOSTNAMELEN < 64)
00069 # undef MAXHOSTNAMELEN
00070 # define MAXHOSTNAMELEN 256
00071 #endif
00072
00073
00074 #ifndef HAVE_GETHOSTNAME
00075 inline int
00076 gethostname(char* hostname, size_t len)
00077 {
00078 int result =-1;
00079 if(len<1)
00080 {
00081 errno=EINVAL;
00082 return result;
00083 }
00084 if(len>MAXHOSTNAMELEN)
00085 {
00086 len=MAXHOSTNAMELEN;
00087 }
00088
00089 #if defined(__WIN32__)
00090 DWORD dwordlen = len;
00091 if( GetComputerName((LPTSTR) hostname, &dwordlen) )
00092 {
00093 result=0;
00094 }
00095 else
00096 {
00097 errno=EFAULT;
00098 }
00099 #else
00100 struct utsname un;
00101 if( uname(&un)==0 && strlen(un.nodename)<len)
00102 {
00103 strcpy(hostname,un.nodename);
00104 result=0;
00105 }
00106 else
00107 {
00108 errno=EFAULT;
00109 }
00110 #endif
00111 return result;
00112 }
00113 #endif
00114
00115 #endif