Go to the previous, next section.
This chapter describes functions that return information about the particular machine that is in use--the type of hardware, the type of software, and the individual machine's name.
This section explains how to identify the particular machine that your program is running on. The identification of a machine consists of its Internet host name and Internet address; see section The Internet Namespace.
Prototypes for these functions appear in `unistd.h'. The shell
commands hostname
and hostid
work by calling them.
Function: int gethostname (char *name, size_t size)
This function returns the name of the host machine in the array name. The size argument specifies the size of this array, in bytes.
The return value is 0
on success and -1
on failure. In
the GNU C library, gethostname
fails if size is not large
enough; then you can try again with a larger array. The following
errno
error condition is defined for this function:
ENAMETOOLONG
On some systems, there is a symbol for the maximum possible host name
length: MAXHOSTNAMELEN
. It is defined in `sys/param.h'.
But you can't count on this to exist, so it is cleaner to handle
failure and try again.
gethostname
stores the beginning of the host name in name
even if the host name won't entirely fit. For some purposes, a
truncated host name is good enough. If it is, you can ignore the
error code.
Function: int sethostname (const char *name, size_t length)
The sethostname
function sets the name of the host machine to
name, a string with length length. Only privileged
processes are allowed to do this. Usually it happens just once, at
system boot time.
The return value is 0
on success and -1
on failure.
The following errno
error condition is defined for this function:
EPERM
Function: long int gethostid (void)
This function returns the Internet address of the machine the program is running on.
Function: int sethostid (long int id)
The sethostid
function sets the address of the host machine to
id. Only privileged processes are allowed to do this. Usually it
happens just once, at system boot time.
The return value is 0
on success and -1
on failure.
The following errno
error condition is defined for this function:
EPERM
You can use the uname
function to find out some information about
the type of computer your program is running on. This function and the
associated data type are declared in the header file
`sys/utsname.h'.
The utsname
structure is used to hold information returned
by the uname
function. It has the following members:
char sysname[]
char nodename[]
gethostname
;
see section Host Identification.
char release[]
char version[]
char machine[]
The GNU C Library fills in this field based on the configuration name that was specified when building and installing the library. GNU uses a three-part name to describe a system configuration; the three parts are cpu, manufacturer and system-type, and they are separated with dashes. Any possible combination of three names is potentially meaningful, but most such combinations are meaningless in practice and even the meaningful ones are not necessarily supported by any particular GNU program.
Since the value in machine
is supposed to describe just the
hardware, it consists of the first two parts of the configuration name:
`cpu-manufacturer'.
Here is a list of all the possible alternatives:
"i386-anything"
,"m68k-hp"
,"sparc-sun"
,"m68k-sun"
,"m68k-sony"
,"mips-dec"
Function: int uname (struct utsname *info)
The uname
function fills in the structure pointed to by
info with information about the operating system and host machine.
A non-negative value indicates that the data was successfully stored.
-1
as the value indicates an error. The only error possible is
EFAULT
, which we normally don't mention as it is always a
possibility.
Go to the previous, next section.