xref: /csrg-svn/lib/libc/net/res_init.c (revision 18527)
118142Sralph #ifndef lint
2*18527Sralph static char sccsid[] = "@(#)res_init.c	4.2 (Berkeley) 03/27/85";
318142Sralph #endif
418142Sralph 
518142Sralph #include <sys/types.h>
618142Sralph #include <sys/socket.h>
718142Sralph #include <netinet/in.h>
818142Sralph #include <stdio.h>
918142Sralph #include <nameser.h>
1018142Sralph #include <resolv.h>
1118142Sralph 
1218142Sralph /*
1318142Sralph  * Resolver state default settings
1418142Sralph  */
1518142Sralph struct state _res = {
1618142Sralph 	90,
1718142Sralph 	2,
1818142Sralph 	RES_RECURSE|RES_DEFNAMES,
1918142Sralph };
2018142Sralph 
2118142Sralph /*
2218142Sralph  * Read the configuration file for default settings.
2318142Sralph  * Return true if the name server address is initialized.
2418142Sralph  */
2518142Sralph res_init()
2618142Sralph {
2718142Sralph 	FILE *fp;
2818142Sralph 	char buf[BUFSIZ], *cp;
2918142Sralph 	int n;
3018142Sralph 	extern u_long inet_addr();
31*18527Sralph 	extern char *index(), *getenv();
3218142Sralph 
3318142Sralph 	_res.options |= RES_INIT;
3418142Sralph 	_res.nsaddr.sin_family = AF_INET;
3518142Sralph 	_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
36*18527Sralph 	_res.nsaddr.sin_port = HTONS(NAMESERVER_PORT);
3718142Sralph 
3818142Sralph 	/* first try reading the config file */
3918142Sralph 	if ((fp = fopen(CONFFILE, "r")) != NULL) {
40*18527Sralph 		if (fgets(_res.defdname, sizeof(_res.defdname), fp) == NULL)
4118142Sralph 			_res.defdname[0] = '\0';
4218142Sralph 		else if ((cp = index(_res.defdname, '\n')) != NULL)
4318142Sralph 			*cp = '\0';
44*18527Sralph 		if (fgets(buf, sizeof (buf), fp) != NULL)
4518142Sralph 			_res.nsaddr.sin_addr.s_addr = inet_addr(buf);
4618142Sralph 		(void) fclose(fp);
4718142Sralph 	}
4818142Sralph 
49*18527Sralph 	/* Allow user to override the local domain definition */
50*18527Sralph 	if ((cp = getenv("LOCALDOMAIN")) != NULL)
51*18527Sralph 		strncpy(_res.defdname, cp, sizeof(_res.defdname));
5218142Sralph 
5318142Sralph 	return (0);
5418142Sralph }
55