xref: /csrg-svn/lib/libc/net/res_init.c (revision 18531)
1 #ifndef lint
2 static char sccsid[] = "@(#)res_init.c	4.3 (Berkeley) 03/28/85";
3 #endif
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <stdio.h>
9 #include <nameser.h>
10 #include <resolv.h>
11 
12 /*
13  * Resolver state default settings
14  */
15 struct state _res = {
16 	90,
17 	2,
18 	RES_RECURSE|RES_DEFNAMES,
19 };
20 
21 /*
22  * Read the configuration file for default settings.
23  * Return true if the name server address is initialized.
24  */
25 res_init()
26 {
27 	FILE *fp;
28 	char buf[BUFSIZ], *cp;
29 	int n;
30 	extern u_long inet_addr();
31 	extern char *index(), *getenv();
32 
33 	_res.options |= RES_INIT;
34 	_res.nsaddr.sin_family = AF_INET;
35 	_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
36 	_res.nsaddr.sin_port = HTONS(NAMESERVER_PORT);
37 
38 	/* first try reading the config file */
39 	if ((fp = fopen(CONFFILE, "r")) != NULL) {
40 		if (fgets(_res.defdname, sizeof(_res.defdname), fp) == NULL)
41 			_res.defdname[0] = '\0';
42 		else if ((cp = index(_res.defdname, '\n')) != NULL)
43 			*cp = '\0';
44 		if (fgets(buf, sizeof (buf), fp) != NULL)
45 			_res.nsaddr.sin_addr.s_addr = inet_addr(buf);
46 		(void) fclose(fp);
47 	}
48 
49 	/* Allow user to override the local domain definition */
50 	if ((cp = getenv("LOCALDOMAIN")) != NULL)
51 		strncpy(_res.defdname, cp, sizeof(_res.defdname));
52 }
53