From: Larry D'Anna Date: Tue, 16 Feb 2010 04:10:04 GMT Subject: Re: [PATCH v2 2/4] Refactoring: connect.c: move duplicated code to get_host_and_port Message-ID: <20100216041004.GA7529@cthulhu> In-Reply-To: <1266276411-5796-3-git-send-email-michael.lukashov@gmail.com> * Michael Lukashov (michael.lukashov@gmail.com) [100215 18:33]: > The following functions: > > git_tcp_connect_sock (IPV6 version) > git_tcp_connect_sock (no IPV6 version), > git_proxy_connect > > have common block of code. Move it to a new function 'get_host_and_port' > > Signed-off-by: Michael Lukashov > --- > connect.c | 83 +++++++++++++++++++++--------------------------------------- > 1 files changed, 29 insertions(+), 54 deletions(-) > > diff --git a/connect.c b/connect.c > index 20054e4..cd399f4 100644 > --- a/connect.c > +++ b/connect.c > @@ -152,6 +152,28 @@ static enum protocol get_protocol(const char *name) > #define STR_(s) # s > #define STR(s) STR_(s) > > +static void get_host_and_port(char **host, const char **port) > +{ > + char *colon, *end; > + > + if (*host[0] == '[') { > + end = strchr(*host + 1, ']'); > + if (end) { > + *end = 0; > + end++; > + (*host)++; > + } else > + end = *host; > + } else > + end = *host; > + colon = strchr(end, ':'); > + > + if (colon) { > + *colon = 0; > + *port = colon + 1; > + } > +} > + > #ifndef NO_IPV6 > > static const char *ai_name(const struct addrinfo *ai) > @@ -170,30 +192,14 @@ static const char *ai_name(const struct addrinfo *ai) > static int git_tcp_connect_sock(char *host, int flags) > { > int sockfd = -1, saved_errno = 0; > - char *colon, *end; > const char *port = STR(DEFAULT_GIT_PORT); > struct addrinfo hints, *ai0, *ai; > int gai; > int cnt = 0; > > - if (host[0] == '[') { > - end = strchr(host + 1, ']'); > - if (end) { > - *end = 0; > - end++; > - host++; > - } else > - end = host; > - } else > - end = host; > - colon = strchr(end, ':'); > - > - if (colon) { > - *colon = 0; > - port = colon + 1; > - if (!*port) > - port = ""; > - } > + get_host_and_port(&host, &port); > + if (!*port) > + *port = ""; shouldn't that be 'port = "none";'? --larry