summaryrefslogtreecommitdiffstats
path: root/qregex.c
blob: 59c0f762bc4d51b53e4f238f63c32c1fcea59036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * qregex (v2)
 * $Id: qregex.c,v 2.1 2001/12/28 07:05:21 evan Exp $
 *
 * Author  : Evan Borgstrom (evan at unixpimps dot org)
 * Created : 2001/12/14 23:08:16
 * Modified: $Date: 2001/12/28 07:05:21 $
 * Revision: $Revision: 2.1 $
 *
 * Do POSIX regex matching on addresses for anti-relay / spam control.
 * It logs to the maillog
 * See the qregex-readme file included with this tarball.
 * If you didn't get this file in a tarball please see the following URL:
 *  http://www.unixpimps.org/software/qregex
 *
 * qregex.c is released under a BSD style copyright.
 * See http://www.unixpimps.org/software/qregex/copyright.html
 *
 * Note: this revision follows the coding guidelines set forth by the rest of
 *       the qmail code and that described at the following URL.
 *       http://cr.yp.to/qmail/guarantee.html
 * 
 */

#include <sys/types.h>
#include <regex.h>
#include "qregex.h"

#define REGCOMP(X,Y)    regcomp(&X, Y, REG_EXTENDED|REG_ICASE)
#define REGEXEC(X,Y)    regexec(&X, Y, (size_t)0, (regmatch_t *)0, (int)0)

int matchregex(char *text, char *regex) {
  regex_t qreg;
  int retval = 0;


  /* build the regex */
  if ((retval = REGCOMP(qreg, regex)) != 0) {
    regfree(&qreg);
    return(-retval);
  }

  /* execute the regex */
  if ((retval = REGEXEC(qreg, text)) != 0) {
    /* did we just not match anything? */
    if (retval == REG_NOMATCH) {
      regfree(&qreg);
      return(0);
    }
    regfree(&qreg);
    return(-retval);
  }

  /* signal the match */
  regfree(&qreg);
  return(1);
}