X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/7b98850d3b8b2453b473fdfb6db52f4aa3fe39b6..90bb6fabed03373c6b11aa9486df2eb23650660c:/sbr/client.c diff --git a/sbr/client.c b/sbr/client.c index ae6968f7..99788963 100644 --- a/sbr/client.c +++ b/sbr/client.c @@ -2,8 +2,6 @@ /* * client.c -- connect to a server * - * $Id$ - * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. @@ -12,17 +10,10 @@ #include #include #include -#include #include #include #include - -#ifdef HAVE_ARPA_INET_H -# include -#endif - -#define TRUE 1 -#define FALSE 0 +#include #define MAXARGS 1000 @@ -34,7 +25,6 @@ static char **client_brkstring (char *, char *, char *); static int client_brkany (char, char *); static char **client_copyip (char **, char **, int); -static char *client_getcpy (char *); static void client_freelist(char **); @@ -47,20 +37,22 @@ client (char *args, char *service, char *response, int len_response, int debug) ap = arguments; if (args != NULL && *args != 0) { - ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"), + ap = client_copyip (client_brkstring (mh_xstrdup(args), " ", "\n"), ap, MAXARGS); } else { if (servers != NULL && *servers != 0) - ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"), + ap = client_copyip (client_brkstring (mh_xstrdup(servers), " ", "\n"), ap, MAXARGS); } if (ap == arguments) { - *ap++ = client_getcpy ("localhost"); + *ap++ = mh_xstrdup("localhost"); *ap = NULL; } memset(&hints, 0, sizeof(hints)); +#ifdef AI_ADDRCONFIG hints.ai_flags = AI_ADDRCONFIG; +#endif hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -83,12 +75,15 @@ client (char *args, char *service, char *response, int len_response, int debug) for (ai = res; ai != NULL; ai = ai->ai_next) { if (debug) { char address[NI_MAXHOST]; + char port[NI_MAXSERV]; rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, address, - sizeof(address), NULL, NULL, NI_NUMERICHOST); + sizeof(address), port, sizeof port, + NI_NUMERICHOST | NI_NUMERICSERV); - fprintf(stderr, "Connecting to %s...\n", - rc ? "unknown" : address); + fprintf(stderr, "Connecting to %s:%s...\n", + rc ? "unknown" : address, + rc ? "--" : port); } sd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); @@ -101,7 +96,7 @@ client (char *args, char *service, char *response, int len_response, int debug) if (connect(sd, ai->ai_addr, ai->ai_addrlen) == 0) { freeaddrinfo(res); - client_freelist(ap); + client_freelist(arguments); return sd; } @@ -111,10 +106,11 @@ client (char *args, char *service, char *response, int len_response, int debug) close(sd); } + + freeaddrinfo(res); } - freeaddrinfo(res); - client_freelist(ap); + client_freelist(arguments); strncpy (response, "no servers available", len_response); return NOTOK; } @@ -127,8 +123,7 @@ client (char *args, char *service, char *response, int len_response, int debug) static void client_freelist(char **list) { - while (*list++ != NULL) - free(*list); + free(*list); } @@ -141,8 +136,8 @@ static char *broken[MAXARGS + 1]; static char ** client_brkstring (char *strg, char *brksep, char *brkterm) { - register int bi; - register char c, *sp; + int bi; + char c, *sp; sp = strg; @@ -171,8 +166,8 @@ client_brkstring (char *strg, char *brksep, char *brkterm) static int client_brkany (char chr, char *strg) { - register char *sp; - + char *sp; + if (strg) for (sp = strg; *sp; sp++) if (chr == *sp) @@ -194,18 +189,3 @@ client_copyip (char **p, char **q, int len_q) return q; } - - -static char * -client_getcpy (char *str) -{ - char *cp; - size_t len; - - len = strlen(str) + 1; - cp = mh_xmalloc(len); - - memcpy (cp, str, len); - return cp; -} -