121383Sdist /* 221383Sdist * Copyright (c) 1983 Regents of the University of California. 333679Sbostic * All rights reserved. 433679Sbostic * 542626Sbostic * %sccs.include.redist.c% 621383Sdist */ 77901Ssam 826624Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46604Sbostic static char sccsid[] = "@(#)getservent.c 5.9 (Berkeley) 02/24/91"; 1033679Sbostic #endif /* LIBC_SCCS and not lint */ 1121383Sdist 129808Ssam #include <sys/types.h> 137901Ssam #include <sys/socket.h> 147901Ssam #include <netdb.h> 15*46604Sbostic #include <stdio.h> 16*46604Sbostic #include <string.h> 17*46604Sbostic #include <stdlib.h> 187901Ssam 197901Ssam #define MAXALIASES 35 207901Ssam 217901Ssam static FILE *servf = NULL; 227901Ssam static char line[BUFSIZ+1]; 237901Ssam static struct servent serv; 247901Ssam static char *serv_aliases[MAXALIASES]; 2528280Slepreau int _serv_stayopen; 267901Ssam 27*46604Sbostic void 287901Ssam setservent(f) 297901Ssam int f; 307901Ssam { 317901Ssam if (servf == NULL) 3242266Sbostic servf = fopen(_PATH_SERVICES, "r" ); 337901Ssam else 347901Ssam rewind(servf); 3528280Slepreau _serv_stayopen |= f; 367901Ssam } 377901Ssam 38*46604Sbostic void 397901Ssam endservent() 407901Ssam { 4128280Slepreau if (servf) { 427901Ssam fclose(servf); 437901Ssam servf = NULL; 447901Ssam } 4528280Slepreau _serv_stayopen = 0; 467901Ssam } 477901Ssam 487901Ssam struct servent * 497901Ssam getservent() 507901Ssam { 517901Ssam char *p; 527901Ssam register char *cp, **q; 537901Ssam 5442266Sbostic if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) 557901Ssam return (NULL); 567901Ssam again: 577901Ssam if ((p = fgets(line, BUFSIZ, servf)) == NULL) 587901Ssam return (NULL); 597901Ssam if (*p == '#') 607901Ssam goto again; 61*46604Sbostic cp = strpbrk(p, "#\n"); 627901Ssam if (cp == NULL) 637901Ssam goto again; 647901Ssam *cp = '\0'; 657901Ssam serv.s_name = p; 66*46604Sbostic p = strpbrk(p, " \t"); 677901Ssam if (p == NULL) 687901Ssam goto again; 697901Ssam *p++ = '\0'; 707901Ssam while (*p == ' ' || *p == '\t') 717901Ssam p++; 72*46604Sbostic cp = strpbrk(p, ",/"); 737901Ssam if (cp == NULL) 747901Ssam goto again; 757901Ssam *cp++ = '\0'; 769808Ssam serv.s_port = htons((u_short)atoi(p)); 777901Ssam serv.s_proto = cp; 789190Ssam q = serv.s_aliases = serv_aliases; 79*46604Sbostic cp = strpbrk(cp, " \t"); 8010087Ssam if (cp != NULL) 817901Ssam *cp++ = '\0'; 8210087Ssam while (cp && *cp) { 8310087Ssam if (*cp == ' ' || *cp == '\t') { 8410087Ssam cp++; 8510087Ssam continue; 867901Ssam } 8710087Ssam if (q < &serv_aliases[MAXALIASES - 1]) 8810087Ssam *q++ = cp; 89*46604Sbostic cp = strpbrk(cp, " \t"); 9010087Ssam if (cp != NULL) 9110087Ssam *cp++ = '\0'; 927901Ssam } 937901Ssam *q = NULL; 947901Ssam return (&serv); 957901Ssam } 96