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