xref: /csrg-svn/lib/libc/net/getservent.c (revision 34816)
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
6*34816Sbostic  * provided that the above copyright notice and this paragraph are
7*34816Sbostic  * duplicated in all such forms and that any documentation,
8*34816Sbostic  * advertising materials, and other materials related to such
9*34816Sbostic  * distribution and use acknowledge that the software was developed
10*34816Sbostic  * by the University of California, Berkeley.  The name of the
11*34816Sbostic  * University may not be used to endorse or promote products derived
12*34816Sbostic  * from this software without specific prior written permission.
13*34816Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34816Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34816Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621383Sdist  */
177901Ssam 
1826624Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34816Sbostic static char sccsid[] = "@(#)getservent.c	5.6 (Berkeley) 06/27/88";
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 
319808Ssam static char SERVDB[] = "/etc/services";
327901Ssam static FILE *servf = NULL;
337901Ssam static char line[BUFSIZ+1];
347901Ssam static struct servent serv;
357901Ssam static char *serv_aliases[MAXALIASES];
367901Ssam static char *any();
3728280Slepreau int _serv_stayopen;
387901Ssam 
397901Ssam setservent(f)
407901Ssam 	int f;
417901Ssam {
427901Ssam 	if (servf == NULL)
437901Ssam 		servf = fopen(SERVDB, "r" );
447901Ssam 	else
457901Ssam 		rewind(servf);
4628280Slepreau 	_serv_stayopen |= f;
477901Ssam }
487901Ssam 
497901Ssam endservent()
507901Ssam {
5128280Slepreau 	if (servf) {
527901Ssam 		fclose(servf);
537901Ssam 		servf = NULL;
547901Ssam 	}
5528280Slepreau 	_serv_stayopen = 0;
567901Ssam }
577901Ssam 
587901Ssam struct servent *
597901Ssam getservent()
607901Ssam {
617901Ssam 	char *p;
627901Ssam 	register char *cp, **q;
637901Ssam 
647901Ssam 	if (servf == NULL && (servf = fopen(SERVDB, "r" )) == NULL)
657901Ssam 		return (NULL);
667901Ssam again:
677901Ssam 	if ((p = fgets(line, BUFSIZ, servf)) == NULL)
687901Ssam 		return (NULL);
697901Ssam 	if (*p == '#')
707901Ssam 		goto again;
717901Ssam 	cp = any(p, "#\n");
727901Ssam 	if (cp == NULL)
737901Ssam 		goto again;
747901Ssam 	*cp = '\0';
757901Ssam 	serv.s_name = p;
767901Ssam 	p = any(p, " \t");
777901Ssam 	if (p == NULL)
787901Ssam 		goto again;
797901Ssam 	*p++ = '\0';
807901Ssam 	while (*p == ' ' || *p == '\t')
817901Ssam 		p++;
827901Ssam 	cp = any(p, ",/");
837901Ssam 	if (cp == NULL)
847901Ssam 		goto again;
857901Ssam 	*cp++ = '\0';
869808Ssam 	serv.s_port = htons((u_short)atoi(p));
877901Ssam 	serv.s_proto = cp;
889190Ssam 	q = serv.s_aliases = serv_aliases;
897901Ssam 	cp = any(cp, " \t");
9010087Ssam 	if (cp != NULL)
917901Ssam 		*cp++ = '\0';
9210087Ssam 	while (cp && *cp) {
9310087Ssam 		if (*cp == ' ' || *cp == '\t') {
9410087Ssam 			cp++;
9510087Ssam 			continue;
967901Ssam 		}
9710087Ssam 		if (q < &serv_aliases[MAXALIASES - 1])
9810087Ssam 			*q++ = cp;
9910087Ssam 		cp = any(cp, " \t");
10010087Ssam 		if (cp != NULL)
10110087Ssam 			*cp++ = '\0';
1027901Ssam 	}
1037901Ssam 	*q = NULL;
1047901Ssam 	return (&serv);
1057901Ssam }
1067901Ssam 
1077901Ssam static char *
1087901Ssam any(cp, match)
1097901Ssam 	register char *cp;
1107901Ssam 	char *match;
1117901Ssam {
1127901Ssam 	register char *mp, c;
1137901Ssam 
1147901Ssam 	while (c = *cp) {
1157901Ssam 		for (mp = match; *mp; mp++)
1167901Ssam 			if (*mp == c)
1177901Ssam 				return (cp);
1187901Ssam 		cp++;
1197901Ssam 	}
1207901Ssam 	return ((char *)0);
1217901Ssam }
122