121379Sdist /* 221379Sdist * Copyright (c) 1983 Regents of the University of California. 321379Sdist * All rights reserved. The Berkeley software License Agreement 421379Sdist * specifies the terms and conditions for redistribution. 521379Sdist */ 67899Ssam 726620Sdonn #if defined(LIBC_SCCS) && !defined(lint) 8*28280Slepreau static char sccsid[] = "@(#)getprotoent.c 5.3 (Berkeley) 05/19/86"; 926620Sdonn #endif LIBC_SCCS and not lint 1021379Sdist 117899Ssam #include <stdio.h> 1212691Ssam #include <sys/types.h> 137899Ssam #include <sys/socket.h> 147899Ssam #include <netdb.h> 157899Ssam #include <ctype.h> 167899Ssam 177899Ssam #define MAXALIASES 35 187899Ssam 199808Ssam static char PROTODB[] = "/etc/protocols"; 207899Ssam static FILE *protof = NULL; 217899Ssam static char line[BUFSIZ+1]; 227899Ssam static struct protoent proto; 237899Ssam static char *proto_aliases[MAXALIASES]; 247899Ssam static char *any(); 25*28280Slepreau int _proto_stayopen; 267899Ssam 277899Ssam setprotoent(f) 287899Ssam int f; 297899Ssam { 307899Ssam if (protof == NULL) 317899Ssam protof = fopen(PROTODB, "r" ); 327899Ssam else 337899Ssam rewind(protof); 34*28280Slepreau _proto_stayopen |= f; 357899Ssam } 367899Ssam 377899Ssam endprotoent() 387899Ssam { 39*28280Slepreau if (protof) { 407899Ssam fclose(protof); 417899Ssam protof = NULL; 427899Ssam } 43*28280Slepreau _proto_stayopen = 0; 447899Ssam } 457899Ssam 467899Ssam struct protoent * 477899Ssam getprotoent() 487899Ssam { 497899Ssam char *p; 507899Ssam register char *cp, **q; 517899Ssam 527899Ssam if (protof == NULL && (protof = fopen(PROTODB, "r" )) == NULL) 537899Ssam return (NULL); 547899Ssam again: 557899Ssam if ((p = fgets(line, BUFSIZ, protof)) == NULL) 567899Ssam return (NULL); 577899Ssam if (*p == '#') 587899Ssam goto again; 597899Ssam cp = any(p, "#\n"); 607899Ssam if (cp == NULL) 617899Ssam goto again; 627899Ssam *cp = '\0'; 637899Ssam proto.p_name = p; 647899Ssam cp = any(p, " \t"); 657899Ssam if (cp == NULL) 667899Ssam goto again; 677899Ssam *cp++ = '\0'; 687899Ssam while (*cp == ' ' || *cp == '\t') 697899Ssam cp++; 707899Ssam p = any(cp, " \t"); 717899Ssam if (p != NULL) 727899Ssam *p++ = '\0'; 737899Ssam proto.p_proto = atoi(cp); 749190Ssam q = proto.p_aliases = proto_aliases; 759190Ssam if (p != NULL) { 769190Ssam cp = p; 7710087Ssam while (cp && *cp) { 789190Ssam if (*cp == ' ' || *cp == '\t') { 799190Ssam cp++; 809190Ssam continue; 819190Ssam } 829190Ssam if (q < &proto_aliases[MAXALIASES - 1]) 839190Ssam *q++ = cp; 849190Ssam cp = any(cp, " \t"); 8510087Ssam if (cp != NULL) 869190Ssam *cp++ = '\0'; 877899Ssam } 887899Ssam } 897899Ssam *q = NULL; 907899Ssam return (&proto); 917899Ssam } 927899Ssam 937899Ssam static char * 947899Ssam any(cp, match) 957899Ssam register char *cp; 967899Ssam char *match; 977899Ssam { 987899Ssam register char *mp, c; 997899Ssam 1007899Ssam while (c = *cp) { 1017899Ssam for (mp = match; *mp; mp++) 1027899Ssam if (*mp == c) 1037899Ssam return (cp); 1047899Ssam cp++; 1057899Ssam } 1067899Ssam return ((char *)0); 1077899Ssam } 108