1763ed733SEd Schouten /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
4763ed733SEd Schouten * Copyright (c) 1992 The Regents of the University of California.
5763ed733SEd Schouten * All rights reserved.
6763ed733SEd Schouten *
7763ed733SEd Schouten * This code is derived from software contributed to Berkeley by
8763ed733SEd Schouten * James da Silva at the University of Maryland at College Park.
9763ed733SEd Schouten *
10763ed733SEd Schouten * Redistribution and use in source and binary forms, with or without
11763ed733SEd Schouten * modification, are permitted provided that the following conditions
12763ed733SEd Schouten * are met:
13763ed733SEd Schouten * 1. Redistributions of source code must retain the above copyright
14763ed733SEd Schouten * notice, this list of conditions and the following disclaimer.
15763ed733SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
16763ed733SEd Schouten * notice, this list of conditions and the following disclaimer in the
17763ed733SEd Schouten * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
19763ed733SEd Schouten * may be used to endorse or promote products derived from this software
20763ed733SEd Schouten * without specific prior written permission.
21763ed733SEd Schouten *
22763ed733SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23763ed733SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24763ed733SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25763ed733SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26763ed733SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27763ed733SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28763ed733SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29763ed733SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30763ed733SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31763ed733SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32763ed733SEd Schouten * SUCH DAMAGE.
33763ed733SEd Schouten */
34763ed733SEd Schouten
35763ed733SEd Schouten
36763ed733SEd Schouten /*
37763ed733SEd Schouten * Compatibility routines that implement the old re_comp/re_exec interface in
38763ed733SEd Schouten * terms of the regcomp/regexec interface. It's possible that some programs
39763ed733SEd Schouten * rely on dark corners of re_comp/re_exec and won't work with this version,
40763ed733SEd Schouten * but most programs should be fine.
41763ed733SEd Schouten */
42763ed733SEd Schouten
43763ed733SEd Schouten #include <regex.h>
44763ed733SEd Schouten #include <stddef.h>
45763ed733SEd Schouten #include <unistd.h>
46763ed733SEd Schouten
47763ed733SEd Schouten static regex_t re_regexp;
48763ed733SEd Schouten static int re_gotexp;
49763ed733SEd Schouten static char re_errstr[100];
50763ed733SEd Schouten
51763ed733SEd Schouten char *
re_comp(const char * s)52763ed733SEd Schouten re_comp(const char *s)
53763ed733SEd Schouten {
54763ed733SEd Schouten int rc;
55763ed733SEd Schouten
56763ed733SEd Schouten if (s == NULL || *s == '\0') {
57763ed733SEd Schouten if (!re_gotexp)
58763ed733SEd Schouten return __DECONST(char *,
59763ed733SEd Schouten "no previous regular expression");
60763ed733SEd Schouten return (NULL);
61763ed733SEd Schouten }
62763ed733SEd Schouten
63763ed733SEd Schouten if (re_gotexp) {
64763ed733SEd Schouten regfree(&re_regexp);
65763ed733SEd Schouten re_gotexp = 0;
66763ed733SEd Schouten }
67763ed733SEd Schouten
68763ed733SEd Schouten rc = regcomp(&re_regexp, s, REG_EXTENDED);
69763ed733SEd Schouten if (rc == 0) {
70763ed733SEd Schouten re_gotexp = 1;
71763ed733SEd Schouten return (NULL);
72763ed733SEd Schouten }
73763ed733SEd Schouten
74763ed733SEd Schouten regerror(rc, &re_regexp, re_errstr, sizeof(re_errstr));
75763ed733SEd Schouten re_errstr[sizeof(re_errstr) - 1] = '\0';
76763ed733SEd Schouten return (re_errstr);
77763ed733SEd Schouten }
78763ed733SEd Schouten
79763ed733SEd Schouten int
re_exec(const char * s)80763ed733SEd Schouten re_exec(const char *s)
81763ed733SEd Schouten {
82763ed733SEd Schouten int rc;
83763ed733SEd Schouten
84763ed733SEd Schouten if (!re_gotexp)
85763ed733SEd Schouten return (-1);
86763ed733SEd Schouten rc = regexec(&re_regexp, s, 0, NULL, 0);
87763ed733SEd Schouten return (rc == 0 ? 1 : 0);
88763ed733SEd Schouten }
89