You can try:
int getdomainname(char *name, size_t len);
Try running this program:
#include <unistd.h> #include <stdio.h> int main() { char buf[255]; getdomainname(buf, 255); printf("Domain: %s\n", buf); return 0; }
EDIT:
No, after many attempts, I suspect you'll need to use syscall and an ugly C-parser (using AWK inside popen would make this code a little shorter).
This code works for me:
#include <stdio.h> #include <string.h> int main() { char buf[255]; const char reqhostname[255] = "ENTER_YOUR_HOSTNAME_HERE"; FILE *fd; char readbuf[255]; char *pch; int token_counter = 0; memset(buf, 0, 255); strcat(buf, "host "); strcat(buf, reqhostname); fd = popen(buf, "r"); fgets(readbuf, 255, fd); printf("Host returned: %s\n", readbuf); pclose(fd); pch = strtok(readbuf, " "); while (pch != NULL) { strcpy(buf, pch); break; } memset(buf2, 0, 255); pch = strtok(buf, "."); while (pch != NULL) { pch = strtok(NULL, "."); if (pch == NULL) { memset(buf, 0, 255); strncpy(buf, buf2, strlen(buf2) - 1); break; } token_counter++; strcat(buf2, pch); strcat(buf2, "."); } printf("Domain: %s\n", buf); return 0; }
Avio
source share