xref: /csrg-svn/lib/libc/net/getservent.c (revision 28280)
121383Sdist /*
221383Sdist  * Copyright (c) 1983 Regents of the University of California.
321383Sdist  * All rights reserved.  The Berkeley software License Agreement
421383Sdist  * specifies the terms and conditions for redistribution.
521383Sdist  */
67901Ssam 
726624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*28280Slepreau static char sccsid[] = "@(#)getservent.c	5.3 (Berkeley) 05/19/86";
926624Sdonn #endif LIBC_SCCS and not lint
1021383Sdist 
117901Ssam #include <stdio.h>
129808Ssam #include <sys/types.h>
137901Ssam #include <sys/socket.h>
147901Ssam #include <netdb.h>
157901Ssam #include <ctype.h>
167901Ssam 
177901Ssam #define	MAXALIASES	35
187901Ssam 
199808Ssam static char SERVDB[] = "/etc/services";
207901Ssam static FILE *servf = NULL;
217901Ssam static char line[BUFSIZ+1];
227901Ssam static struct servent serv;
237901Ssam static char *serv_aliases[MAXALIASES];
247901Ssam static char *any();
25*28280Slepreau int _serv_stayopen;
267901Ssam 
277901Ssam setservent(f)
287901Ssam 	int f;
297901Ssam {
307901Ssam 	if (servf == NULL)
317901Ssam 		servf = fopen(SERVDB, "r" );
327901Ssam 	else
337901Ssam 		rewind(servf);
34*28280Slepreau 	_serv_stayopen |= f;
357901Ssam }
367901Ssam 
377901Ssam endservent()
387901Ssam {
39*28280Slepreau 	if (servf) {
407901Ssam 		fclose(servf);
417901Ssam 		servf = NULL;
427901Ssam 	}
43*28280Slepreau 	_serv_stayopen = 0;
447901Ssam }
457901Ssam 
467901Ssam struct servent *
477901Ssam getservent()
487901Ssam {
497901Ssam 	char *p;
507901Ssam 	register char *cp, **q;
517901Ssam 
527901Ssam 	if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
537901Ssam 		return (NULL);
547901Ssam again:
557901Ssam 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
567901Ssam 		return (NULL);
577901Ssam 	if (*p == '#')
587901Ssam 		goto again;
597901Ssam 	cp = any(p, "#\n");
607901Ssam 	if (cp == NULL)
617901Ssam 		goto again;
627901Ssam 	*cp = '\0';
637901Ssam 	serv.s_name = p;
647901Ssam 	p = any(p, " \t");
657901Ssam 	if (p == NULL)
667901Ssam 		goto again;
677901Ssam 	*p++ = '\0';
687901Ssam 	while (*p == ' ' || *p == '\t')
697901Ssam 		p++;
707901Ssam 	cp = any(p, ",/");
717901Ssam 	if (cp == NULL)
727901Ssam 		goto again;
737901Ssam 	*cp++ = '\0';
749808Ssam 	serv.s_port = htons((u_short)atoi(p));
757901Ssam 	serv.s_proto = cp;
769190Ssam 	q = serv.s_aliases = serv_aliases;
777901Ssam 	cp = any(cp, " \t");
7810087Ssam 	if (cp != NULL)
797901Ssam 		*cp++ = '\0';
8010087Ssam 	while (cp && *cp) {
8110087Ssam 		if (*cp == ' ' || *cp == '\t') {
8210087Ssam 			cp++;
8310087Ssam 			continue;
847901Ssam 		}
8510087Ssam 		if (q < &serv_aliases[MAXALIASES - 1])
8610087Ssam 			*q++ = cp;
8710087Ssam 		cp = any(cp, " \t");
8810087Ssam 		if (cp != NULL)
8910087Ssam 			*cp++ = '\0';
907901Ssam 	}
917901Ssam 	*q = NULL;
927901Ssam 	return (&serv);
937901Ssam }
947901Ssam 
957901Ssam static char *
967901Ssam any(cp, match)
977901Ssam 	register char *cp;
987901Ssam 	char *match;
997901Ssam {
1007901Ssam 	register char *mp, c;
1017901Ssam 
1027901Ssam 	while (c = *cp) {
1037901Ssam 		for (mp = match; *mp; mp++)
1047901Ssam 			if (*mp == c)
1057901Ssam 				return (cp);
1067901Ssam 		cp++;
1077901Ssam 	}
1087901Ssam 	return ((char *)0);
1097901Ssam }
110