~ubuntu-branches/ubuntu/vivid/postfix/vivid-proposed

« back to all changes in this revision

Viewing changes to src/util/get_hostname.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      get_hostname 3
 
4
/* SUMMARY
 
5
/*      network name lookup
 
6
/* SYNOPSIS
 
7
/*      #include <get_hostname.h>
 
8
/*
 
9
/*      const char *get_hostname()
 
10
/* DESCRIPTION
 
11
/*      get_hostname() returns the local hostname as obtained
 
12
/*      via gethostname() or its moral equivalent. This routine
 
13
/*      goes to great length to avoid dependencies on any network
 
14
/*      services.
 
15
/* DIAGNOSTICS
 
16
/*      Fatal errors: no hostname, invalid hostname.
 
17
/* SEE ALSO
 
18
/*      valid_hostname(3)
 
19
/* LICENSE
 
20
/* .ad
 
21
/* .fi
 
22
/*      The Secure Mailer license must be distributed with this software.
 
23
/* AUTHOR(S)
 
24
/*      Wietse Venema
 
25
/*      IBM T.J. Watson Research
 
26
/*      P.O. Box 704
 
27
/*      Yorktown Heights, NY 10598, USA
 
28
/*--*/
 
29
 
 
30
/* System library. */
 
31
 
 
32
#include <sys_defs.h>
 
33
#include <sys/param.h>
 
34
#include <string.h>
 
35
#include <unistd.h>
 
36
 
 
37
#if (MAXHOSTNAMELEN < 256)
 
38
#undef MAXHOSTNAMELEN
 
39
#define MAXHOSTNAMELEN  256
 
40
#endif
 
41
 
 
42
/* Utility library. */
 
43
 
 
44
#include "mymalloc.h"
 
45
#include "msg.h"
 
46
#include "valid_hostname.h"
 
47
#include "get_hostname.h"
 
48
 
 
49
/* Local stuff. */
 
50
 
 
51
static char *my_host_name;
 
52
 
 
53
/* get_hostname - look up my host name */
 
54
 
 
55
const char *get_hostname(void)
 
56
{
 
57
    char    namebuf[MAXHOSTNAMELEN + 1];
 
58
 
 
59
    /*
 
60
     * The gethostname() call is not (or not yet) in ANSI or POSIX, but it is
 
61
     * part of the socket interface library. We avoid the more politically-
 
62
     * correct uname() routine because that has no portable way of dealing
 
63
     * with long (FQDN) hostnames.
 
64
     */
 
65
    if (my_host_name == 0) {
 
66
        if (gethostname(namebuf, sizeof(namebuf)) < 0)
 
67
            msg_fatal("gethostname: %m");
 
68
        namebuf[MAXHOSTNAMELEN] = 0;
 
69
        if (valid_hostname(namebuf, DO_GRIPE) == 0)
 
70
            msg_fatal("unable to use my own hostname");
 
71
        my_host_name = mystrdup(namebuf);
 
72
    }
 
73
    return (my_host_name);
 
74
}