- hostname[k] = '\0';
- err = gethostname (hostname, size);
- if (err >= 0 && hostname[k] == '\0')
- break;
- else if (err < 0 && errno != ENAMETOOLONG && errno != 0)
- error (EXIT_FAILURE, errno, "gethostname");
- size *= 2;
- hostname = xrealloc (hostname, size + 1);
+
+ 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;
+ free (hostname);
+ errno = saved_errno;
+ return NULL;
+ }