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