xref: /freebsd/lib/libcompat/4.3/re_comp.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
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 #include <sys/cdefs.h>
36763ed733SEd Schouten __FBSDID("$FreeBSD$");
37763ed733SEd Schouten 
38763ed733SEd Schouten /*
39763ed733SEd Schouten  * Compatibility routines that implement the old re_comp/re_exec interface in
40763ed733SEd Schouten  * terms of the regcomp/regexec interface.  It's possible that some programs
41763ed733SEd Schouten  * rely on dark corners of re_comp/re_exec and won't work with this version,
42763ed733SEd Schouten  * but most programs should be fine.
43763ed733SEd Schouten  */
44763ed733SEd Schouten 
45763ed733SEd Schouten #if defined(LIBC_SCCS) && !defined(lint)
46763ed733SEd Schouten static char sccsid[] = "@(#)regex.c	5.1 (Berkeley) 3/29/92";
47763ed733SEd Schouten #endif /* LIBC_SCCS and not lint */
48763ed733SEd Schouten 
49763ed733SEd Schouten #include <regex.h>
50763ed733SEd Schouten #include <stddef.h>
51763ed733SEd Schouten #include <unistd.h>
52763ed733SEd Schouten 
53763ed733SEd Schouten static regex_t re_regexp;
54763ed733SEd Schouten static int re_gotexp;
55763ed733SEd Schouten static char re_errstr[100];
56763ed733SEd Schouten 
57763ed733SEd Schouten char *
58763ed733SEd Schouten re_comp(const char *s)
59763ed733SEd Schouten {
60763ed733SEd Schouten 	int rc;
61763ed733SEd Schouten 
62763ed733SEd Schouten 	if (s == NULL || *s == '\0') {
63763ed733SEd Schouten 		if (!re_gotexp)
64763ed733SEd Schouten 			return __DECONST(char *,
65763ed733SEd Schouten 			    "no previous regular expression");
66763ed733SEd Schouten 		return (NULL);
67763ed733SEd Schouten 	}
68763ed733SEd Schouten 
69763ed733SEd Schouten 	if (re_gotexp) {
70763ed733SEd Schouten 		regfree(&re_regexp);
71763ed733SEd Schouten 		re_gotexp = 0;
72763ed733SEd Schouten 	}
73763ed733SEd Schouten 
74763ed733SEd Schouten 	rc = regcomp(&re_regexp, s, REG_EXTENDED);
75763ed733SEd Schouten 	if (rc == 0) {
76763ed733SEd Schouten 		re_gotexp = 1;
77763ed733SEd Schouten 		return (NULL);
78763ed733SEd Schouten 	}
79763ed733SEd Schouten 
80763ed733SEd Schouten 	regerror(rc, &re_regexp, re_errstr, sizeof(re_errstr));
81763ed733SEd Schouten 	re_errstr[sizeof(re_errstr) - 1] = '\0';
82763ed733SEd Schouten 	return (re_errstr);
83763ed733SEd Schouten }
84763ed733SEd Schouten 
85763ed733SEd Schouten int
86763ed733SEd Schouten re_exec(const char *s)
87763ed733SEd Schouten {
88763ed733SEd Schouten 	int rc;
89763ed733SEd Schouten 
90763ed733SEd Schouten 	if (!re_gotexp)
91763ed733SEd Schouten 		return (-1);
92763ed733SEd Schouten 	rc = regexec(&re_regexp, s, 0, NULL, 0);
93763ed733SEd Schouten 	return (rc == 0 ? 1 : 0);
94763ed733SEd Schouten }
95