00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <regex.h>
00029 #include <string.h>
00030 #include <errno.h>
00031
00032 #include "../include/cupl.h"
00033 #include "../include/string.h"
00034
00035 static int regex_match_pattern(regex_t *regexp, char *buffer);
00036
00037
00038 static int regex_match_pattern(regex_t *regexp, char *buffer)
00039 {
00040 int start = 0;
00041 size_t no_sub = regexp->re_nsub + 1;
00042 regmatch_t *result;
00043
00044 if ((result = (regmatch_t *) malloc(sizeof(regmatch_t) * no_sub)) == 0)
00045 {
00046 fprintf(stderr, "%s: %d: malloc(): %s.\n", __FILE__, __LINE__, strerror(errno));
00047 return -1;
00048 }
00049
00050 while (regexec(regexp, buffer+start, no_sub, result, 0) == 0)
00051 {
00052 free(result);
00053 return 1;
00054 }
00055
00056 free(result);
00057
00058 return 0;
00059 }
00060
00061
00062
00063 extern int cupl_match_regex(char *buffer, char *pattern)
00064 {
00065 int ret = 0;
00066 regex_t *regexp;
00067
00068
00069 if ((regexp = (regex_t *) malloc(sizeof(regex_t))) == NULL) {
00070 fprintf(stderr, "%s: %d: malloc(): %s.\n", __FILE__, __LINE__, strerror(errno));
00071 return -1;
00072 }
00073
00074 memset(regexp, 0, sizeof(regex_t));
00075
00076 if ((ret = regcomp(regexp, pattern, 0)) != 0)
00077 {
00078 regfree(regexp);
00079 free(regexp);
00080 return -1;
00081 }
00082
00083 ret = regex_match_pattern(regexp, buffer);
00084
00085 regfree(regexp);
00086 free(regexp);
00087
00088 return ret;
00089 }
00090
00091
00092