]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/xalloc: add xgethostname()
authorKarel Zak <kzak@redhat.com>
Fri, 19 Oct 2012 14:23:54 +0000 (16:23 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 19 Oct 2012 14:23:54 +0000 (16:23 +0200)
The new function allocates memory by xalloc() for hostname and
fill in the buffer by gethostname().

Signed-off-by: Karel Zak <kzak@redhat.com>
include/xalloc.h

index 1704259bca804a2bfcbca5026503457dfb1c52ea..7b685e71843172d221ef94a0484de1b7cd1fee58 100644 (file)
@@ -75,4 +75,19 @@ static inline int __attribute__ ((__format__(printf, 2, 3)))
                err(XALLOC_EXIT_CODE, "cannot allocate string");
        return ret;
 }
+
+
+static inline char *xgethostname(void)
+{
+       char *name;
+       size_t sz = get_hostname_max() + 1;
+
+       name = xmalloc(sizeof(char) * sz);
+       if (gethostname(name, sz) != 0)
+               return NULL;
+
+       name[sz - 1] = '\0';
+       return name;
+}
+
 #endif