From: Karel Zak Date: Fri, 19 Oct 2012 14:23:54 +0000 (+0200) Subject: include/xalloc: add xgethostname() X-Git-Tag: v2.23-rc1~600 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b2aa6293d10d9d1b12c8620ab9e4436e678843b;p=thirdparty%2Futil-linux.git 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 --- diff --git a/include/xalloc.h b/include/xalloc.h index 1704259bca..7b685e7184 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