This is Unofficial EPICS BASE Doxygen Site
localHostName.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE is distributed subject to a Software License Agreement found
7 * in file LICENSE that is included with this distribution.
8 \*************************************************************************/
9 
10 /*
11  *
12  * L O S A L A M O S
13  * Los Alamos National Laboratory
14  * Los Alamos, New Mexico 87545
15  *
16  * Copyright, 1986, The Regents of the University of California.
17  *
18  * Author: Jeff Hill
19  */
20 
21 #include "osiSock.h"
22 
23 #include "localHostName.h"
24 
26 
28  attachedToSockLib ( osiSockAttach () != 0 ), length ( 0u )
29 {
30  const char * pErrStr = "<unknown host>";
31  int status = -1;
32  if ( this->attachedToSockLib ) {
33  status = gethostname (
34  this->cache, sizeof ( this->cache ) );
35  }
36  if ( status ) {
37  strncpy ( this->cache, pErrStr, sizeof ( this->cache ) );
38  }
39  this->cache [ sizeof ( this->cache ) - 1u ] = '\0';
40  this->length = strlen ( this->cache );
41 }
42 
44 {
45  if ( this->attachedToSockLib ) {
46  osiSockRelease ();
47  }
48 }
49 
51  char * pBuf, unsigned bufLength ) const
52 {
53  if ( bufLength ) {
54  strncpy ( pBuf, this->cache, bufLength );
55  if ( this->length < bufLength ) {
56  return this->length;
57  }
58  else {
59  unsigned reducedSize = bufLength - 1;
60  pBuf [ reducedSize ] = '\0';
61  return reducedSize;
62  }
63  }
64  return 0u;
65 }
epicsSingleton< localHostName > localHostNameCache
pvd::Status status
void osiSockRelease()
Definition: osdSock.c:62
char * cache
Definition: reader.c:18
unsigned getName(char *pBuf, unsigned bufLength) const
int osiSockAttach()
Definition: osdSock.c:54