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