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>
This commit is contained in:
parent
e0b6ab5687
commit
1b2aa6293d
1 changed files with 15 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue