@Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.
Œ

ˆ‰SEC473Št�”›System Information

@�”˜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.
ˆ‰SEC474Št�”›Host Identification

@�”˜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 ‰ŠP�”›The Internet Namespace@�”˜. The host name should always be a fully qualified domain name, like H`crispy-wheats-n-chicken.ai.mit.edu'@, not a simple name like just H`crispy-wheats-n-chicken'@.
‰IDX2080Š‰IDX2081Š‰IDX2082Š@�”˜Prototypes for these functions appear in H`unistd.h'@. The shell commands Hhostname@ and Hhostid@ work by calling them.

AFunction:@ int Dgethostname@ B(char *Bname@, size_t Bsize@)
Â‰IDX2083Š@�”˜This function returns the name of the host machine in the array Bname@. The Bsize@ argument specifies the size of this array, in bytes.
The return value is H0@ on success and H-1@ on failure. In the GNU C library, Hgethostname@ fails if Bsize@ is not large enough; then you can try again with a larger array. The following Herrno@ error condition is defined for this function:
HENAMETOOLONG
Ä@The Bsize@ argument is less than the size of the host name plus one.Â

‰IDX2084Š@�”˜On some systems, there is a symbol for the maximum possible host name length: HMAXHOSTNAMELEN@. It is defined in H`sys/param.h'@. But you can't count on this to exist, so it is cleaner to handle failure and try again.
Hgethostname@ stores the beginning of the host name in Bname@ 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.À

AFunction:@ int Dsethostname@ B(const char *Bname@, size_t Blength@)
Â‰IDX2085Š@�”˜The Hsethostname@ function sets the name of the host machine to Bname@, a string with length Blength@. Only privileged processes are allowed to do this. Usually it happens just once, at system boot time.
The return value is H0@ on success and H-1@ on failure. The following Herrno@ error condition is defined for this function:
HEPERM
Ä@This process cannot set the host name because it is not privileged.Â
À

AFunction:@ long int Dgethostid@ B(void)
Â‰IDX2086Š@�”˜This function returns the "host ID" of the machine the program is running on. By convention, this is usually the primary Internet address of that machine, converted to a Hlong int@. However, some systems it is a meaningless but unique number which is hard-coded for each machine.À

AFunction:@ int Dsethostid@ B(long int Bid@)
Â‰IDX2087Š@�”˜The Hsethostid@ function sets the "host ID" of the host machine to Bid@. Only privileged processes are allowed to do this. Usually it happens just once, at system boot time.
The return value is H0@ on success and H-1@ on failure. The following Herrno@ error condition is defined for this function:
HEPERM
Ä@This process cannot set the host name because it is not privileged.
ÂHENOSYS
Ä@The operating system does not support setting the host ID. On some systems, the host ID is a meaningless but unique number hard-coded for each machine.Â
À

ˆ‰SEC475Št�”›Hardware/Software Type Identification

@�”˜You can use the Huname@ 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 H`sys/utsname.h'@. ‰IDX2088Š

A�”˜Data Type:@ Dstruct utsname
Â‰IDX2089Š@�”˜The Hutsname@ structure is used to hold information returned by the Huname@ function. It has the following members:
Hchar sysname[]
Ä@This is the name of the operating system in use.
ÂHchar nodename[]
Ä@This is the network name of this particular computer. In the GNU library, the value is the same as that returned by Hgethostname@; see section ‰ŠP�”›Host Identification@�”˜.
ÂHchar release[]
Ä@This is the current release level of the operating system implementation.
ÂHchar version[]
Ä@This is the current version level within the release of the operating system.
ÂHchar machine[]
Ä@This is a description of the type of hardware that is in use. Some systems provide a mechanism to interrogate the kernel directly for this information. On systems without such a mechanism, 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 Bcpu@, Bmanufacturer@ and Bsystem-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 Hmachine@ is supposed to describe just the hardware, it consists of the first two parts of the configuration name: H`JcpuH-JmanufacturerH'@. For example, it might be one of these:
Æ
H"sparc-sun"@, H"i386-JanythingH"@, H"m68k-hp"@, H"m68k-sony"@, H"m68k-sun"@, H"mips-dec"Ä
Â
À

AFunction:@ int Duname@ B(struct utsname *Binfo@)
Â‰IDX2090Š@�”˜The Huname@ function fills in the structure pointed to by Binfo@ with information about the operating system and host machine. A non-negative value indicates that the data was successfully stored.
H-1@ as the value indicates an error. The only error possible is HEFAULT@, which we normally don't mention as it is always a possibility.À

Œ
Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.