+
+char *get_hostname(void)
+{
+ int len = 128;
+ char *hostname = NULL;
+ do {
+ len *= 2;
+ hostname = sresize(hostname, len, char);
+ if ((gethostname(hostname, len) < 0) &&
+ (errno != ENAMETOOLONG)) {
+ sfree(hostname);
+ hostname = NULL;
+ break;
+ }
+ } while (strlen(hostname) >= len-1);
+ return hostname;
+}
+
+SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum)
+{
+ SockAddr ret = snew(struct SockAddr_tag);
+ int n;
+
+ memset(ret, 0, sizeof *ret);
+ ret->superfamily = UNIX;
+ /*
+ * In special circumstances (notably Mac OS X Leopard), we'll
+ * have been passed an explicit Unix socket path.
+ */
+ if (sockpath) {
+ n = snprintf(ret->hostname, sizeof ret->hostname,
+ "%s", sockpath);
+ } else {
+ n = snprintf(ret->hostname, sizeof ret->hostname,
+ "%s%d", X11_UNIX_PATH, displaynum);
+ }
+
+ if (n < 0)
+ ret->error = "snprintf failed";
+ else if (n >= sizeof ret->hostname)
+ ret->error = "X11 UNIX name too long";
+
+#ifndef NO_IPV6
+ ret->ais = NULL;
+#else
+ ret->addresses = NULL;
+ ret->naddresses = 0;
+#endif
+ ret->refcount = 1;
+ return ret;
+}