/*
* dnsutl - utilities to make DNS easier to configure
* Copyright (C) 1999, 2006, 2007 Peter Miller
*
* 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 3 of the License, 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, see
* .
*/
#include
#include
#include
#include
#include
int
regular_expression_match(const char *formal, const char *actual)
{
#if defined(HAVE_REGCOMP) && defined(HAVE_REGEXEC)
/* use POSIX regular expressions */
regex_t reg;
int n;
n = regcomp(®, formal, REG_NOSUB | REG_EXTENDED);
if (n)
{
char errbuf[100];
bomb:
regerror(n, ®, errbuf, sizeof(errbuf));
fatal("pattern \"%s\" error: %s", formal, errbuf);
}
n = regexec(®, actual, 0, (regmatch_t *) 0, 0);
switch (n)
{
case 0:
regfree(®);
return 1;
case REG_NOMATCH:
regfree(®);
return 0;
default:
goto bomb;
}
#else /* !posix */
#if defined(HAVE_REGCMP) && defined(HAVE_REGEX)
/* use ancient BSD regular expressions */
int n;
char *magic;
magic = regcmp(formal, (char *)0);
if (!magic)
{
fatal("pattern \"%s\" error", formal);
}
n = regex(magic, actual);
free(magic);
return (n != 0);
#else /* !bsd */
#if defined(HAVE_RE_COMP) && defined(HAVE_RE_EXEC)
/* use BSD 4.2 regular expressions */
int n;
char *s;
s = re_comp(formal);
if (s)
{
fatal("pattern \"%s\" error: %s", formal, s);
}
n = re_exec(actual);
if (n < 0)
fatal("pattern \"%s\" error", formal);
return n;
#else /* !sun */
#error "Can't find any Regular Expression functions!"
fatal("no regular expressions available");
#endif /* !sun */
#endif /* !bsd */
#endif /* !posix */
}