diff options
author | Jim Meyering <jim@meyering.net> | 1994-05-02 04:26:07 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1994-05-02 04:26:07 +0000 |
commit | 31185771208980da2c1c5924a960538b158880a6 (patch) | |
tree | 327fc4ab3386066f4b63a769e6b63c31ccdb9556 /lib/xgethostname.c | |
parent | bd3061bed95fc14478e958c95fe3b53911e74890 (diff) | |
download | coreutils-31185771208980da2c1c5924a960538b158880a6.tar.xz |
.
Diffstat (limited to 'lib/xgethostname.c')
-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; +} |