tidy up the domain name trimming code, and move it to a single place

rather than having the same bit of code duplicated in three places,
each with their own static copy of the host's local name.
This commit is contained in:
peter 1996-01-15 02:18:35 +00:00
parent f9c7af3d08
commit 297db4d6f7
4 changed files with 39 additions and 29 deletions

View File

@ -466,17 +466,7 @@ inetname(inp)
static char line[50];
struct hostent *hp;
struct netent *np;
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
if (first && !nflag) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = index(domain, '.')))
(void) strcpy(domain, cp + 1);
else
domain[0] = 0;
}
cp = 0;
if (!nflag && inp->s_addr != INADDR_ANY) {
int net = inet_netof(*inp);
@ -490,10 +480,8 @@ inetname(inp)
if (cp == 0) {
hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
if (hp) {
if ((cp = index(hp->h_name, '.')) &&
!strcmp(cp + 1, domain))
*cp = 0;
cp = hp->h_name;
trimdomain(cp);
}
}
}

View File

@ -542,3 +542,33 @@ usage()
" %s [-M core] [-N system] [-p protocol]\n", prog);
exit(1);
}
void
trimdomain(cp)
char *cp;
{
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
char *s;
if (first) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(s = strchr(domain, '.')))
(void) strcpy(domain, s + 1);
else
domain[0] = 0;
}
if (domain[0]) {
while ((cp = strchr(cp, '.'))) {
if (!strcasecmp(cp + 1, domain)) {
*cp = 0; /* hit it */
break;
} else {
cp++;
}
}
}
}

View File

@ -61,6 +61,7 @@ char *prog; /* program name */
int kread __P((u_long addr, char *buf, int size));
char *plural __P((int));
char *plurales __P((int));
void trimdomain __P((char *));
void protopr __P((u_long, char *));
void tcp_stats __P((u_long, char *));
@ -70,7 +71,7 @@ void icmp_stats __P((u_long, char *));
void igmp_stats __P((u_long, char *));
void protopr __P((u_long, char *));
void mbpr(u_long);
void mbpr __P((u_long));
void hostpr __P((u_long, u_long));
void impstats __P((u_long, u_long));
@ -116,3 +117,4 @@ void tp_stats __P((caddr_t, caddr_t));
void mroutepr __P((u_long, u_long, u_long));
void mrt_stats __P((u_long, u_long));

View File

@ -36,7 +36,7 @@
static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95";
#endif
static const char rcsid[] =
"$Id: route.c,v 1.9 1996/01/14 23:33:13 peter Exp $";
"$Id: route.c,v 1.10 1996/01/14 23:42:19 peter Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -540,26 +540,14 @@ routename(in)
register char *cp;
static char line[MAXHOSTNAMELEN + 1];
struct hostent *hp;
static char domain[MAXHOSTNAMELEN + 1];
static int first = 1;
if (first) {
first = 0;
if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
(cp = index(domain, '.')))
(void) strcpy(domain, cp + 1);
else
domain[0] = 0;
}
cp = 0;
if (!nflag) {
hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
AF_INET);
if (hp) {
if ((cp = index(hp->h_name, '.')) &&
!strcmp(cp + 1, domain))
*cp = 0;
cp = hp->h_name;
trimdomain(cp);
}
}
if (cp)
@ -664,8 +652,10 @@ netname(in, mask)
mask >>= 1, net >>= 1;
if (!(np = getnetbyaddr(i, AF_INET)))
np = getnetbyaddr(net, AF_INET);
if (np)
if (np) {
cp = np->n_name;
trimdomain(cp);
}
}
if (cp)
strncpy(line, cp, sizeof(line) - 1);