Created
November 30, 2016 09:37
-
-
Save ictlyh/8ccabe64a257b331fca1af5ae1f51d6f to your computer and use it in GitHub Desktop.
Demo of getting host name and domain name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
int main(int argc,char **argv) { | |
int z; | |
char buf[32]; | |
z = gethostname(buf,sizeof(buf)); | |
if ( z == -1 ) { | |
fprintf(stderr, "%s: gethostname(2)\n", | |
strerror(errno)); | |
exit(1); | |
} | |
printf("host name = '%s'\n",buf); | |
z = getdomainname(buf,sizeof buf); | |
if ( z == -1 ) { | |
fprintf(stderr,"%s: getdomainname(2)\n", | |
strerror(errno)); | |
exit(1); | |
} | |
printf("domain name = '%s'\n",buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment