aboutsummaryrefslogtreecommitdiffstats
path: root/unix/x0vncserver
diff options
context:
space:
mode:
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>2021-07-14 14:01:15 +0100
committerAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>2021-07-15 09:44:11 +0100
commite6a5a3684a4ad5907bd6e820d850cca1bbd7d380 (patch)
treeb974bd8d387d9a2b5f0d08f53446c62e0788cdb5 /unix/x0vncserver
parentdfc9421dcf0fc97ad99638df501b95cb162e95b2 (diff)
downloadtigervnc-e6a5a3684a4ad5907bd6e820d850cca1bbd7d380.tar.gz
tigervnc-e6a5a3684a4ad5907bd6e820d850cca1bbd7d380.zip
Fix build on systems without HOST_NAME_MAX
Some operating systems such as FreeBSD don't define a HOST_NAME_MAX macro. The portable approach to determine the real host name limit is calling sysconf(_SC_HOST_NAME_MAX) so do that instead.
Diffstat (limited to 'unix/x0vncserver')
-rw-r--r--unix/x0vncserver/x0vncserver.cxx22
1 files changed, 9 insertions, 13 deletions
diff --git a/unix/x0vncserver/x0vncserver.cxx b/unix/x0vncserver/x0vncserver.cxx
index 89c9817e..29bc3d4c 100644
--- a/unix/x0vncserver/x0vncserver.cxx
+++ b/unix/x0vncserver/x0vncserver.cxx
@@ -71,29 +71,25 @@ BoolParameter localhostOnly("localhost",
static const char* defaultDesktopName()
{
- static char* name = NULL;
-
- char hostname[HOST_NAME_MAX + 1];
- struct passwd* pwent;
-
- size_t len;
-
- delete [] name;
+ size_t host_max = sysconf(_SC_HOST_NAME_MAX);
+ if (host_max < 0)
+ return "";
- if (gethostname(hostname, sizeof(hostname)) == -1)
+ std::vector<char> hostname(host_max + 1);
+ if (gethostname(hostname.data(), hostname.size()) == -1)
return "";
- pwent = getpwuid(getuid());
+ struct passwd* pwent = getpwuid(getuid());
if (pwent == NULL)
return "";
- len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname);
+ size_t len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname.data());
if (len < 0)
return "";
- name = new char[len + 1];
+ char* name = new char[len + 1];
- snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname);
+ snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname.data());
return name;
}