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