lib: remove sparc-only libdscp
[unleashed.git] / usr / src / cmd / auditreduce / regex2.c
blob1c3b54c4def5aa3288b7bbd5069a53da2c7d28eb
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 free(p_exp[i].comp);
63 free(p_exp);
65 if (fexp != NULL) {
66 free(fexp);
68 fexp = strdup(s);
69 for (p = fexp, nexp = 1; *p != '\0'; p++) {
70 if (*p == SEP) {
71 nexp++;
74 p_exp = (struct exp *)malloc(nexp * sizeof (struct exp));
75 for (i = 0, p = fexp; *p != '\0'; i++) {
76 p_exp[i].comp = NULL;
77 if (*p == NOT) {
78 p++;
79 p_exp[i].not = 1;
80 } else {
81 p_exp[i].not = 0;
83 p_exp[i].s = p;
84 while (*p != SEP && *p != '\0')
85 p++;
86 if (*p == SEP) {
87 *p = '\0';
88 p++;
90 if (regcmp(p_exp[i].s, NULL) == NULL)
91 return (er);
93 return (NULL);
96 int
97 re_exec2(s)
98 char *s;
100 int i;
101 char *ret;
103 if (compile) {
104 for (i = 0; i < nexp; i++) {
105 if ((p_exp[i].comp = regcmp(p_exp[i].s, NULL)) == NULL)
106 return (-1);
108 compile = 0;
110 for (i = 0; i < nexp; i++) {
111 ret = regex(p_exp[i].comp, s);
112 if (ret != NULL) {
113 return (!p_exp[i].not);
117 /* no match and no more to check */
118 return (0);