121379Sdist /* 221379Sdist * Copyright (c) 1983 Regents of the University of California. 3*33679Sbostic * All rights reserved. 4*33679Sbostic * 5*33679Sbostic * Redistribution and use in source and binary forms are permitted 6*33679Sbostic * provided that this notice is preserved and that due credit is given 7*33679Sbostic * to the University of California at Berkeley. The name of the University 8*33679Sbostic * may not be used to endorse or promote products derived from this 9*33679Sbostic * software without specific prior written permission. This software 10*33679Sbostic * is provided ``as is'' without express or implied warranty. 1121379Sdist */ 127899Ssam 1326620Sdonn #if defined(LIBC_SCCS) && !defined(lint) 14*33679Sbostic static char sccsid[] = "@(#)getprotoent.c 5.4 (Berkeley) 03/07/88"; 15*33679Sbostic #endif /* LIBC_SCCS and not lint */ 1621379Sdist 177899Ssam #include <stdio.h> 1812691Ssam #include <sys/types.h> 197899Ssam #include <sys/socket.h> 207899Ssam #include <netdb.h> 217899Ssam #include <ctype.h> 227899Ssam 237899Ssam #define MAXALIASES 35 247899Ssam 259808Ssam static char PROTODB[] = "/etc/protocols"; 267899Ssam static FILE *protof = NULL; 277899Ssam static char line[BUFSIZ+1]; 287899Ssam static struct protoent proto; 297899Ssam static char *proto_aliases[MAXALIASES]; 307899Ssam static char *any(); 3128280Slepreau int _proto_stayopen; 327899Ssam 337899Ssam setprotoent(f) 347899Ssam int f; 357899Ssam { 367899Ssam if (protof == NULL) 377899Ssam protof = fopen(PROTODB, "r" ); 387899Ssam else 397899Ssam rewind(protof); 4028280Slepreau _proto_stayopen |= f; 417899Ssam } 427899Ssam 437899Ssam endprotoent() 447899Ssam { 4528280Slepreau if (protof) { 467899Ssam fclose(protof); 477899Ssam protof = NULL; 487899Ssam } 4928280Slepreau _proto_stayopen = 0; 507899Ssam } 517899Ssam 527899Ssam struct protoent * 537899Ssam getprotoent() 547899Ssam { 557899Ssam char *p; 567899Ssam register char *cp, **q; 577899Ssam 587899Ssam if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 597899Ssam return (NULL); 607899Ssam again: 617899Ssam if ((p = fgets(line, BUFSIZ, protof)) == NULL) 627899Ssam return (NULL); 637899Ssam if (*p == '#') 647899Ssam goto again; 657899Ssam cp = any(p, "#\n"); 667899Ssam if (cp == NULL) 677899Ssam goto again; 687899Ssam *cp = '\0'; 697899Ssam proto.p_name = p; 707899Ssam cp = any(p, " \t"); 717899Ssam if (cp == NULL) 727899Ssam goto again; 737899Ssam *cp++ = '\0'; 747899Ssam while (*cp == ' ' || *cp == '\t') 757899Ssam cp++; 767899Ssam p = any(cp, " \t"); 777899Ssam if (p != NULL) 787899Ssam *p++ = '\0'; 797899Ssam proto.p_proto = atoi(cp); 809190Ssam q = proto.p_aliases = proto_aliases; 819190Ssam if (p != NULL) { 829190Ssam cp = p; 8310087Ssam while (cp && *cp) { 849190Ssam if (*cp == ' ' || *cp == '\t') { 859190Ssam cp++; 869190Ssam continue; 879190Ssam } 889190Ssam if (q < &proto_aliases[MAXALIASES - 1]) 899190Ssam *q++ = cp; 909190Ssam cp = any(cp, " \t"); 9110087Ssam if (cp != NULL) 929190Ssam *cp++ = '\0'; 937899Ssam } 947899Ssam } 957899Ssam *q = NULL; 967899Ssam return (&proto); 977899Ssam } 987899Ssam 997899Ssam static char * 1007899Ssam any(cp, match) 1017899Ssam register char *cp; 1027899Ssam char *match; 1037899Ssam { 1047899Ssam register char *mp, c; 1057899Ssam 1067899Ssam while (c = *cp) { 1077899Ssam for (mp = match; *mp; mp++) 1087899Ssam if (*mp == c) 1097899Ssam return (cp); 1107899Ssam cp++; 1117899Ssam } 1127899Ssam return ((char *)0); 1137899Ssam } 114