summaryrefslogtreecommitdiff
path: root/lib/xgethostname.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1994-05-02 04:26:07 +0000
committerJim Meyering <jim@meyering.net>1994-05-02 04:26:07 +0000
commit31185771208980da2c1c5924a960538b158880a6 (patch)
tree327fc4ab3386066f4b63a769e6b63c31ccdb9556 /lib/xgethostname.c
parentbd3061bed95fc14478e958c95fe3b53911e74890 (diff)
downloadcoreutils-31185771208980da2c1c5924a960538b158880a6.tar.xz
.
Diffstat (limited to 'lib/xgethostname.c')
-rw-r--r--lib/xgethostname.c29
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;
+}