1 /*	$NetBSD: get_domainname.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	get_domainname 3
6 /* SUMMARY
7 /*	network domain name lookup
8 /* SYNOPSIS
9 /*	#include <get_domainname.h>
10 /*
11 /*	const char *get_domainname()
12 /* DESCRIPTION
13 /*	get_domainname() returns the local domain name as obtained
14 /*	by stripping the hostname component from the result from
15 /*	get_hostname().  The result is the hostname when get_hostname()
16 /*	does not return a FQDN form ("foo"), or its result has only two
17 /*	components ("foo.com").
18 /* DIAGNOSTICS
19 /*	Fatal errors: no hostname, invalid hostname.
20 /* SEE ALSO
21 /*	get_hostname(3)
22 /* LICENSE
23 /* .ad
24 /* .fi
25 /*	The Secure Mailer license must be distributed with this software.
26 /* AUTHOR(S)
27 /*	Wietse Venema
28 /*	IBM T.J. Watson Research
29 /*	P.O. Box 704
30 /*	Yorktown Heights, NY 10598, USA
31 /*--*/
32 
33 /* System library. */
34 
35 #include <sys_defs.h>
36 #include <string.h>
37 
38 /* Utility library. */
39 
40 #include "mymalloc.h"
41 #include "get_hostname.h"
42 #include "get_domainname.h"
43 
44 /* Local stuff. */
45 
46 static char *my_domain_name;
47 
48 /* get_domainname - look up my domain name */
49 
50 const char *get_domainname(void)
51 {
52     const char *host;
53     const char *dot;
54 
55     /*
56      * Use the hostname when it is not a FQDN ("foo"), or when the hostname
57      * actually is a domain name ("foo.com").
58      */
59     if (my_domain_name == 0) {
60 	host = get_hostname();
61 	if ((dot = strchr(host, '.')) == 0 || strchr(dot + 1, '.') == 0) {
62 	    my_domain_name = mystrdup(host);
63 	} else {
64 	    my_domain_name = mystrdup(dot + 1);
65 	}
66     }
67     return (my_domain_name);
68 }
69