If given host.domain:nn[.nn], trimdomain() now reduces it to

host:nn[.nn] (if the domain is the same as the local one).
This commit is contained in:
Brian Somers 1999-04-09 01:54:10 +00:00
parent d85bfa1067
commit d746fb6643
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=45505
2 changed files with 54 additions and 10 deletions

View File

@ -36,7 +36,7 @@
static char sccsid[] = "@(#)logwtmp.c 8.1 (Berkeley) 6/4/93";
#else
static const char rcsid[] =
"$Id: logwtmp.c,v 1.10 1999/04/07 14:03:31 brian Exp $";
"$Id: logwtmp.c,v 1.11 1999/04/08 08:00:06 brian Exp $";
#endif
#endif /* LIBC_SCCS and not lint */
@ -59,7 +59,9 @@ trimdomain(char *fullhost, int hostsize)
{
static char domain[MAXHOSTNAMELEN];
static int first = 1;
char *s;
static size_t dlen;
char *s, *end;
int spn, ok;
if (first) {
first = 0;
@ -68,17 +70,39 @@ trimdomain(char *fullhost, int hostsize)
memmove(domain, s + 1, strlen(s + 1) + 1);
else
domain[0] = '\0';
dlen = strlen(domain);
}
if (domain[0] != '\0') {
s = fullhost;
while ((fullhost = strchr(fullhost, '.')) != NULL)
if (!strcasecmp(fullhost + 1, domain)) {
if (fullhost - s <= hostsize)
*fullhost = '\0'; /* hit it and acceptable size*/
break;
} else
fullhost++;
end = s + hostsize + 1;
for (; (s = memchr(s, '.', end - s)) != NULL; s++)
if (!strncasecmp(s + 1, domain, dlen)) {
if (s[dlen + 1] == '\0') {
*s = '\0'; /* Found - lose the domain */
break;
} else if (s[dlen + 1] == ':') { /* $DISPLAY ? */
ok = dlen + 2;
spn = strspn(s + ok, "0123456789");
if (spn > 0 && ok + spn - dlen <= end - s) {
ok += spn;
if (s[ok] == '\0') {
/* host.domain:nn */
memmove(s, s + dlen + 1, ok - dlen);
break;
} else if (s[ok] == '.') {
ok++;
spn = strspn(s + ok, "0123456789");
if (spn > 0 && s[ok + spn] == '\0' &&
ok + spn - dlen <= end - s) {
/* host.domain:nn.nn */
memmove(s, s + dlen + 1, ok + spn - dlen);
break;
}
}
}
}
}
}
}

View File

@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: realhostname.3,v 1.1 1999/04/06 23:02:35 brian Exp $
.\" $Id: trimdomain.3,v 1.1 1999/04/07 14:03:31 brian Exp $
.\"
.Dd April 7, 1999
.Os
@ -31,6 +31,7 @@
.Nm trimdomain
.Nd "trim the current domain name from a host name"
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <libutil.h>
.Ft void
.Fn trimdomain "char *fullhost" "int hostsize"
@ -59,6 +60,25 @@ will only trim the domain name if the passed
ends with the current domain name and if the length of the resulting host
name does not exceed
.Ar hostsize .
.Pp
If the passed
.Ar fullname
is actually a
.Dv DISPLAY
specification of the form
.Sm off
.Ar host No . Ar domain No : Ns
.Sm on
.Ar \&nn Oo
.No . Ns Ar \&nn
.Oc
and the domain name is the same as the local domain name,
.Fn trimdomain
will remove the embedded domain name, copying the screen and display
numbers to the end of the base host name and resulting in
.Ar host No : Ns Ar \&nn Oo
.No . Ns Ar \&nn
.Oc .
.Sh RETURN VALUES
.Fn trimdomain
does not return a value.