xref: /dflybsd-src/lib/libcompat/4.3/re_comp.c (revision c66c7e2fb8d0d28477d550f1d2a50c4677d547ff)
10d5acd74SJohn Marino /*-
20d5acd74SJohn Marino  * Copyright (c) 1992 The Regents of the University of California.
30d5acd74SJohn Marino  * All rights reserved.
40d5acd74SJohn Marino  *
50d5acd74SJohn Marino  * This code is derived from software contributed to Berkeley by
60d5acd74SJohn Marino  * James da Silva at the University of Maryland at College Park.
70d5acd74SJohn Marino  *
80d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
90d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
100d5acd74SJohn Marino  * are met:
110d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
120d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
130d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
140d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
150d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
16*c66c7e2fSzrj  * 3. Neither the name of the University nor the names of its contributors
170d5acd74SJohn Marino  *    may be used to endorse or promote products derived from this software
180d5acd74SJohn Marino  *    without specific prior written permission.
190d5acd74SJohn Marino  *
200d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
210d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
230d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
240d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
250d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
260d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
270d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
280d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
290d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
300d5acd74SJohn Marino  * SUCH DAMAGE.
310d5acd74SJohn Marino  *
320d5acd74SJohn Marino  * @(#)regex.c	5.1 (Berkeley) 3/29/92
330d5acd74SJohn Marino  * $FreeBSD: head/lib/libcompat/4.3/re_comp.c 205146 2010-03-14 10:18:58Z ed $
340d5acd74SJohn Marino  */
350d5acd74SJohn Marino 
360d5acd74SJohn Marino /*
370d5acd74SJohn Marino  * Compatibility routines that implement the old re_comp/re_exec interface in
380d5acd74SJohn Marino  * terms of the regcomp/regexec interface.  It's possible that some programs
390d5acd74SJohn Marino  * rely on dark corners of re_comp/re_exec and won't work with this version,
400d5acd74SJohn Marino  * but most programs should be fine.
410d5acd74SJohn Marino  */
420d5acd74SJohn Marino 
430d5acd74SJohn Marino #include <regex.h>
440d5acd74SJohn Marino #include <stddef.h>
450d5acd74SJohn Marino #include <unistd.h>
460d5acd74SJohn Marino 
470d5acd74SJohn Marino static regex_t re_regexp;
480d5acd74SJohn Marino static int re_gotexp;
490d5acd74SJohn Marino static char re_errstr[100];
500d5acd74SJohn Marino 
510d5acd74SJohn Marino char *
re_comp(const char * s)520d5acd74SJohn Marino re_comp(const char *s)
530d5acd74SJohn Marino {
540d5acd74SJohn Marino 	int rc;
550d5acd74SJohn Marino 
560d5acd74SJohn Marino 	if (s == NULL || *s == '\0') {
570d5acd74SJohn Marino 		if (!re_gotexp)
580d5acd74SJohn Marino 			return __DECONST(char *,
590d5acd74SJohn Marino 			    "no previous regular expression");
600d5acd74SJohn Marino 		return (NULL);
610d5acd74SJohn Marino 	}
620d5acd74SJohn Marino 
630d5acd74SJohn Marino 	if (re_gotexp) {
640d5acd74SJohn Marino 		regfree(&re_regexp);
650d5acd74SJohn Marino 		re_gotexp = 0;
660d5acd74SJohn Marino 	}
670d5acd74SJohn Marino 
680d5acd74SJohn Marino 	rc = regcomp(&re_regexp, s, REG_EXTENDED);
690d5acd74SJohn Marino 	if (rc == 0) {
700d5acd74SJohn Marino 		re_gotexp = 1;
710d5acd74SJohn Marino 		return (NULL);
720d5acd74SJohn Marino 	}
730d5acd74SJohn Marino 
740d5acd74SJohn Marino 	regerror(rc, &re_regexp, re_errstr, sizeof(re_errstr));
750d5acd74SJohn Marino 	re_errstr[sizeof(re_errstr) - 1] = '\0';
760d5acd74SJohn Marino 	return (re_errstr);
770d5acd74SJohn Marino }
780d5acd74SJohn Marino 
790d5acd74SJohn Marino int
re_exec(const char * s)800d5acd74SJohn Marino re_exec(const char *s)
810d5acd74SJohn Marino {
820d5acd74SJohn Marino 	int rc;
830d5acd74SJohn Marino 
840d5acd74SJohn Marino 	if (!re_gotexp)
850d5acd74SJohn Marino 		return (-1);
860d5acd74SJohn Marino 	rc = regexec(&re_regexp, s, 0, NULL, 0);
870d5acd74SJohn Marino 	return (rc == 0 ? 1 : 0);
880d5acd74SJohn Marino }
89