aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/gethostname.bi
blob: 9914a574ee5c155bc98b99450ffbeeccdd922b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2009-2018 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

MF_BUILTIN_MODULE

#ifndef ENAMETOOLONG
# define ENAMETOOLONG 0
#endif

#ifndef INITIAL_HOSTNAME_LENGTH
# define INITIAL_HOSTNAME_LENGTH 34
#endif

#ifndef INITIAL_DOMAINNAME_LENGTH
# define INITIAL_DOMAINNAME_LENGTH 34
#endif

/* string gethostname()

   This implementation is based on xgethostname by Jim Meyering
*/
MF_DEFUN(gethostname, STRING, OPTIONAL, NUMBER dns)
{
	size_t size = INITIAL_HOSTNAME_LENGTH;
	
	MF_OBSTACK_BEGIN();
	while (1) {
		size_t nsize;
		size_t size_1;
		char *hostname;

		MF_OBSTACK_GROW(NULL, size);
		hostname = MF_OBSTACK_BASE();

                /* Use SIZE_1 here rather than SIZE to work around the bug in
		   SunOS 5.5's gethostname whereby it NUL-terminates HOSTNAME
		   even when the name is as long as the supplied buffer.  */
		size_1 = size - 1;
		hostname[size_1 - 1] = '\0';
		errno = 0;

		if (gethostname(hostname, size_1) == 0)	{
			if (!hostname[size_1 - 1])
				break;
		} else if (errno != 0
			   && errno != ENAMETOOLONG && errno != EINVAL
			   /* OSX/Darwin does this when the buffer is not
			      large enough */
			   && errno != ENOMEM) {
			int saved_errno = errno;
			MF_OBSTACK_CANCEL();
			MF_THROW(mfe_failure,
				 "%s", mu_strerror(saved_errno));
		}

		nsize = size * 2;
		if (nsize <= size) {
			MF_OBSTACK_CANCEL();
			MF_THROW(mfe_failure,
				 "%s", mu_strerror(ENOMEM));
		}
		size = nsize;
	}

	if (MF_OPTVAL(dns)) {
		struct hostent *hp;
		char *ptr = MF_OBSTACK_BASE;
		
		hp = gethostbyname(ptr);
		if (hp) {
			size_t nlen = strlen(hp->h_name);
			if (nlen >= size)
				MF_OBSTACK_GROW(NULL,
						nlen - size + 1);
			strcpy(ptr, hp->h_name);
		}
	}
	
	MF_RETURN_OBSTACK();
}
END

/* string getdomainname()

   This implementation is based on xgetdomainname by Jim Meyering
*/
MF_DEFUN(getdomainname, STRING)
{
	size_t size = INITIAL_DOMAINNAME_LENGTH;
	
	MF_OBSTACK_BEGIN();
	while (1) {
		size_t nsize;
		size_t size_1;
		char *domainname;
		int rc;
		
		MF_OBSTACK_GROW(NULL, size);
		domainname = MF_OBSTACK_BASE();

		size_1 = size - 1;
		domainname[size_1 - 1] = '\0';
		errno = 0;

		rc = getdomainname(domainname, size);
		if (rc >= 0 && domainname[size_1] == '\0')
			break;
		else if (rc < 0 && errno != EINVAL) {
			int saved_errno = errno;
			MF_OBSTACK_CANCEL();
			MF_THROW(mfe_failure,
				 "%s", mu_strerror(saved_errno));
		}

		nsize = size * 2;
		if (nsize <= size) {
			MF_OBSTACK_CANCEL();
			MF_THROW(mfe_failure,
				 "%s", mu_strerror(ENOMEM));
		}
		size = nsize;
	}
	MF_RETURN_OBSTACK();
}
END

Return to:

Send suggestions and report system problems to the System administrator.