// Ivan Novick // August 10, 2009 // demonstrate GNU libc function for finding the hostname of the local host #include #define BUF_SIZE 256 int main(int argc, char** argv){ char buffer[BUF_SIZE]; if (gethostname(buffer, BUF_SIZE)){ fprintf(stderr, "error retrieving local hostname\n"); return 1; } // it is possible the hostname was truncated and not null-terminated buffer[BUF_SIZE-1] = 0; // print the result fprintf(stdout, "%s\n", buffer); return 0; }