source: regex.c @ 0982901

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 0982901 was 554a2b8, checked in by Anders Kaseorg <andersk@mit.edu>, 16 years ago
Fix format string injection bugs.
  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4static const char fileIdent[] = "$Id$";
5
6void owl_regex_init(owl_regex *re)
7{
8  re->negate=0;
9  re->string=NULL;
10}
11
12int owl_regex_create(owl_regex *re, char *string)
13{
14  int ret;
15  char buff1[LINE];
16  char *ptr;
17 
18  re->string=owl_strdup(string);
19
20  ptr=string;
21  re->negate=0;
22  if (string[0]=='!') {
23    ptr++;
24    re->negate=1;
25  }
26
27  /* set the regex */
28  ret=regcomp(&(re->re), ptr, REG_EXTENDED|REG_ICASE);
29  if (ret) {
30    regerror(ret, NULL, buff1, LINE);
31    owl_function_makemsg("Error in regular expression: %s", buff1);
32    owl_free(re->string);
33    re->string=NULL;
34    return(-1);
35  }
36
37  return(0);
38}
39
40int owl_regex_create_quoted(owl_regex *re, char *string)
41{
42  char *quoted;
43 
44  quoted=owl_text_quote(string, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH);
45  owl_regex_create(re, quoted);
46  owl_free(quoted);
47  return(0);
48}
49
50int owl_regex_compare(owl_regex *re, char *string)
51{
52  int out, ret;
53
54  /* if the regex is not set we match */
55  if (!owl_regex_is_set(re)) {
56    return(0);
57  }
58 
59  ret=regexec(&(re->re), string, 0, NULL, 0);
60  out=ret;
61  if (re->negate) {
62    out=!out;
63  }
64  return(out);
65}
66
67int owl_regex_is_set(owl_regex *re)
68{
69  if (re->string) return(1);
70  return(0);
71}
72
73char *owl_regex_get_string(owl_regex *re)
74{
75  return(re->string);
76}
77
78void owl_regex_copy(owl_regex *a, owl_regex *b)
79{
80  b->negate=a->negate;
81  b->string=owl_strdup(a->string);
82  memcpy(&(b->re), &(a->re), sizeof(regex_t));
83}
84
85void owl_regex_free(owl_regex *re)
86{
87    if (re->string) {
88        owl_free(re->string);
89        regfree(&(re->re));
90    }
91}
Note: See TracBrowser for help on using the repository browser.