121379Sdist /* 221379Sdist * Copyright (c) 1983 Regents of the University of California. 333679Sbostic * All rights reserved. 433679Sbostic * 542626Sbostic * %sccs.include.redist.c% 621379Sdist */ 77899Ssam 826620Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46604Sbostic static char sccsid[] = "@(#)getprotoent.c 5.8 (Berkeley) 02/24/91"; 1033679Sbostic #endif /* LIBC_SCCS and not lint */ 1121379Sdist 1212691Ssam #include <sys/types.h> 137899Ssam #include <sys/socket.h> 147899Ssam #include <netdb.h> 15*46604Sbostic #include <stdio.h> 16*46604Sbostic #include <stdlib.h> 17*46604Sbostic #include <string.h> 187899Ssam 197899Ssam #define MAXALIASES 35 207899Ssam 217899Ssam static FILE *protof = NULL; 227899Ssam static char line[BUFSIZ+1]; 237899Ssam static struct protoent proto; 247899Ssam static char *proto_aliases[MAXALIASES]; 2528280Slepreau int _proto_stayopen; 267899Ssam 27*46604Sbostic void 287899Ssam setprotoent(f) 297899Ssam int f; 307899Ssam { 317899Ssam if (protof == NULL) 3242266Sbostic protof = fopen(_PATH_PROTOCOLS, "r" ); 337899Ssam else 347899Ssam rewind(protof); 3528280Slepreau _proto_stayopen |= f; 367899Ssam } 377899Ssam 38*46604Sbostic void 397899Ssam endprotoent() 407899Ssam { 4128280Slepreau if (protof) { 427899Ssam fclose(protof); 437899Ssam protof = NULL; 447899Ssam } 4528280Slepreau _proto_stayopen = 0; 467899Ssam } 477899Ssam 487899Ssam struct protoent * 497899Ssam getprotoent() 507899Ssam { 517899Ssam char *p; 527899Ssam register char *cp, **q; 537899Ssam 5442266Sbostic if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) 557899Ssam return (NULL); 567899Ssam again: 577899Ssam if ((p = fgets(line, BUFSIZ, protof)) == NULL) 587899Ssam return (NULL); 597899Ssam if (*p == '#') 607899Ssam goto again; 61*46604Sbostic cp = strpbrk(p, "#\n"); 627899Ssam if (cp == NULL) 637899Ssam goto again; 647899Ssam *cp = '\0'; 657899Ssam proto.p_name = p; 66*46604Sbostic cp = strpbrk(p, " \t"); 677899Ssam if (cp == NULL) 687899Ssam goto again; 697899Ssam *cp++ = '\0'; 707899Ssam while (*cp == ' ' || *cp == '\t') 717899Ssam cp++; 72*46604Sbostic p = strpbrk(cp, " \t"); 737899Ssam if (p != NULL) 747899Ssam *p++ = '\0'; 757899Ssam proto.p_proto = atoi(cp); 769190Ssam q = proto.p_aliases = proto_aliases; 779190Ssam if (p != NULL) { 789190Ssam cp = p; 7910087Ssam while (cp && *cp) { 809190Ssam if (*cp == ' ' || *cp == '\t') { 819190Ssam cp++; 829190Ssam continue; 839190Ssam } 849190Ssam if (q < &proto_aliases[MAXALIASES - 1]) 859190Ssam *q++ = cp; 86*46604Sbostic cp = strpbrk(cp, " \t"); 8710087Ssam if (cp != NULL) 889190Ssam *cp++ = '\0'; 897899Ssam } 907899Ssam } 917899Ssam *q = NULL; 927899Ssam return (&proto); 937899Ssam } 94