Node and Host Name Sizes on HP-UX: Using the Expanded Capabilities of HP-UX

13
However, since the expanded version of the uname(2) function and associated structure does not
exist on versions prior to HP-UX 11i v2 September 2004 Update, it is not possible to build a binary
that uses expanded uname(2) on those versions.
The solution is to employ the approach described in the previous section, and to compile the program
on the earliest HP-UX version on which it is to be run.
Again, do not use this approach for programs that re-export, via an API, the utsname structure, or
any of the symbolic constants: MAXHOSTNAMELEN, UTSLEN, SYS_NMLN, SNLEN (or equivalents).
Dynamic Buffer Sizing
Programs should avoid using the MAXHOSTNAMELEN parameter. Instead, they should query the
system for the maximum possible length by usingsysconf(_SC_HOST_NAME_MAX). The result,
after adding 1 to allow for a null terminator, should be used to allocate a buffer of that size to hold
the host name.
Because this system configuration variable is not available on all versions of HP-UX, you might want to
use conditional compilation in the following manner:
hostbufsz = 0;
#ifdef _SC_HOST_NAME_MAX
hostbufsz = sysconf(_SC_HOST_NAME_MAX) + 1;
#endif
if (hostbufsz <= 0) hostbufsz = 256;
hostbuf = malloc(hostbufsz);
retval = gethostname(hostbuf, hostbufsz);
The code fragment defaults to a host name buffer size of 256 when the sysconf() parameter is not
available.
Re-export of Node Name and Host Name Interfaces
Some software developers supply libraries of application interfaces for their customers. These
“exported” interfaces are accessible to customer applications. These interfaces might be functions or
accessible data structures.
The specification of these interfaces can include direct dependencies on the HP-UX node or host name
interfaces. If so, they are said to “re-export” those interfaces. Examples of re-exported interfaces
include:
A structure, struct abdx, is passed into or out of some library function. A field in struct
abdx is struct utsname abdx_uts. Thus, the interface re-exports the utsname structure.
A library function, muddle(), accepts a pointer to a buffer into which it places a string. The
specification states that the buffer should be UTSLEN bytes long. This re-exports the UTSLEN
symbol.
If the library functions are simply recompiled with -D_HPUX_API_LEVEL=20040821, they can
accommodate long host and node names. They can also allow the applications that use them to
recompile to accommodate the long names.