xref: /csrg-svn/lib/libc/compat-43/regex.c (revision 60986)
153075Sbostic /*-
2*60986Sbostic  * Copyright (c) 1992, 1993
3*60986Sbostic  *	The Regents of the University of California.  All rights reserved.
453075Sbostic  *
553075Sbostic  * This code is derived from software contributed to Berkeley by
653075Sbostic  * James da Silva at the University of Maryland at College Park.
753075Sbostic  *
853075Sbostic  * %sccs.include.redist.c%
953075Sbostic  */
1053075Sbostic 
1153075Sbostic /*
1253075Sbostic  * Compatibility routines that implement the old re_comp/re_exec interface in
1353075Sbostic  * terms of the regcomp/regexec interface.  It's possible that some programs
1453075Sbostic  * rely on dark corners of re_comp/re_exec and won't work with this version,
1553075Sbostic  * but most programs should be fine.
1653075Sbostic  */
1753075Sbostic 
1853075Sbostic #if defined(LIBC_SCCS) && !defined(lint)
19*60986Sbostic static char sccsid[] = "@(#)regex.c	8.1 (Berkeley) 06/02/93";
2053075Sbostic #endif /* LIBC_SCCS and not lint */
2153075Sbostic 
2253075Sbostic #include <sys/types.h>
2353075Sbostic #include <stddef.h>
2453075Sbostic #include <regexp.h>
2553075Sbostic #include <string.h>
2653075Sbostic #include <stdlib.h>
2753075Sbostic 
2853075Sbostic static regexp *re_regexp;
2953075Sbostic static int re_goterr;
3053075Sbostic static char *re_errstr;
3153075Sbostic 
3253075Sbostic char *
re_comp(s)3353075Sbostic re_comp(s)
3453075Sbostic 	char *s;
3553075Sbostic {
3653075Sbostic 	if (s == NULL)
3753075Sbostic 		return (NULL);
3853075Sbostic 	if (re_regexp)
3953075Sbostic 		free(re_regexp);
4053075Sbostic 	if (re_errstr)
4153075Sbostic 		free(re_errstr);
4253075Sbostic 	re_goterr = 0;
4353075Sbostic 	re_regexp = regcomp(s);
4453075Sbostic 	return (re_goterr ? re_errstr : NULL);
4553075Sbostic }
4653075Sbostic 
4753075Sbostic int
re_exec(s)4853075Sbostic re_exec(s)
4953075Sbostic 	char *s;
5053075Sbostic {
5153075Sbostic 	int rc;
5253075Sbostic 
5353075Sbostic 	re_goterr = 0;
5453075Sbostic 	rc = regexec(re_regexp, s);
5553075Sbostic 	return (re_goterr ? -1 : rc);
5653075Sbostic }
5753075Sbostic 
5853075Sbostic void
regerror(s)5953075Sbostic regerror(s)
6053075Sbostic 	const char *s;
6153075Sbostic {
6253075Sbostic 	re_goterr = 1;
6353075Sbostic 	if (re_errstr)
6453075Sbostic 		free(re_errstr);
6553075Sbostic 	re_errstr = strdup(s);
6653075Sbostic }
67