OmniEvents
|
00001 /* Package : omniEvents 00002 * gethostname.h Created : 2003/10/31 00003 * Author : Alex Tingle 00004 * 00005 * Copyright (C) 2003 Alex Tingle. 00006 * 00007 * This file is part of the omniEvents application. 00008 * 00009 * omniEvents is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * omniEvents is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 * Ensure that MAXHOSTNAMELEN is defined correctly. 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 /* Apparently on some AIX versions, MAXHOSTNAMELEN is too small (32) to 00067 * reflect the true size a hostname can be. Check and fix the value. */ 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 /* HAVE_GETHOSTNAME */ 00114 00115 #endif /* __GETHOSTNAME_H */