xref: /csrg-svn/lib/libc/net/getservent.c (revision 42626)
121383Sdist /*
221383Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
5*42626Sbostic  * %sccs.include.redist.c%
621383Sdist  */
77901Ssam 
826624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42626Sbostic static char sccsid[] = "@(#)getservent.c	5.8 (Berkeley) 06/01/90";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121383Sdist 
127901Ssam #include <stdio.h>
1332104Smckusick #include <sys/param.h>
149808Ssam #include <sys/types.h>
157901Ssam #include <sys/socket.h>
167901Ssam #include <netdb.h>
177901Ssam #include <ctype.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];
257901Ssam static char *any();
2628280Slepreau int _serv_stayopen;
277901Ssam 
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 
387901Ssam endservent()
397901Ssam {
4028280Slepreau 	if (servf) {
417901Ssam 		fclose(servf);
427901Ssam 		servf = NULL;
437901Ssam 	}
4428280Slepreau 	_serv_stayopen = 0;
457901Ssam }
467901Ssam 
477901Ssam struct servent *
487901Ssam getservent()
497901Ssam {
507901Ssam 	char *p;
517901Ssam 	register char *cp, **q;
527901Ssam 
5342266Sbostic 	if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
547901Ssam 		return (NULL);
557901Ssam again:
567901Ssam 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
577901Ssam 		return (NULL);
587901Ssam 	if (*p == '#')
597901Ssam 		goto again;
607901Ssam 	cp = any(p, "#\n");
617901Ssam 	if (cp == NULL)
627901Ssam 		goto again;
637901Ssam 	*cp = '\0';
647901Ssam 	serv.s_name = p;
657901Ssam 	p = any(p, " \t");
667901Ssam 	if (p == NULL)
677901Ssam 		goto again;
687901Ssam 	*p++ = '\0';
697901Ssam 	while (*p == ' ' || *p == '\t')
707901Ssam 		p++;
717901Ssam 	cp = any(p, ",/");
727901Ssam 	if (cp == NULL)
737901Ssam 		goto again;
747901Ssam 	*cp++ = '\0';
759808Ssam 	serv.s_port = htons((u_short)atoi(p));
767901Ssam 	serv.s_proto = cp;
779190Ssam 	q = serv.s_aliases = serv_aliases;
787901Ssam 	cp = any(cp, " \t");
7910087Ssam 	if (cp != NULL)
807901Ssam 		*cp++ = '\0';
8110087Ssam 	while (cp && *cp) {
8210087Ssam 		if (*cp == ' ' || *cp == '\t') {
8310087Ssam 			cp++;
8410087Ssam 			continue;
857901Ssam 		}
8610087Ssam 		if (q < &serv_aliases[MAXALIASES - 1])
8710087Ssam 			*q++ = cp;
8810087Ssam 		cp = any(cp, " \t");
8910087Ssam 		if (cp != NULL)
9010087Ssam 			*cp++ = '\0';
917901Ssam 	}
927901Ssam 	*q = NULL;
937901Ssam 	return (&serv);
947901Ssam }
957901Ssam 
967901Ssam static char *
977901Ssam any(cp, match)
987901Ssam 	register char *cp;
997901Ssam 	char *match;
1007901Ssam {
1017901Ssam 	register char *mp, c;
1027901Ssam 
1037901Ssam 	while (c = *cp) {
1047901Ssam 		for (mp = match; *mp; mp++)
1057901Ssam 			if (*mp == c)
1067901Ssam 				return (cp);
1077901Ssam 		cp++;
1087901Ssam 	}
1097901Ssam 	return ((char *)0);
1107901Ssam }
111