xref: /csrg-svn/lib/libc/net/getservent.c (revision 42266)
121383Sdist /*
221383Sdist  * Copyright (c) 1983 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
533679Sbostic  * Redistribution and use in source and binary forms are permitted
634816Sbostic  * provided that the above copyright notice and this paragraph are
734816Sbostic  * duplicated in all such forms and that any documentation,
834816Sbostic  * advertising materials, and other materials related to such
934816Sbostic  * distribution and use acknowledge that the software was developed
1034816Sbostic  * by the University of California, Berkeley.  The name of the
1134816Sbostic  * University may not be used to endorse or promote products derived
1234816Sbostic  * from this software without specific prior written permission.
1334816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621383Sdist  */
177901Ssam 
1826624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*42266Sbostic static char sccsid[] = "@(#)getservent.c	5.7 (Berkeley) 05/21/90";
2033679Sbostic #endif /* LIBC_SCCS and not lint */
2121383Sdist 
227901Ssam #include <stdio.h>
2332104Smckusick #include <sys/param.h>
249808Ssam #include <sys/types.h>
257901Ssam #include <sys/socket.h>
267901Ssam #include <netdb.h>
277901Ssam #include <ctype.h>
287901Ssam 
297901Ssam #define	MAXALIASES	35
307901Ssam 
317901Ssam static FILE *servf = NULL;
327901Ssam static char line[BUFSIZ+1];
337901Ssam static struct servent serv;
347901Ssam static char *serv_aliases[MAXALIASES];
357901Ssam static char *any();
3628280Slepreau int _serv_stayopen;
377901Ssam 
387901Ssam setservent(f)
397901Ssam 	int f;
407901Ssam {
417901Ssam 	if (servf == NULL)
42*42266Sbostic 		servf = fopen(_PATH_SERVICES, "r" );
437901Ssam 	else
447901Ssam 		rewind(servf);
4528280Slepreau 	_serv_stayopen |= f;
467901Ssam }
477901Ssam 
487901Ssam endservent()
497901Ssam {
5028280Slepreau 	if (servf) {
517901Ssam 		fclose(servf);
527901Ssam 		servf = NULL;
537901Ssam 	}
5428280Slepreau 	_serv_stayopen = 0;
557901Ssam }
567901Ssam 
577901Ssam struct servent *
587901Ssam getservent()
597901Ssam {
607901Ssam 	char *p;
617901Ssam 	register char *cp, **q;
627901Ssam 
63*42266Sbostic 	if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
647901Ssam 		return (NULL);
657901Ssam again:
667901Ssam 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
677901Ssam 		return (NULL);
687901Ssam 	if (*p == '#')
697901Ssam 		goto again;
707901Ssam 	cp = any(p, "#\n");
717901Ssam 	if (cp == NULL)
727901Ssam 		goto again;
737901Ssam 	*cp = '\0';
747901Ssam 	serv.s_name = p;
757901Ssam 	p = any(p, " \t");
767901Ssam 	if (p == NULL)
777901Ssam 		goto again;
787901Ssam 	*p++ = '\0';
797901Ssam 	while (*p == ' ' || *p == '\t')
807901Ssam 		p++;
817901Ssam 	cp = any(p, ",/");
827901Ssam 	if (cp == NULL)
837901Ssam 		goto again;
847901Ssam 	*cp++ = '\0';
859808Ssam 	serv.s_port = htons((u_short)atoi(p));
867901Ssam 	serv.s_proto = cp;
879190Ssam 	q = serv.s_aliases = serv_aliases;
887901Ssam 	cp = any(cp, " \t");
8910087Ssam 	if (cp != NULL)
907901Ssam 		*cp++ = '\0';
9110087Ssam 	while (cp && *cp) {
9210087Ssam 		if (*cp == ' ' || *cp == '\t') {
9310087Ssam 			cp++;
9410087Ssam 			continue;
957901Ssam 		}
9610087Ssam 		if (q < &serv_aliases[MAXALIASES - 1])
9710087Ssam 			*q++ = cp;
9810087Ssam 		cp = any(cp, " \t");
9910087Ssam 		if (cp != NULL)
10010087Ssam 			*cp++ = '\0';
1017901Ssam 	}
1027901Ssam 	*q = NULL;
1037901Ssam 	return (&serv);
1047901Ssam }
1057901Ssam 
1067901Ssam static char *
1077901Ssam any(cp, match)
1087901Ssam 	register char *cp;
1097901Ssam 	char *match;
1107901Ssam {
1117901Ssam 	register char *mp, c;
1127901Ssam 
1137901Ssam 	while (c = *cp) {
1147901Ssam 		for (mp = match; *mp; mp++)
1157901Ssam 			if (*mp == c)
1167901Ssam 				return (cp);
1177901Ssam 		cp++;
1187901Ssam 	}
1197901Ssam 	return ((char *)0);
1207901Ssam }
121