summaryrefslogtreecommitdiffstats
path: root/usr.bin/whois
diff options
context:
space:
mode:
authormike <mike@FreeBSD.org>2001-07-20 16:44:44 +0000
committermike <mike@FreeBSD.org>2001-07-20 16:44:44 +0000
commit567548e3b87b763ec7c69bcfd2a2707833ea9f15 (patch)
tree1310f63d1b1922e343f918cdc52f9e96993d8126 /usr.bin/whois
parent519f5fca7a6d7b6f0a2039103007fccf998ae8f3 (diff)
downloadFreeBSD-src-567548e3b87b763ec7c69bcfd2a2707833ea9f15.zip
FreeBSD-src-567548e3b87b763ec7c69bcfd2a2707833ea9f15.tar.gz
o Add phantom's gethostinfo() function to reduce duplicated code.
o Increase readability by creating a wrapper for asprintf(3). o Cast isdigit(3) argument to unsigned char for future locale changes. Submitted by: phantom Reviewed by: -audit, des Approved by: des
Diffstat (limited to 'usr.bin/whois')
-rw-r--r--usr.bin/whois/whois.c105
1 files changed, 56 insertions, 49 deletions
diff --git a/usr.bin/whois/whois.c b/usr.bin/whois/whois.c
index e790336..6fd7e1c 100644
--- a/usr.bin/whois/whois.c
+++ b/usr.bin/whois/whois.c
@@ -52,6 +52,7 @@ static const char rcsid[] =
#include <ctype.h>
#include <err.h>
#include <netdb.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -80,16 +81,18 @@ static const char rcsid[] =
const char *ip_whois[] = { RNICHOST, PNICHOST, NULL };
static char *choose_server(char *);
+static struct addrinfo *gethostinfo(char const *host, int exit_on_error);
+static void s_asprintf(char **ret, const char *format, ...);
static void usage(void);
static void whois(char *, struct addrinfo *, int);
int
main(int argc, char *argv[])
{
- struct addrinfo hints, *res;
+ struct addrinfo *res;
const char *host;
char *qnichost;
- int ch, error, flags, use_qnichost;
+ int ch, flags, use_qnichost;
#ifdef SOCKS
SOCKSinit(argv[0]);
@@ -159,29 +162,11 @@ main(int argc, char *argv[])
flags |= WHOIS_INIC_FALLBACK | WHOIS_RECURSE;
}
while (argc--) {
- if (use_qnichost) {
- if ((qnichost = choose_server(*argv)) != NULL) {
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = 0;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- error = getaddrinfo(qnichost, "whois",
- &hints, &res);
- if (error != 0)
- errx(EX_NOHOST, "%s: %s", qnichost,
- gai_strerror(error));
- }
- }
- if (qnichost == NULL) {
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = 0;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- error = getaddrinfo(host, "whois", &hints, &res);
- if (error != 0)
- errx(EX_NOHOST, "%s: %s", host,
- gai_strerror(error));
- }
+ if (use_qnichost)
+ if ((qnichost = choose_server(*argv)) != NULL)
+ res = gethostinfo(qnichost, 1);
+ if (qnichost == NULL)
+ res = gethostinfo(host, 1);
free(qnichost);
qnichost = NULL;
@@ -208,23 +193,56 @@ choose_server(char *domain)
errx(EX_USAGE, "can't search for a null string");
while (pos > domain && *pos != '.')
--pos;
- if (isdigit(*++pos)) {
- if (asprintf(&retval, "%s", ANICHOST) == -1)
- err(1, "asprintf()");
- } else {
- if (asprintf(&retval, "%s%s", pos, QNICHOST_TAIL) == -1)
- err(1, "asprintf()");
- }
+ if (isdigit((unsigned char)*++pos))
+ s_asprintf(&retval, "%s", ANICHOST);
+ else
+ s_asprintf(&retval, "%s%s", pos, QNICHOST_TAIL);
return (retval);
}
+static struct addrinfo *
+gethostinfo(char const *host, int exit_on_error)
+{
+ struct addrinfo hints, *res;
+ int error;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = 0;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ error = getaddrinfo(host, "whois", &hints, &res);
+ if (error) {
+ warnx("%s: %s", host, gai_strerror(error));
+ if (exit_on_error)
+ exit(EX_NOHOST);
+ return (NULL);
+ }
+ return (res);
+}
+
+/*
+ * Wrapper for asprintf(3) that exits on error.
+ */
+static void
+s_asprintf(char **ret, const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ if (vasprintf(ret, format, ap) == -1) {
+ va_end(ap);
+ err(EX_OSERR, "vasprintf()");
+ }
+ va_end(ap);
+}
+
static void
whois(char *name, struct addrinfo *res, int flags)
{
FILE *sfi, *sfo;
- struct addrinfo hints, *res2;
+ struct addrinfo *res2;
char *buf, *nhost, *p;
- int i, nomatch, error, s;
+ int i, nomatch, s;
size_t len;
for (; res; res = res->ai_next) {
@@ -255,17 +273,13 @@ whois(char *name, struct addrinfo *res, int flags)
if (p != NULL) {
p += sizeof(WHOIS_SERVER_ID) - 1;
if ((len = strcspn(p, " \t\n\r")) != 0) {
- asprintf(&nhost, "%s", p);
- if (nhost == NULL)
- err(1, "asprintf()");
+ s_asprintf(&nhost, "%s", p);
}
} else {
for (i = 0; ip_whois[i] != NULL; i++) {
if (strstr(buf, ip_whois[i]) == NULL)
continue;
- nhost = strdup(ip_whois[i]);
- if (nhost == NULL)
- err(1, "strdup()");
+ s_asprintf(&nhost, "%s", ip_whois[i]);
}
}
}
@@ -285,18 +299,11 @@ whois(char *name, struct addrinfo *res, int flags)
/* Do second lookup as needed. */
if (nomatch && nhost == NULL) {
printf("Looking up %s at %s.\n\n", name, INICHOST);
- if ((nhost = strdup(INICHOST)) == NULL)
- err(1, "strdup()");
+ s_asprintf(&nhost, "%s", INICHOST);
}
if (nhost != NULL) {
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = 0;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- error = getaddrinfo(nhost, "whois", &hints, &res2);
- if (error != 0) {
- warnx("%s: %s", nhost, gai_strerror(error));
+ if ((res2 = gethostinfo(nhost, 0)) == NULL) {
free(nhost);
return;
}
OpenPOWER on IntegriCloud