8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / auditreduce / regex2.c
blobd7a2aabe00723e5b55c79698e0db64f12835fcd2
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Extend regular expression matching for the file objects to allow
28 * multiple regular expressions (instead of just 1), and to not select
29 * regular expressions starting with a "~". This will allow adminstrator
30 * to exclude uninteresting files from the audit trail.
33 #include <stdlib.h>
34 #include <string.h>
35 #include <libgen.h>
37 struct exp {
38 char *s; /* The regular is expression */
39 int not; /* Exclude if matched? */
40 char *comp; /* The compiled regular expression */
43 static char SEP = ','; /* separator used between reg exprs */
44 static char NOT = '~'; /* Character used to exclude rex exprs */
45 static int compile = 1; /* Must we compile the expressions */
47 static char *fexp = NULL; /* full list of regular expressions */
48 static int nexp = 1; /* number of regular expressions in fexp */
49 static struct exp *p_exp = NULL; /* list of individual expressions */
51 char *
52 re_comp2(s)
53 char *s;
55 char *p;
56 int i;
57 static char *er = "regcmp: error";
59 compile = 1;
60 if (p_exp != NULL) {
61 for (i = 0; i < nexp; i++)
62 if (p_exp[i].comp != NULL)
63 free(p_exp[i].comp);
64 free(p_exp);
66 if (fexp != NULL) {
67 free(fexp);
69 fexp = strdup(s);
70 for (p = fexp, nexp = 1; *p != '\0'; p++) {
71 if (*p == SEP) {
72 nexp++;
75 p_exp = (struct exp *)malloc(nexp * sizeof (struct exp));
76 for (i = 0, p = fexp; *p != '\0'; i++) {
77 p_exp[i].comp = NULL;
78 if (*p == NOT) {
79 p++;
80 p_exp[i].not = 1;
81 } else {
82 p_exp[i].not = 0;
84 p_exp[i].s = p;
85 while (*p != SEP && *p != '\0')
86 p++;
87 if (*p == SEP) {
88 *p = '\0';
89 p++;
91 if (regcmp(p_exp[i].s, NULL) == NULL)
92 return (er);
94 return (NULL);
97 int
98 re_exec2(s)
99 char *s;
101 int i;
102 char *ret;
104 if (compile) {
105 for (i = 0; i < nexp; i++) {
106 if ((p_exp[i].comp = regcmp(p_exp[i].s, NULL)) == NULL)
107 return (-1);
109 compile = 0;
111 for (i = 0; i < nexp; i++) {
112 ret = regex(p_exp[i].comp, s);
113 if (ret != NULL) {
114 return (!p_exp[i].not);
118 /* no match and no more to check */
119 return (0);