From 54e9622e95afc8d2d550da8d3974cc9ed7ef7160 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Thu, 30 Dec 2010 13:19:36 +0100 Subject: [PATCH] ENH: add Foam::domainName(), implemented as per 'hostname -d' from net-tools --- src/OSspecific/POSIX/POSIX.C | 47 +++++++++++++++++++++---------- src/OpenFOAM/include/OSspecific.H | 7 +++-- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 380c785c0b8..ec0d1e79f4b 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 3ee759a5968..df867a589b6 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(); -- GitLab