mirror of
https://github.com/X11Libre/xserver.git
synced 2026-01-26 14:03:17 +00:00
os: xhostname: helper for gethostname()
This little helper does the OS specific part for gethostname() calls. It's putting the result into a fixed-length struct and making sure it's properly filled and the string is always zero-terminated. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
parent
e0748cf74e
commit
df0d09dcb2
@ -18,6 +18,7 @@ srcs_os = [
|
||||
'string.c',
|
||||
'utils.c',
|
||||
'xdmauth.c',
|
||||
'xhostname.c',
|
||||
'xsha1.c',
|
||||
'xstrans.c',
|
||||
'xprintf.c',
|
||||
|
||||
30
os/xhostname.c
Normal file
30
os/xhostname.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* SPDX-License-Identifier: MIT OR X11
|
||||
*
|
||||
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
|
||||
*/
|
||||
#include <dix-config.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if WIN32
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
|
||||
#include "os/xhostname.h"
|
||||
|
||||
int xhostname(struct xhostname* hn)
|
||||
{
|
||||
/* being extra-paranoid here */
|
||||
memset(hn, 0, sizeof(struct xhostname));
|
||||
int ret = gethostname(hn->name, sizeof(hn->name));
|
||||
|
||||
if (ret == -1) {
|
||||
hn->name[0] = 0;
|
||||
return errno;
|
||||
}
|
||||
|
||||
hn->name[sizeof(hn->name)-1] = 0;
|
||||
return ret;
|
||||
}
|
||||
23
os/xhostname.h
Normal file
23
os/xhostname.h
Normal file
@ -0,0 +1,23 @@
|
||||
/* SPDX-License-Identifier: MIT OR X11
|
||||
*
|
||||
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
|
||||
*/
|
||||
#ifndef _XSERVER_OS_XHOSTNAME_H_
|
||||
#define _XSERVER_OS_XHOSTNAME_H_
|
||||
|
||||
#define XHOSTNAME_MAX 2048
|
||||
|
||||
struct xhostname {
|
||||
char name[XHOSTNAME_MAX];
|
||||
};
|
||||
|
||||
/*
|
||||
* retrieve host's nodename. basically a safer way of gethostname() / uname()
|
||||
* making sure that the nodename is always zero-terminated.
|
||||
*
|
||||
* @hn pointer to struct xhostname that will be filled
|
||||
* @return 0 on success
|
||||
*/
|
||||
int xhostname(struct xhostname* hn);
|
||||
|
||||
#endif /* _XSERVER_OS_XHOSTNAME_H_ */
|
||||
Loading…
x
Reference in New Issue
Block a user