trimdomain.c revision 74266
174266Speter/*- 274266Speter * Copyright (c) 1988, 1993 374266Speter * The Regents of the University of California. All rights reserved. 474266Speter * 574266Speter * Redistribution and use in source and binary forms, with or without 674266Speter * modification, are permitted provided that the following conditions 774266Speter * are met: 874266Speter * 1. Redistributions of source code must retain the above copyright 974266Speter * notice, this list of conditions and the following disclaimer. 1074266Speter * 2. Redistributions in binary form must reproduce the above copyright 1174266Speter * notice, this list of conditions and the following disclaimer in the 1274266Speter * documentation and/or other materials provided with the distribution. 1374266Speter * 3. All advertising materials mentioning features or use of this software 1474266Speter * must display the following acknowledgement: 1574266Speter * This product includes software developed by the University of 1674266Speter * California, Berkeley and its contributors. 1774266Speter * 4. Neither the name of the University nor the names of its contributors 1874266Speter * may be used to endorse or promote products derived from this software 1974266Speter * without specific prior written permission. 2074266Speter * 2174266Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2274266Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2374266Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2474266Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2574266Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2674266Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2774266Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2874266Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2974266Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3074266Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3174266Speter * SUCH DAMAGE. 3274266Speter */ 3374266Speter 3474266Speter#if defined(LIBC_SCCS) && !defined(lint) 3574266Speterstatic const char rcsid[] = 3674266Speter "$FreeBSD: head/lib/libutil/trimdomain.c 74266 2001-03-15 00:15:22Z peter $"; 3774266Speter#endif /* LIBC_SCCS and not lint */ 3874266Speter 3974266Speter#include <sys/param.h> 4074266Speter#include <sys/socket.h> 4174266Speter#include <sys/types.h> 4274266Speter#include <sys/file.h> 4374266Speter#include <sys/stat.h> 4474266Speter#include <netinet/in.h> 4574266Speter#include <arpa/inet.h> 4674266Speter 4774266Speter#include <libutil.h> 4874266Speter#include <netdb.h> 4974266Speter#include <string.h> 5074266Speter#include <time.h> 5174266Speter#include <unistd.h> 5274266Speter#include <utmp.h> 5374266Speter#include <stdio.h> 5474266Speter 5574266Spetervoid 5674266Spetertrimdomain(char *fullhost, int hostsize) 5774266Speter{ 5874266Speter static char domain[MAXHOSTNAMELEN]; 5974266Speter static int first = 1; 6074266Speter static size_t dlen; 6174266Speter char *s, *end; 6274266Speter int spn, ok; 6374266Speter 6474266Speter if (first) { 6574266Speter first = 0; 6674266Speter if (gethostname(domain, sizeof(domain) - 1) == 0 && 6774266Speter (s = strchr(domain, '.'))) 6874266Speter memmove(domain, s + 1, strlen(s + 1) + 1); 6974266Speter else 7074266Speter domain[0] = '\0'; 7174266Speter dlen = strlen(domain); 7274266Speter } 7374266Speter 7474266Speter if (domain[0] != '\0') { 7574266Speter s = fullhost; 7674266Speter end = s + hostsize + 1; 7774266Speter for (; (s = memchr(s, '.', end - s)) != NULL; s++) 7874266Speter if (!strncasecmp(s + 1, domain, dlen)) { 7974266Speter if (s[dlen + 1] == '\0') { 8074266Speter *s = '\0'; /* Found - lose the domain */ 8174266Speter break; 8274266Speter } else if (s[dlen + 1] == ':') { /* $DISPLAY ? */ 8374266Speter ok = dlen + 2; 8474266Speter spn = strspn(s + ok, "0123456789"); 8574266Speter if (spn > 0 && ok + spn - dlen <= end - s) { 8674266Speter ok += spn; 8774266Speter if (s[ok] == '\0') { 8874266Speter /* host.domain:nn */ 8974266Speter memmove(s, s + dlen + 1, ok - dlen); 9074266Speter break; 9174266Speter } else if (s[ok] == '.') { 9274266Speter ok++; 9374266Speter spn = strspn(s + ok, "0123456789"); 9474266Speter if (spn > 0 && s[ok + spn] == '\0' && 9574266Speter ok + spn - dlen <= end - s) { 9674266Speter /* host.domain:nn.nn */ 9774266Speter memmove(s, s + dlen + 1, ok + spn - dlen); 9874266Speter break; 9974266Speter } 10074266Speter } 10174266Speter } 10274266Speter } 10374266Speter } 10474266Speter } 10574266Speter} 106