diff options
author | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-09-04 15:32:11 +0200 |
---|---|---|
committer | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-09-04 16:07:52 +0200 |
commit | db84b7988bfbc9caf4fa584d775b4b43154261db (patch) | |
tree | 42d62103e0b10314b77626c60ee8ee26bdd12858 /src/netops.c | |
parent | 3d975abcb8b1dfe538b20d64598f85c94c34df61 (diff) | |
download | libgit2-db84b7988bfbc9caf4fa584d775b4b43154261db.tar.gz |
Move extract_host_and_port to netops and add default port argument
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
Diffstat (limited to 'src/netops.c')
-rw-r--r-- | src/netops.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/netops.c b/src/netops.c index b5251925e..f44209aef 100644 --- a/src/netops.c +++ b/src/netops.c @@ -161,3 +161,33 @@ int gitno_select_in(gitno_buffer *buf, long int sec, long int usec) /* The select(2) interface is silly */ return select(buf->fd + 1, &fds, NULL, NULL, &tv); } + +int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port) +{ + char *colon, *slash, *delim; + int error = GIT_SUCCESS; + + colon = strchr(url, ':'); + slash = strchr(url, '/'); + + if (slash == NULL) + return git__throw(GIT_EOBJCORRUPTED, "Malformed URL: missing /"); + + if (colon == NULL) { + *port = git__strdup(default_port); + } else { + *port = git__strndup(colon + 1, slash - colon - 1); + } + if (*port == NULL) + return GIT_ENOMEM;; + + + delim = colon == NULL ? slash : colon; + *host = git__strndup(url, delim - url); + if (*host == NULL) { + free(*port); + error = GIT_ENOMEM; + } + + return error; +} |