From owner-freebsd-bugs Tue Jul 10 13:50:15 2001 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id 757B137B403 for ; Tue, 10 Jul 2001 13:50:01 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.3/8.11.3) id f6AKo1D33185; Tue, 10 Jul 2001 13:50:01 -0700 (PDT) (envelope-from gnats) Received: from coffee.q9media.com (coffee.q9media.com [216.94.229.19]) by hub.freebsd.org (Postfix) with ESMTP id 5ECBB37B401 for ; Tue, 10 Jul 2001 13:47:57 -0700 (PDT) (envelope-from mike@coffee.q9media.com) Received: (from mike@localhost) by coffee.q9media.com (8.11.2/8.11.2) id f6AL3QB20816; Tue, 10 Jul 2001 17:03:26 -0400 (EDT) (envelope-from mike) Message-Id: <200107102103.f6AL3QB20816@coffee.q9media.com> Date: Tue, 10 Jul 2001 17:03:26 -0400 (EDT) From: Mike Barcroft Reply-To: Mike Barcroft To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/28880: [patch] src/usr.bin/whois bug fix Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org >Number: 28880 >Category: bin >Synopsis: [patch] src/usr.bin/whois bug fix >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Tue Jul 10 13:50:01 PDT 2001 >Closed-Date: >Last-Modified: >Originator: Mike Barcroft >Release: FreeBSD 5.0-CURRENT i386 >Organization: Q9 Media >Environment: $FreeBSD: src/usr.bin/whois/whois.c,v 1.20 2001/06/27 23:06:47 dd Exp $ >Description: o 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.] o Tested on i386 and alpha >How-To-Repeat: N/A >Fix: Index: whois/whois.c =================================================================== RCS file: /home/ncvs/src/usr.bin/whois/whois.c,v retrieving revision 1.20 diff -u -r1.20 whois.c --- whois/whois.c 2001/06/27 23:06:47 1.20 +++ whois/whois.c 2001/07/10 20:38:48 @@ -79,6 +79,7 @@ const char *ip_whois[] = { RNICHOST, PNICHOST, NULL }; +static char *choose_server(const char *); static void usage(void); static void whois(char *, struct addrinfo *, int); @@ -88,7 +89,7 @@ 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 @@ } 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; @@ -202,6 +189,40 @@ freeaddrinfo(res); } exit(0); +} + +/* + * Returns a pointer to newly allocated memory containing the whois server to + * be queried, or a NULL pointer, if the correct server couldn't be determined. + * The caller must remember to free(3) the allocated memory. + */ +static char * +choose_server(const char *domain) +{ + size_t len; + char *buf, *pos, *retval; + + if ((buf = strdup(domain)) == NULL) + err(1, "strdup()"); + len = strlen(buf); + while (len && buf[len - 1] == '.') + buf[--len] = '\0'; + if ((pos = strrchr(buf, '.')) == NULL) { + free(buf); + return (NULL); + } + pos++; + if (isdigit(*pos)) { + asprintf(&retval, "%s", ANICHOST); + if (retval == NULL) + err(1, "asprintf()"); + } else { + asprintf(&retval, "%s%s", pos, QNICHOST_TAIL); + if (retval == NULL) + err(1, "asprintf()"); + } + free(buf); + return (retval); } static void >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message