1*7899Ssam /* getprotoent.c 4.1 82/08/25 */ 2*7899Ssam 3*7899Ssam #include <stdio.h> 4*7899Ssam #include <sys/socket.h> 5*7899Ssam #include <netdb.h> 6*7899Ssam #include <ctype.h> 7*7899Ssam 8*7899Ssam #define MAXALIASES 35 9*7899Ssam 10*7899Ssam static char *PROTODB = "/usr/lib/protocols"; 11*7899Ssam static FILE *protof = NULL; 12*7899Ssam static char line[BUFSIZ+1]; 13*7899Ssam static struct protoent proto; 14*7899Ssam static char *proto_aliases[MAXALIASES]; 15*7899Ssam static int stayopen = 0; 16*7899Ssam static unsigned long value(); 17*7899Ssam static char *any(); 18*7899Ssam 19*7899Ssam setprotoent(f) 20*7899Ssam int f; 21*7899Ssam { 22*7899Ssam if (protof == NULL) 23*7899Ssam protof = fopen(PROTODB, "r" ); 24*7899Ssam else 25*7899Ssam rewind(protof); 26*7899Ssam stayopen |= f; 27*7899Ssam } 28*7899Ssam 29*7899Ssam endprotoent() 30*7899Ssam { 31*7899Ssam if (protof && !stayopen) { 32*7899Ssam fclose(protof); 33*7899Ssam protof = NULL; 34*7899Ssam } 35*7899Ssam } 36*7899Ssam 37*7899Ssam struct protoent * 38*7899Ssam getprotoent() 39*7899Ssam { 40*7899Ssam char *p; 41*7899Ssam register char *cp, **q; 42*7899Ssam 43*7899Ssam if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 44*7899Ssam return (NULL); 45*7899Ssam again: 46*7899Ssam if ((p = fgets(line, BUFSIZ, protof)) == NULL) 47*7899Ssam return (NULL); 48*7899Ssam if (*p == '#') 49*7899Ssam goto again; 50*7899Ssam cp = any(p, "#\n"); 51*7899Ssam if (cp == NULL) 52*7899Ssam goto again; 53*7899Ssam *cp = '\0'; 54*7899Ssam proto.p_name = p; 55*7899Ssam cp = any(p, " \t"); 56*7899Ssam if (cp == NULL) 57*7899Ssam goto again; 58*7899Ssam *cp++ = '\0'; 59*7899Ssam while (*cp == ' ' || *cp == '\t') 60*7899Ssam cp++; 61*7899Ssam p = any(cp, " \t"); 62*7899Ssam if (p != NULL) 63*7899Ssam *p++ = '\0'; 64*7899Ssam proto.p_proto = atoi(cp); 65*7899Ssam proto.p_aliases = proto_aliases; 66*7899Ssam cp = p; 67*7899Ssam q = proto_aliases; 68*7899Ssam while (*cp) { 69*7899Ssam if (*cp == ' ' || *cp == '\t') { 70*7899Ssam cp++; 71*7899Ssam continue; 72*7899Ssam } 73*7899Ssam if (q < &proto_aliases[MAXALIASES - 1]) 74*7899Ssam *q++ = cp; 75*7899Ssam cp = any(cp, " \t"); 76*7899Ssam if (*cp != NULL) 77*7899Ssam *cp++ = '\0'; 78*7899Ssam } 79*7899Ssam *q = NULL; 80*7899Ssam return (&proto); 81*7899Ssam } 82*7899Ssam 83*7899Ssam static char * 84*7899Ssam any(cp, match) 85*7899Ssam register char *cp; 86*7899Ssam char *match; 87*7899Ssam { 88*7899Ssam register char *mp, c; 89*7899Ssam 90*7899Ssam while (c = *cp) { 91*7899Ssam for (mp = match; *mp; mp++) 92*7899Ssam if (*mp == c) 93*7899Ssam return (cp); 94*7899Ssam cp++; 95*7899Ssam } 96*7899Ssam return ((char *)0); 97*7899Ssam } 98