From c65c520cb6ff9b2673ec8df72c54c28752608143 Mon Sep 17 00:00:00 2001 From: Miles Bader Date: Sun, 18 Feb 1996 04:54:47 +0000 Subject: Initial revision --- libshouldbeinlibc/shared-dom.c | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 libshouldbeinlibc/shared-dom.c (limited to 'libshouldbeinlibc/shared-dom.c') diff --git a/libshouldbeinlibc/shared-dom.c b/libshouldbeinlibc/shared-dom.c new file mode 100644 index 00000000..0f8efdf8 --- /dev/null +++ b/libshouldbeinlibc/shared-dom.c @@ -0,0 +1,57 @@ +/* Deduce the shared portion of two hostnames + + Copyright (C) 1996 Free Software Foundation, Inc. + + Written by Miles Bader + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#include +#include +#include +#include + +/* Returns a pointer into HOST1 that is the part of the domain shared with + HOST2. If the two do not share anything, the return value will point to + the end of HOST1. If either host is NULL, NULL is returned. */ +char * +shared_domain (char *host1, char *host2) +{ + char *shared, *e1, *e2; + + if (!host1 || !host2) + return 0; + + /* Now compare HOST1 and HOST2 from the end. */ + e2 = host2 + strlen (host2); + e1 = host1 + strlen (host1); + shared = e1; + + /* Ignore `absolute' syntax. */ + if (*e1 == '.') + e1--; + if (*e2 == '.') + e2--; + + while (e1 > host1 && e2 > host2 && *e2 == *e1) + { + if (*e1 == '.') + shared = e1; /* A common domain level has been passed. */ + e1--; + e2--; + } + + return shared; +} -- cgit v1.2.3