diff options
author | Karel Zak | 2012-10-19 16:23:54 +0200 |
---|---|---|
committer | Karel Zak | 2012-10-19 16:23:54 +0200 |
commit | 1b2aa6293d10d9d1b12c8620ab9e4436e678843b (patch) | |
tree | f6f4a6d3613313b00c1ac9104db7c83551ed7ad4 /include/xalloc.h | |
parent | include/env: unify indentation (diff) | |
download | kernel-qcow2-util-linux-1b2aa6293d10d9d1b12c8620ab9e4436e678843b.tar.gz kernel-qcow2-util-linux-1b2aa6293d10d9d1b12c8620ab9e4436e678843b.tar.xz kernel-qcow2-util-linux-1b2aa6293d10d9d1b12c8620ab9e4436e678843b.zip |
include/xalloc: add xgethostname()
The new function allocates memory by xalloc() for hostname and
fill in the buffer by gethostname().
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'include/xalloc.h')
-rw-r--r-- | include/xalloc.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/include/xalloc.h b/include/xalloc.h index 1704259bc..7b685e718 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -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 |