mirror of
https://github.com/systemd/systemd.git
synced 2026-08-04 15:10:33 +00:00
hostname-util: add new helper split_user_at_host()
Let's introduce a common helper for splitting user@host specifications like we use them for --machine=.
This commit is contained in:
@@ -172,3 +172,36 @@ int get_pretty_hostname(char **ret) {
|
||||
*ret = TAKE_PTR(n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int split_user_at_host(const char *s, char **ret_user, char **ret_host) {
|
||||
_cleanup_free_ char *u = NULL, *h = NULL;
|
||||
|
||||
/* Splits a user@host expression (one of those we accept on --machine= and similar). Returns NULL in
|
||||
* each of the two return parameters if that part was left empty. */
|
||||
|
||||
const char *rhs = strchr(s, '@');
|
||||
if (rhs) {
|
||||
if (ret_user && rhs > s) {
|
||||
u = strndup(s, rhs - s);
|
||||
if (!u)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if (ret_host && rhs[1] != 0) {
|
||||
h = strdup(rhs + 1);
|
||||
if (!h)
|
||||
return -ENOMEM;
|
||||
}
|
||||
} else if (!isempty(s) && ret_host) {
|
||||
h = strdup(s);
|
||||
if (!h)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if (ret_user)
|
||||
*ret_user = TAKE_PTR(u);
|
||||
if (ret_host)
|
||||
*ret_host = TAKE_PTR(h);
|
||||
|
||||
return !!rhs; /* return > 0 if '@' was specified, 0 otherwise */
|
||||
}
|
||||
|
||||
@@ -38,3 +38,5 @@ static inline bool is_dns_proxy_stub_hostname(const char *hostname) {
|
||||
}
|
||||
|
||||
int get_pretty_hostname(char **ret);
|
||||
|
||||
int split_user_at_host(const char *s, char **ret_user, char **ret_host);
|
||||
|
||||
Reference in New Issue
Block a user