diff options
author | mike <mike@FreeBSD.org> | 2001-07-17 20:40:41 +0000 |
---|---|---|
committer | mike <mike@FreeBSD.org> | 2001-07-17 20:40:41 +0000 |
commit | c13f900ffc96d90cd33b196b84381538bbe2ba9c (patch) | |
tree | ae3de0f542188150684552d21e45afad6a42c81a /usr.bin/whois | |
parent | 35acbfe0b39fd6099d0a015c291e0d16d3588532 (diff) | |
download | FreeBSD-src-c13f900ffc96d90cd33b196b84381538bbe2ba9c.zip FreeBSD-src-c13f900ffc96d90cd33b196b84381538bbe2ba9c.tar.gz |
Re-write the logic that finds the whois server to query.
[This fixes a bug where one would type 'whois foo.bar.'
and get an error because of the trailing period.]
PR: 28880
Approved by: des
Diffstat (limited to 'usr.bin/whois')
-rw-r--r-- | usr.bin/whois/whois.c | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/usr.bin/whois/whois.c b/usr.bin/whois/whois.c index ac2619b..e790336 100644 --- a/usr.bin/whois/whois.c +++ b/usr.bin/whois/whois.c @@ -79,6 +79,7 @@ static const char rcsid[] = const char *ip_whois[] = { RNICHOST, PNICHOST, NULL }; +static char *choose_server(char *); static void usage(void); static void whois(char *, struct addrinfo *, int); @@ -88,7 +89,7 @@ main(int argc, char *argv[]) struct addrinfo hints, *res; const char *host; char *qnichost; - int ch, error, flags, i, j, use_qnichost; + int ch, error, flags, use_qnichost; #ifdef SOCKS SOCKSinit(argv[0]); @@ -159,21 +160,7 @@ main(int argc, char *argv[]) } while (argc--) { if (use_qnichost) { - for (i = j = 0; (*argv)[i]; i++) - if ((*argv)[i] == '.') - j = i; - if (j != 0) { - if (isdigit(*(*argv + j + 1))) { - asprintf(&qnichost, "%s", ANICHOST); - if (qnichost == NULL) - err(1, "asprintf()"); - } else { - asprintf(&qnichost, "%s%s", - *argv + j + 1, QNICHOST_TAIL); - if (qnichost == NULL) - err(1, "asprintf()"); - } - + if ((qnichost = choose_server(*argv)) != NULL) { memset(&hints, 0, sizeof(hints)); hints.ai_flags = 0; hints.ai_family = AF_UNSPEC; @@ -204,6 +191,33 @@ main(int argc, char *argv[]) exit(0); } +/* + * This function will remove any trailing periods from domain, after which it + * returns a pointer to newly allocated memory containing the whois server to + * be queried, or a NULL if the correct server couldn't be determined. The + * caller must remember to free(3) the allocated memory. + */ +static char * +choose_server(char *domain) +{ + char *pos, *retval; + + for (pos = strchr(domain, '\0'); pos > domain && *--pos == '.';) + *pos = '\0'; + if (*domain == '\0') + 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()"); + } + return (retval); +} + static void whois(char *name, struct addrinfo *res, int flags) { |