hostname: introduce gethostname_full() and use it in various gethostname() variants

This commit is contained in:
Yu Watanabe
2021-08-12 13:48:15 +09:00
parent ccdf235464
commit 0995accdfd
5 changed files with 64 additions and 78 deletions
+30 -3
View File
@@ -7,10 +7,37 @@
#include "macro.h"
#include "strv.h"
typedef enum GetHostnameFlags {
GET_HOSTNAME_ALLOW_NONE = 1 << 0, /* accepts "(none)". */
GET_HOSTNAME_ALLOW_LOCALHOST = 1 << 1, /* accepts "localhost" or friends. */
GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 2, /* use default hostname if no hostname is set. */
GET_HOSTNAME_SHORT = 1 << 3, /* kills the FQDN part if present. */
} GetHostnameFlags;
int gethostname_full(GetHostnameFlags flags, char **ret);
static inline int gethostname_strict(char **ret) {
return gethostname_full(0, ret);
}
static inline char* gethostname_malloc(void) {
char *s;
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &s) < 0)
return NULL;
return s;
}
static inline char* gethostname_short_malloc(void) {
char *s;
if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT | GET_HOSTNAME_SHORT, &s) < 0)
return NULL;
return s;
}
char* get_default_hostname(void);
char* gethostname_malloc(void);
char* gethostname_short_malloc(void);
int gethostname_strict(char **ret);
bool valid_ldh_char(char c) _const_;