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