xref: /csrg-svn/lib/libc/net/getservent.c (revision 26624)
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 
7*26624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*26624Sdonn static char sccsid[] = "@(#)getservent.c	5.2 (Berkeley) 03/09/86";
9*26624Sdonn #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 int stayopen = 0;
257901Ssam static char *any();
267901Ssam 
277901Ssam setservent(f)
287901Ssam 	int f;
297901Ssam {
307901Ssam 	if (servf == NULL)
317901Ssam 		servf = fopen(SERVDB, "r" );
327901Ssam 	else
337901Ssam 		rewind(servf);
347901Ssam 	stayopen |= f;
357901Ssam }
367901Ssam 
377901Ssam endservent()
387901Ssam {
397901Ssam 	if (servf && !stayopen) {
407901Ssam 		fclose(servf);
417901Ssam 		servf = NULL;
427901Ssam 	}
437901Ssam }
447901Ssam 
457901Ssam struct servent *
467901Ssam getservent()
477901Ssam {
487901Ssam 	char *p;
497901Ssam 	register char *cp, **q;
507901Ssam 
517901Ssam 	if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
527901Ssam 		return (NULL);
537901Ssam again:
547901Ssam 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
557901Ssam 		return (NULL);
567901Ssam 	if (*p == '#')
577901Ssam 		goto again;
587901Ssam 	cp = any(p, "#\n");
597901Ssam 	if (cp == NULL)
607901Ssam 		goto again;
617901Ssam 	*cp = '\0';
627901Ssam 	serv.s_name = p;
637901Ssam 	p = any(p, " \t");
647901Ssam 	if (p == NULL)
657901Ssam 		goto again;
667901Ssam 	*p++ = '\0';
677901Ssam 	while (*p == ' ' || *p == '\t')
687901Ssam 		p++;
697901Ssam 	cp = any(p, ",/");
707901Ssam 	if (cp == NULL)
717901Ssam 		goto again;
727901Ssam 	*cp++ = '\0';
739808Ssam 	serv.s_port = htons((u_short)atoi(p));
747901Ssam 	serv.s_proto = cp;
759190Ssam 	q = serv.s_aliases = serv_aliases;
767901Ssam 	cp = any(cp, " \t");
7710087Ssam 	if (cp != NULL)
787901Ssam 		*cp++ = '\0';
7910087Ssam 	while (cp && *cp) {
8010087Ssam 		if (*cp == ' ' || *cp == '\t') {
8110087Ssam 			cp++;
8210087Ssam 			continue;
837901Ssam 		}
8410087Ssam 		if (q < &serv_aliases[MAXALIASES - 1])
8510087Ssam 			*q++ = cp;
8610087Ssam 		cp = any(cp, " \t");
8710087Ssam 		if (cp != NULL)
8810087Ssam 			*cp++ = '\0';
897901Ssam 	}
907901Ssam 	*q = NULL;
917901Ssam 	return (&serv);
927901Ssam }
937901Ssam 
947901Ssam static char *
957901Ssam any(cp, match)
967901Ssam 	register char *cp;
977901Ssam 	char *match;
987901Ssam {
997901Ssam 	register char *mp, c;
1007901Ssam 
1017901Ssam 	while (c = *cp) {
1027901Ssam 		for (mp = match; *mp; mp++)
1037901Ssam 			if (*mp == c)
1047901Ssam 				return (cp);
1057901Ssam 		cp++;
1067901Ssam 	}
1077901Ssam 	return ((char *)0);
1087901Ssam }
109