From 18914495e90639c12eeec0805bbc74c82700e205 Mon Sep 17 00:00:00 2001 From: lns Date: Thu, 7 Jun 2018 17:59:50 +0200 Subject: POTD skeleton #97. Signed-off-by: lns --- src/utils.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 6bf281f..7bbdc1e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -695,13 +695,13 @@ void escape_ascii_string(const char ascii[], size_t siz, char **dest, size_t *ne (*dest)[ns] = 0; } -size_t parse_hostport(char *str, char *result[2], +size_t parse_hostport(const char *str, const char *result[2], size_t siz[2]) { size_t i; - char *hostend = strchr(str, ':'); - char *portend; - char sep[] = ": \t\n\0"; + const char *hostend = strchr(str, ':'); + const char *portend; + const char sep[] = ": \t\n\0"; result[0] = NULL; result[1] = NULL; @@ -724,5 +724,29 @@ size_t parse_hostport(char *str, char *result[2], siz[0] = hostend - str - 1; siz[1] = portend - hostend; - return siz[0] + siz[1] + 1; + return siz[0] + siz[1] + 1 + (*portend != 0 ? 1 : 0); +} + +size_t parse_hostport_str(const char *str, char hbuf[NI_MAXHOST], + char sbuf[NI_MAXSERV]) +{ + const char *hostport[2]; + size_t hostport_siz[2]; + size_t siz; + + siz = parse_hostport(str, hostport, hostport_siz); + if (!siz) + return 0; + if (snprintf(hbuf, NI_MAXHOST, "%.*s", (int) hostport_siz[0], + hostport[0]) <= 0) + { + return 0; + } + if (snprintf(sbuf, NI_MAXSERV, "%.*s", (int) hostport_siz[1], + hostport[1]) <= 0) + { + return 0; + } + + return siz; } -- cgit v1.2.3