Skip to content

Instantly share code, notes, and snippets.

@ictlyh
Created November 30, 2016 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ictlyh/8ccabe64a257b331fca1af5ae1f51d6f to your computer and use it in GitHub Desktop.
Save ictlyh/8ccabe64a257b331fca1af5ae1f51d6f to your computer and use it in GitHub Desktop.
Demo of getting host name and domain name
#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