From owner-freebsd-bugs Fri Feb 11 14:40:13 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by builder.freebsd.org (Postfix) with ESMTP id A6C113DEB for ; Fri, 11 Feb 2000 14:40:09 -0800 (PST) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id OAA33717; Fri, 11 Feb 2000 14:40:02 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from ntstn.sasknow.com (h139-142-245-100.ss.fiberone.net [139.142.245.100]) by builder.freebsd.org (Postfix) with ESMTP id B90DE3DAE for ; Fri, 11 Feb 2000 14:33:26 -0800 (PST) Received: (from root@localhost) by ntstn.sasknow.com (8.9.3/8.9.3) id KAA09318; Fri, 11 Feb 2000 10:33:39 -0600 (CST) (envelope-from root) Message-Id: <200002111633.KAA09318@ntstn.sasknow.com> Date: Fri, 11 Feb 2000 10:33:39 -0600 (CST) From: Ryan Thompson Reply-To: root@ntstn.sasknow.com To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.2 Subject: bin/16657: /bin/hostname: New feature to return subcomponents of hostname Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >Number: 16657 >Category: bin >Synopsis: /bin/hostname: New feature to return subcomponents of hostname >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Fri Feb 11 14:40:02 PST 2000 >Closed-Date: >Last-Modified: >Originator: Ryan Thompson >Release: FreeBSD 3.4-STABLE i386 >Organization: SaskNow Technologies, http://www.sasknow.com/ >Environment: >Description: /bin/hostname's default behaviour is rather basic, and requires copious kludging to produce useful output in shell scripts and the like, when only part of the domain name is desired. For example, given the domain ftp.inner.host.com, perhaps a mail address is desired, and an automated way to fetch only the host.com portion is desired. Or, perhaps only the host and inner subdomain is desired, i.e, ftp.inner. See the fix below for examples. >How-To-Repeat: >Fix: Apply the following patches and use the [-l number] option. Note that the current functionality of /bin/hostname is preserved; the output (and, indeed, most of the executed code) only differs if the -l option is used. Example usage of new options, given domain ftp.inner.host.com: # hostname ftp.inner.host.com # hostname -l 2 host.com # hostname -l 3 inner.host.com # hostname -l 1048576 ftp.inner.host.com # hostname -s ftp # hostname -s -l 2 ftp.inner # hostname -s -l 65536 ftp.inner.host.com =================================================================== RCS file: hostname.c,v retrieving revision 1.1 diff -r1.1 hostname.c 63,64c63,69 < int ch, sflag; < char *p, hostname[MAXHOSTNAMELEN]; --- > int ch, > sflag, > level, /* Domain context level */ > i; > > > char *p, hostname[MAXHOSTNAMELEN], *ep; 65a71 > level = -1; /* Indefinite levels */ 67c73 < while ((ch = getopt(argc, argv, "s")) != -1) --- > while ((ch = getopt(argc, argv, "sl:")) != -1) 70a77,90 > if (level == -1) /* Don't clobber anything set by -l */ > level = 1; > > break; > case 'l': > level = strtol(optarg, &ep, 10); > if (level <= 0) > level = -1; > > /* Optimize against unnecessarily complex levels */ > > if (level > MAXHOSTNAMELEN) > level = MAXHOSTNAMELEN; > 88,89c108,137 < if (sflag && (p = strchr(hostname, '.'))) < *p = '\0'; --- > > if (level != -1) > { > if (sflag) > { > p = hostname; > for (i = 0; i < level; i++) > { > if (!(p = strchr(p, '.'))) > p = strchr(hostname, '\0'); > p += 1; > } > if (p > hostname) > p -= 1; > *p = '\0'; > } > else > { > i = 0; > for (p = strchr(hostname, '\0') - 1; > (i < level) && (p > hostname); p--) > { > if (p == strchr(p, '.')) > i++; > } > if (p != hostname) > p += 2; > strcpy(hostname, p); > } > } 99c147 < (void)fprintf(stderr, "usage: hostname [-s] [name-of-host]\n"); --- > (void)fprintf(stderr, "usage: hostname [-s] [-l #] [name-of-host]\n"); =================================================================== RCS file: hostname.1,v retrieving revision 1.1 diff -r1.1 hostname.1 43a44 > .Op Fl l Ar number 62a64,71 > .It Fl l Ar number > Display only the first > .Ar number > levels of the domain name. (Or the last > .Ar number > levels of the domain name, if used with the > .Fl s > option). >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message