getName() returns string size

This commit is contained in:
Jeff Hill
2004-10-19 20:40:47 +00:00
parent 085b228c83
commit 004db1aea7
2 changed files with 29 additions and 22 deletions

View File

@@ -25,19 +25,17 @@
epicsSingleton < localHostName > localHostNameAtLoadTime;
localHostName::localHostName ()
localHostName::localHostName () :
attachedToSockLib ( osiSockAttach () != 0 ), length ( 0u )
{
int status = osiSockAttach ();
if ( status ) {
this->attachedToSockLib = true;
status = gethostname ( this->cache, sizeof ( this->cache ) - 1 );
if ( status ) {
strncpy ( this->cache, "<unknown host>", sizeof ( this->cache ) );
}
const char * pErrStr = "<unknown host>";
int status = -1;
if ( this->attachedToSockLib ) {
status = gethostname (
this->cache, sizeof ( this->cache ) );
}
else {
this->attachedToSockLib = false;
strncpy ( this->cache, "<unknown host>", sizeof ( this->cache ) );
if ( status ) {
strncpy ( this->cache, pErrStr, sizeof ( this->cache ) );
}
this->cache [ sizeof ( this->cache ) - 1u ] = '\0';
this->length = strlen ( this->cache );
@@ -49,3 +47,20 @@ localHostName::~localHostName ()
osiSockRelease ();
}
}
unsigned localHostName::getName (
char * pBuf, unsigned bufLength ) const
{
if ( bufLength ) {
strncpy ( pBuf, this->cache, bufLength );
if ( this->length < bufLength ) {
return this->length;
}
else {
unsigned reducedSize = bufLength - 1;
pBuf [ reducedSize ] = '\0';
return reducedSize;
}
}
return 0u;
}