#include #include #include #include #include #include #include #include #include /* compile: cc -o checkname checkname.c * * usage: * unix% checkname * Will print the local host name as best as can be determined. */ char *GetFQDN(void) { char buff[BUFSIZ]; static char *retval; struct hostent *hp; char *p; char **ap; char *domain; /* Return any old results. */ if (retval) return retval; /* Try gethostname. */ bzero(buff,BUFSIZ); if (gethostname(buff, BUFSIZ) < 0) { printf("Error calling gethostname! This should not happen\n"); return NULL; } if (strchr(buff, '.') != NULL) { retval=strdup(buff); return retval; } /* See if DNS (or /etc/hosts) gives us a full domain name. */ if ((hp = gethostbyname(buff)) != NULL) { /* First, see if the main name is a FQDN. It should be. */ if (hp != NULL && strchr(hp->h_name, '.') != NULL && strstr(hp->h_name,"localdomain")==0 ) { if (strlen(hp->h_name) < BUFSIZ - 1) { return strcpy(buff, hp->h_name); } /* Doesn't fit; make sure we don't return bad data next time. */ buff[0] = '\0'; printf("Host name too long.\n"); retval=NULL; return hp->h_name; } /* Second, see if any aliases are. */ if ((ap = hp->h_aliases) != NULL) { while ((p = *ap++) != NULL) { if (strchr(p, '.') != NULL && strstr(p,"localdomain")==0 ) { retval=strdup(p); return retval; } } } } /* Give up: Get SP_DOMAIN and append it. */ printf("The gethostbyname call returned either no domain, or 'localdomain'\n" "Neither of these will work, falling back to $SP_DOMAIN.\n"); if (!(domain=getenv("SP_DOMAIN"))) { printf("SP_DOMAIN not set!\n"); return NULL; } /* restore base hostname */ bzero(buff,BUFSIZ); if (gethostname(buff, BUFSIZ) < 0) /* this already worked once */ return NULL; (void)strcat(buff, "."); (void)strcat(buff, domain); retval=strdup(buff); return retval; } main() { char *host; if (!(host=GetFQDN())) { printf("\ Couldn't get any local host information. This may mean that your\n\ computer is not configured properly. You may need to add the host\n\ name to /etc/hosts. See the man page for gethostbyname.\n"); return 0; } if (!getenv("SP_DOMAIN")) { printf("\ Note: the $SP_DOMAIN variable is not set.\n\ If gethostname (or this program) doesn't return any domain information,\n\ this variable needs to be set (normally in $SPOCK/Spockrc).\n\ \n\ For the purposes if this test program, set it in the shell if you need it.\n"); } printf("\nThe local host name for this machine is %s\n",host); }