diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 380c785c0b83fe4c92cbf89c0ec622107e59d679..ec0d1e79f4b2192cb3b81b890daa7de7306eda9f 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -111,16 +111,16 @@ bool Foam::setEnv Foam::word Foam::hostName(bool full) { - char buf[256]; - gethostname(buf, 256); + char buf[128]; + gethostname(buf, sizeof(buf)); + // implementation as per hostname from net-tools if (full) { - struct hostent *hptr = gethostbyname(buf); - - if (hptr) + struct hostent *hp = gethostbyname(buf); + if (hp) { - return hptr->h_name; + return hp->h_name; } } @@ -128,6 +128,27 @@ Foam::word Foam::hostName(bool full) } +Foam::word Foam::domainName() +{ + char buf[128]; + gethostname(buf, sizeof(buf)); + + // implementation as per hostname from net-tools + struct hostent *hp = gethostbyname(buf); + if (hp) + { + char *p = strchr(hp->h_name, '.'); + if (p) + { + ++p; + return p; + } + } + + return word::null; +} + + Foam::word Foam::userName() { struct passwd* pw = getpwuid(getuid()); @@ -201,8 +222,8 @@ Foam::fileName Foam::home(const word& userName) Foam::fileName Foam::cwd() { - char buf[255]; - if (getcwd(buf, 255)) + char buf[256]; + if (getcwd(buf, sizeof(buf))) { return buf; } @@ -957,8 +978,6 @@ bool Foam::ping const label timeOut ) { - char *serverAddress; - struct in_addr *ptr; struct hostent *hostPtr; volatile int sockfd; struct sockaddr_in destAddr; // will hold the destination addr @@ -968,15 +987,13 @@ bool Foam::ping { FatalErrorIn ( - "Foam::ping(const word&, const label)" + "Foam::ping(const word&, ...)" ) << "gethostbyname error " << h_errno << " for host " << destName << abort(FatalError); } // Get first of the SLL of addresses - serverAddress = *(hostPtr->h_addr_list); - ptr = reinterpret_cast<struct in_addr*>(serverAddress); - addr = ptr->s_addr; + addr = (reinterpret_cast<struct in_addr*>(*(hostPtr->h_addr_list)))->s_addr; // Allocate socket sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -990,7 +1007,7 @@ bool Foam::ping } // Fill sockaddr_in structure with dest address and port - memset (reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr)); + memset(reinterpret_cast<char *>(&destAddr), '\0', sizeof(destAddr)); destAddr.sin_family = AF_INET; destAddr.sin_port = htons(ushort(destPort)); destAddr.sin_addr.s_addr = addr; diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 3ee759a5968e8639d9274421947399d9c786d4d0..df867a589b6a1624d2282e904a21dd4691e936b0 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -67,10 +67,13 @@ string getEnv(const word&); //- Set an environment variable bool setEnv(const word& name, const string& value, const bool overwrite); -//- Return the system's host name -// Optionally the full name reported from gethostbyname +//- Return the system's host name, as per hostname(1) +// Optionally with the full name (as per the '-f' option) word hostName(const bool full=false); +//- Return the system's domain name, as per hostname(1) with the '-d' option +word domainName(); + //- Return the user's login name word userName();