xref: /csrg-svn/lib/libc/compat-43/regex.c (revision 53075)
1*53075Sbostic /*-
2*53075Sbostic  * Copyright (c) 1992 The Regents of the University of California.
3*53075Sbostic  * All rights reserved.
4*53075Sbostic  *
5*53075Sbostic  * This code is derived from software contributed to Berkeley by
6*53075Sbostic  * James da Silva at the University of Maryland at College Park.
7*53075Sbostic  *
8*53075Sbostic  * %sccs.include.redist.c%
9*53075Sbostic  */
10*53075Sbostic 
11*53075Sbostic /*
12*53075Sbostic  * Compatibility routines that implement the old re_comp/re_exec interface in
13*53075Sbostic  * terms of the regcomp/regexec interface.  It's possible that some programs
14*53075Sbostic  * rely on dark corners of re_comp/re_exec and won't work with this version,
15*53075Sbostic  * but most programs should be fine.
16*53075Sbostic  */
17*53075Sbostic 
18*53075Sbostic #if defined(LIBC_SCCS) && !defined(lint)
19*53075Sbostic static char sccsid[] = "@(#)regex.c	5.1 (Berkeley) 03/29/92";
20*53075Sbostic #endif /* LIBC_SCCS and not lint */
21*53075Sbostic 
22*53075Sbostic #include <sys/types.h>
23*53075Sbostic #include <stddef.h>
24*53075Sbostic #include <regexp.h>
25*53075Sbostic #include <string.h>
26*53075Sbostic #include <stdlib.h>
27*53075Sbostic 
28*53075Sbostic static regexp *re_regexp;
29*53075Sbostic static int re_goterr;
30*53075Sbostic static char *re_errstr;
31*53075Sbostic 
32*53075Sbostic char *
33*53075Sbostic re_comp(s)
34*53075Sbostic 	char *s;
35*53075Sbostic {
36*53075Sbostic 	if (s == NULL)
37*53075Sbostic 		return (NULL);
38*53075Sbostic 	if (re_regexp)
39*53075Sbostic 		free(re_regexp);
40*53075Sbostic 	if (re_errstr)
41*53075Sbostic 		free(re_errstr);
42*53075Sbostic 	re_goterr = 0;
43*53075Sbostic 	re_regexp = regcomp(s);
44*53075Sbostic 	return (re_goterr ? re_errstr : NULL);
45*53075Sbostic }
46*53075Sbostic 
47*53075Sbostic int
48*53075Sbostic re_exec(s)
49*53075Sbostic 	char *s;
50*53075Sbostic {
51*53075Sbostic 	int rc;
52*53075Sbostic 
53*53075Sbostic 	re_goterr = 0;
54*53075Sbostic 	rc = regexec(re_regexp, s);
55*53075Sbostic 	return (re_goterr ? -1 : rc);
56*53075Sbostic }
57*53075Sbostic 
58*53075Sbostic void
59*53075Sbostic regerror(s)
60*53075Sbostic 	const char *s;
61*53075Sbostic {
62*53075Sbostic 	re_goterr = 1;
63*53075Sbostic 	if (re_errstr)
64*53075Sbostic 		free(re_errstr);
65*53075Sbostic 	re_errstr = strdup(s);
66*53075Sbostic }
67