#include <sys/utsname.h>#include <unistd.h>#include <stdio.h>int main(){ char computer[256]; struct utsname uts; if(gethostname(computer,256)!=0 || uname(&uts)<0) { exit(1); } printf("Computer host name is %s\n",computer); printf("System is %s on %s hardware\n",uts.sysname,uts.machine); printf("Nodename is %s\n",uts.nodename); printf("Version is %s, %s\n",uts.release,uts.version); return 0;}
Function prototype:
# Include <unistd. h>
Int gethostname (char * Name, size_t namelen );
Write the Network Name of the machine to name. The string must be at least as long as namelen. 0 is returned. Otherwise,-1 is returned.
# Include <sys/utsname. h>
Int uname (struct utsname * Name );
Write host data to a struct directed by name.
Utsname struct data element:
Char sysname []; operating system name
Char nodename []; host name
Char release []; Release level of the system
Char version []; System Version Number
Char Machine []; hardware type
A non-negative integer is returned for success.-1 is returned for failure.
Note:
License issues:
# Include <unistd. h>
Long gethostid (void); returns a unique value for the host to ensure the software license.