diff options
author | peter <peter@FreeBSD.org> | 1996-01-15 02:18:35 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 1996-01-15 02:18:35 +0000 |
commit | 297db4d6f76e22d93e587a48e8ba9cc319d61a24 (patch) | |
tree | f8d390e27b66f7711ca7e142e0e03b710fab7429 /usr.bin/netstat | |
parent | f9c7af3d08d6ea07bcd3818f4371531323c551da (diff) | |
download | FreeBSD-src-297db4d6f76e22d93e587a48e8ba9cc319d61a24.zip FreeBSD-src-297db4d6f76e22d93e587a48e8ba9cc319d61a24.tar.gz |
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.
Diffstat (limited to 'usr.bin/netstat')
-rw-r--r-- | usr.bin/netstat/inet.c | 14 | ||||
-rw-r--r-- | usr.bin/netstat/main.c | 30 | ||||
-rw-r--r-- | usr.bin/netstat/netstat.h | 4 | ||||
-rw-r--r-- | usr.bin/netstat/route.c | 22 |
4 files changed, 40 insertions, 30 deletions
diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c index 31be4cb..8f1fd95 100644 --- a/usr.bin/netstat/inet.c +++ b/usr.bin/netstat/inet.c @@ -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); } } } diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index d51489a..3ff395a 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -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++; + } + } + } +} + diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index ce3bd38..c37b90b 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -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)); + diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c index 0f9d1a5..b0ca632 100644 --- a/usr.bin/netstat/route.c +++ b/usr.bin/netstat/route.c @@ -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); |