diff options
-rw-r--r-- | lib/xgethostname.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/xgethostname.c b/lib/xgethostname.c new file mode 100644 index 000000000..f00d7bd5f --- /dev/null +++ b/lib/xgethostname.c @@ -0,0 +1,29 @@ +#include <sys/types.h> + +int gethostname (); +char *xmalloc (); +char *xrealloc (); + +#define INITIAL_HOSTNAME_LENGTH 33 + +char * +xgethostname () +{ + char *hostname; + size_t size; + int err; + + size = INITIAL_HOSTNAME_LENGTH; + while (1) + { + hostname = xmalloc (size); + hostname[size - 1] = '\0'; + err = gethostname (hostname, size); + if (err || hostname[size - 1] == '\0') + break; + size *= 2; + hostname = xrealloc (hostname, size); + } + + return hostname; +} |