11578Srgrimes/*-
21578Srgrimes * Copyright (c) 1992 The Regents of the University of California.
31578Srgrimes * All rights reserved.
41578Srgrimes *
51578Srgrimes * This code is derived from software contributed to Berkeley by
61578Srgrimes * James da Silva at the University of Maryland at College Park.
71578Srgrimes *
81578Srgrimes * Redistribution and use in source and binary forms, with or without
91578Srgrimes * modification, are permitted provided that the following conditions
101578Srgrimes * are met:
111578Srgrimes * 1. Redistributions of source code must retain the above copyright
121578Srgrimes *    notice, this list of conditions and the following disclaimer.
131578Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141578Srgrimes *    notice, this list of conditions and the following disclaimer in the
151578Srgrimes *    documentation and/or other materials provided with the distribution.
161578Srgrimes * 4. Neither the name of the University nor the names of its contributors
171578Srgrimes *    may be used to endorse or promote products derived from this software
181578Srgrimes *    without specific prior written permission.
191578Srgrimes *
201578Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211578Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221578Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231578Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241578Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251578Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261578Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271578Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281578Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291578Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301578Srgrimes * SUCH DAMAGE.
311578Srgrimes */
321578Srgrimes
33165906Simp#include <sys/cdefs.h>
34165906Simp__FBSDID("$FreeBSD$");
35165906Simp
361578Srgrimes/*
371578Srgrimes * Compatibility routines that implement the old re_comp/re_exec interface in
381578Srgrimes * terms of the regcomp/regexec interface.  It's possible that some programs
391578Srgrimes * rely on dark corners of re_comp/re_exec and won't work with this version,
401578Srgrimes * but most programs should be fine.
411578Srgrimes */
421578Srgrimes
431578Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
441578Srgrimesstatic char sccsid[] = "@(#)regex.c	5.1 (Berkeley) 3/29/92";
451578Srgrimes#endif /* LIBC_SCCS and not lint */
461578Srgrimes
47205146Sed#include <regex.h>
481578Srgrimes#include <stddef.h>
49205146Sed#include <unistd.h>
501578Srgrimes
51205146Sedstatic regex_t re_regexp;
52205146Sedstatic int re_gotexp;
53205146Sedstatic char re_errstr[100];
541578Srgrimes
551578Srgrimeschar *
56205146Sedre_comp(const char *s)
571578Srgrimes{
58205146Sed	int rc;
59205146Sed
602768Sache	if (s == NULL || *s == '\0') {
61205146Sed		if (!re_gotexp)
62205146Sed			return __DECONST(char *,
63205146Sed			    "no previous regular expression");
641578Srgrimes		return (NULL);
652768Sache	}
66205146Sed
67205146Sed	if (re_gotexp) {
68205146Sed		regfree(&re_regexp);
69205146Sed		re_gotexp = 0;
70205146Sed	}
71205146Sed
72205146Sed	rc = regcomp(&re_regexp, s, REG_EXTENDED);
73205146Sed	if (rc == 0) {
74205146Sed		re_gotexp = 1;
75205146Sed		return (NULL);
76205146Sed	}
77205146Sed
78205146Sed	regerror(rc, &re_regexp, re_errstr, sizeof(re_errstr));
79205146Sed	re_errstr[sizeof(re_errstr) - 1] = '\0';
80205146Sed	return (re_errstr);
811578Srgrimes}
821578Srgrimes
831578Srgrimesint
84205146Sedre_exec(const char *s)
851578Srgrimes{
861578Srgrimes	int rc;
871578Srgrimes
88205146Sed	if (!re_gotexp)
89205146Sed		return (-1);
90205146Sed	rc = regexec(&re_regexp, s, 0, NULL, 0);
91205146Sed	return (rc == 0 ? 1 : 0);
921578Srgrimes}
93