aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2018-06-07 17:59:50 +0200
committerlns <matzeton@googlemail.com>2018-06-07 17:59:50 +0200
commit18914495e90639c12eeec0805bbc74c82700e205 (patch)
treee60315e8ee57c86951e6e2ebfa687d42d675a3ce /src/utils.c
parent019e585ada9b6c1eeeaeee6f29ce312e7e1f993f (diff)
POTD skeleton #97.
Signed-off-by: lns <matzeton@googlemail.com>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c34
1 files changed, 29 insertions, 5 deletions
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;
}