|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include "string.h" ! 3: ! 4: static char SCCSID[] = "@(#)maphost.c 2.1 DKHOST 84/08/10"; ! 5: ! 6: char mh_hostname[64]; ! 7: static char miscplace[128]; ! 8: extern char * miscfield() ; ! 9: ! 10: /* ! 11: * Map DATAKIT VCS destination name to a dialstring ! 12: * ! 13: * The host name is mapped into a full DATAKIT address using ! 14: * the file /etc/dkhosts unless it already looks like a full address ! 15: * by having '/'s in it. The host name must match the first field ! 16: * of the /etc/dkhosts file and the service class specified (a single ! 17: * ASCII character) must be in the listed classes. The dialstring is ! 18: * then constructed from the dialstring (from dkhosts), service ! 19: * (defsufx or from miscfield, below), protocol (defprot or miscfield), ! 20: * and parm. ! 21: * ! 22: * Another way of calling this routine is to provide a null host name, ! 23: * in which case the routine will successively return all hosts that ! 24: * match the service class. When the end of the dkhosts file is reached, ! 25: * a 0 pointer is returned. ! 26: * ! 27: * host = host name to match ! 28: * | 0 -- return all class matches ! 29: * service = service class to match ! 30: * defsufx = service to supply if none in host name or miscellany field ! 31: * defprot = protocol string to supply if none in miscellany field ! 32: * parm = parameter string to append ! 33: * return = dialstring pointer ! 34: * | 0 -- if host == "" and EOF reached on /etc/dkhosts ! 35: * mh_hostname : set to the matched host name if host == "" ! 36: * miscplace : set for subsequent calls to miscfield() ! 37: */ ! 38: ! 39: char * ! 40: maphost(host, service, defsufx, defprot, parm) ! 41: char service; ! 42: register char *host, *defsufx, *defprot, *parm; ! 43: { ! 44: register int matched; ! 45: char *dotted, *wasdotted; ! 46: char hostentry[256], hostaddr[128], classes[64]; ! 47: static FILE *hostfile; ! 48: static char dialstring[128]; ! 49: ! 50: strcpy(hostaddr, host); ! 51: if(dotted = strchr(hostaddr, '.')) ! 52: *dotted++ = '\0'; ! 53: ! 54: matched = 0; ! 55: ! 56: if(!strchr(hostaddr, '/') ! 57: && (hostfile || (hostfile = fopen("/etc/dkhosts", "r")) != NULL)) ! 58: ! 59: while(fgets(hostentry, sizeof(hostentry), hostfile) != NULL) ! 60: ! 61: if(hostentry[0] != '#' && ! 62: sscanf(hostentry, "%s %s %s %s", ! 63: mh_hostname, classes, dialstring, miscplace) == 4) ! 64: ! 65: if(strchr(classes, service) ! 66: && (!*host || strcmp(hostaddr, mh_hostname) == 0)){ ! 67: matched = 1; ! 68: break; ! 69: } ! 70: ! 71: if(hostfile && (*host || !matched)){ ! 72: fclose(hostfile); ! 73: hostfile = NULL; ! 74: } ! 75: ! 76: if(!matched){ ! 77: if(!*host) ! 78: return((char *) 0); ! 79: strcpy(dialstring, hostaddr); ! 80: strcpy(miscplace, "-"); ! 81: } ! 82: ! 83: if(!strchr(dialstring, '.')){ ! 84: wasdotted = dotted; ! 85: ! 86: if(dotted || (dotted = miscfield(service, 's'))) ! 87: defsufx = dotted; ! 88: ! 89: strcat(dialstring, "."); ! 90: strcat(dialstring, defsufx); ! 91: ! 92: if(!wasdotted){ ! 93: if(dotted = miscfield(service, 'p')) ! 94: defprot = dotted; ! 95: ! 96: strcat(dialstring, "."); ! 97: strcat(dialstring, defprot); ! 98: strcat(dialstring, "."); ! 99: strcat(dialstring, parm); ! 100: } ! 101: } ! 102: ! 103: dotted = &dialstring[strlen(dialstring)]; ! 104: ! 105: while(*--dotted == '.') ! 106: *dotted = '\0'; ! 107: ! 108: return(dialstring); ! 109: } ! 110: ! 111: /* ! 112: * Parse Miscellany Field of /etc/dkhosts ! 113: * ! 114: * This routine is only called after a successful call to maphost() ! 115: * and pokes around in the miscellany field saved at that time. ! 116: * The subfields are separated by commas and have two-character names ! 117: * constructed from the service class character and another character ! 118: * appropriate to the subfield value. ! 119: * ! 120: * class = service class as matched by maphost() ! 121: * field = character representing field name ! 122: * return = field value string ! 123: * | 0 -- field not listed ! 124: */ ! 125: ! 126: char * ! 127: miscfield(class, field) ! 128: char class, field; ! 129: { ! 130: register char *next, *ptr; ! 131: int n; ! 132: char cs[16]; ! 133: static char place[64]; ! 134: ! 135: ptr = miscplace; ! 136: ! 137: sprintf(cs, "%c%c=", class, field); ! 138: n = strlen(cs); ! 139: ! 140: do{ ! 141: if(next = strchr(ptr, ',')) ! 142: *next = '\0'; ! 143: ! 144: if(strncmp(ptr, cs, n) == 0){ ! 145: strcpy(place, ptr+n); ! 146: if(next) ! 147: *next++ = ','; ! 148: return(place); ! 149: } ! 150: ! 151: if(next) ! 152: *next++ = ','; ! 153: }while(ptr = next); ! 154: ! 155: return((char *) 0); ! 156: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.