xref: /netbsd/lib/libcompat/4.3/regex.c (revision eb7c1594)
19e98d4b8Snate /*-
29e98d4b8Snate  * Copyright (c) 1992 The Regents of the University of California.
39e98d4b8Snate  * All rights reserved.
49e98d4b8Snate  *
59e98d4b8Snate  * This code is derived from software contributed to Berkeley by
69e98d4b8Snate  * James da Silva at the University of Maryland at College Park.
79e98d4b8Snate  *
89e98d4b8Snate  * Redistribution and use in source and binary forms, with or without
99e98d4b8Snate  * modification, are permitted provided that the following conditions
109e98d4b8Snate  * are met:
119e98d4b8Snate  * 1. Redistributions of source code must retain the above copyright
129e98d4b8Snate  *    notice, this list of conditions and the following disclaimer.
139e98d4b8Snate  * 2. Redistributions in binary form must reproduce the above copyright
149e98d4b8Snate  *    notice, this list of conditions and the following disclaimer in the
159e98d4b8Snate  *    documentation and/or other materials provided with the distribution.
16*eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
179e98d4b8Snate  *    may be used to endorse or promote products derived from this software
189e98d4b8Snate  *    without specific prior written permission.
199e98d4b8Snate  *
209e98d4b8Snate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219e98d4b8Snate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229e98d4b8Snate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239e98d4b8Snate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249e98d4b8Snate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259e98d4b8Snate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269e98d4b8Snate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279e98d4b8Snate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289e98d4b8Snate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299e98d4b8Snate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309e98d4b8Snate  * SUCH DAMAGE.
319e98d4b8Snate  */
329e98d4b8Snate 
339e98d4b8Snate /*
349e98d4b8Snate  * Compatibility routines that implement the old re_comp/re_exec interface in
359e98d4b8Snate  * terms of the regcomp/regexec interface.  It's possible that some programs
369e98d4b8Snate  * rely on dark corners of re_comp/re_exec and won't work with this version,
379e98d4b8Snate  * but most programs should be fine.
389e98d4b8Snate  */
399e98d4b8Snate 
40e37aec67Slukem #include <sys/cdefs.h>
419e98d4b8Snate #if defined(LIBC_SCCS) && !defined(lint)
42e37aec67Slukem #if 0
43e37aec67Slukem static char sccsid[] = "from: @(#)regex.c	5.1 (Berkeley) 3/29/92";*/
44e37aec67Slukem #else
45*eb7c1594Sagc __RCSID("$NetBSD: regex.c,v 1.12 2003/08/07 16:44:16 agc Exp $");
46e37aec67Slukem #endif
479e98d4b8Snate #endif /* LIBC_SCCS and not lint */
489e98d4b8Snate 
499e98d4b8Snate #include <sys/types.h>
509e98d4b8Snate #include <stddef.h>
513a4f700dSchristos #ifdef __lint__
523a4f700dSchristos #define __compat_regerror __compat43_regerror
533a4f700dSchristos #endif
54b48252f3Slukem #include <assert.h>
55ec22bca1Sjtc #include <regexp.h>
56695ad15bSkleink #include <re_comp.h>
57c52b10ccSjtc #include <string.h>
58ec22bca1Sjtc #include <stdlib.h>
59e37aec67Slukem #include <unistd.h>
609e98d4b8Snate 
61ec22bca1Sjtc static regexp *re_regexp;
62ec22bca1Sjtc static int re_goterr;
63ec22bca1Sjtc static char *re_errstr;
649e98d4b8Snate 
659e98d4b8Snate char *
re_comp(s)669e98d4b8Snate re_comp(s)
67dde1c1a0Spk 	const char *s;
689e98d4b8Snate {
69fd08cf75Sjtc 	if (s == NULL || *s == '\0')
709e98d4b8Snate 		return (NULL);
71ec22bca1Sjtc 	if (re_regexp)
72ec22bca1Sjtc 		free(re_regexp);
73ec22bca1Sjtc 	if (re_errstr)
74ec22bca1Sjtc 		free(re_errstr);
75ec22bca1Sjtc 	re_goterr = 0;
76ec22bca1Sjtc 	re_regexp = regcomp(s);
77ec22bca1Sjtc 	return (re_goterr ? re_errstr : NULL);
789e98d4b8Snate }
799e98d4b8Snate 
809e98d4b8Snate int
re_exec(s)819e98d4b8Snate re_exec(s)
82dde1c1a0Spk 	const char *s;
839e98d4b8Snate {
84ec22bca1Sjtc 	int rc;
859e98d4b8Snate 
86b48252f3Slukem 
87ec22bca1Sjtc 	re_goterr = 0;
88ec22bca1Sjtc 	rc = regexec(re_regexp, s);
89ec22bca1Sjtc 	return (re_goterr ? -1 : rc);
90ec22bca1Sjtc }
919e98d4b8Snate 
92ec22bca1Sjtc void
regerror(s)93ec22bca1Sjtc regerror(s)
94ec22bca1Sjtc 	const char *s;
95ec22bca1Sjtc {
96b48252f3Slukem 
97b48252f3Slukem 	_DIAGASSERT(s != NULL);
98b48252f3Slukem 
99ec22bca1Sjtc 	re_goterr = 1;
100ec22bca1Sjtc 	if (re_errstr)
101ec22bca1Sjtc 		free(re_errstr);
102ec22bca1Sjtc 	re_errstr = strdup(s);
1039e98d4b8Snate }
104