regfree.c revision 1.19 1 1.19 christos /* $NetBSD: regfree.c,v 1.19 2021/02/26 19:24:47 christos Exp $ */
2 1.4 cgd
3 1.3 cgd /*-
4 1.16 christos * SPDX-License-Identifier: BSD-3-Clause
5 1.16 christos *
6 1.16 christos * Copyright (c) 1992, 1993, 1994 Henry Spencer.
7 1.3 cgd * Copyright (c) 1992, 1993, 1994
8 1.3 cgd * The Regents of the University of California. All rights reserved.
9 1.3 cgd *
10 1.3 cgd * This code is derived from software contributed to Berkeley by
11 1.3 cgd * Henry Spencer.
12 1.3 cgd *
13 1.3 cgd * Redistribution and use in source and binary forms, with or without
14 1.3 cgd * modification, are permitted provided that the following conditions
15 1.3 cgd * are met:
16 1.3 cgd * 1. Redistributions of source code must retain the above copyright
17 1.3 cgd * notice, this list of conditions and the following disclaimer.
18 1.3 cgd * 2. Redistributions in binary form must reproduce the above copyright
19 1.3 cgd * notice, this list of conditions and the following disclaimer in the
20 1.3 cgd * documentation and/or other materials provided with the distribution.
21 1.13 agc * 3. Neither the name of the University nor the names of its contributors
22 1.13 agc * may be used to endorse or promote products derived from this software
23 1.13 agc * without specific prior written permission.
24 1.13 agc *
25 1.13 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.13 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.13 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.13 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.13 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.13 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.13 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.13 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.13 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.13 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.13 agc * SUCH DAMAGE.
36 1.13 agc *
37 1.13 agc * @(#)regfree.c 8.3 (Berkeley) 3/20/94
38 1.13 agc */
39 1.13 agc
40 1.19 christos #if HAVE_NBTOOL_CONFIG_H
41 1.19 christos #include "nbtool_config.h"
42 1.19 christos #endif
43 1.19 christos
44 1.5 christos #include <sys/cdefs.h>
45 1.4 cgd #if 0
46 1.3 cgd static char sccsid[] = "@(#)regfree.c 8.3 (Berkeley) 3/20/94";
47 1.16 christos __FBSDID("$FreeBSD: head/lib/libc/regex/regfree.c 326025 2017-11-20 19:49:47Z pfg $");
48 1.4 cgd #endif
49 1.19 christos __RCSID("$NetBSD: regfree.c,v 1.19 2021/02/26 19:24:47 christos Exp $");
50 1.3 cgd
51 1.6 jtc #include "namespace.h"
52 1.1 jtc #include <sys/types.h>
53 1.1 jtc #include <stdio.h>
54 1.1 jtc #include <stdlib.h>
55 1.16 christos #include <limits.h>
56 1.15 junyoung #include <regex.h>
57 1.6 jtc
58 1.6 jtc #ifdef __weak_alias
59 1.12 mycroft __weak_alias(regfree,_regfree)
60 1.6 jtc #endif
61 1.1 jtc
62 1.1 jtc #include "utils.h"
63 1.1 jtc #include "regex2.h"
64 1.1 jtc
65 1.1 jtc /*
66 1.1 jtc - regfree - free everything
67 1.2 jtc = extern void regfree(regex_t *);
68 1.1 jtc */
69 1.1 jtc void
70 1.16 christos regfree(regex_t *preg)
71 1.1 jtc {
72 1.7 perry struct re_guts *g;
73 1.16 christos unsigned int i;
74 1.1 jtc
75 1.10 lukem _DIAGASSERT(preg != NULL);
76 1.10 lukem
77 1.10 lukem _DIAGASSERT(preg->re_magic == MAGIC1);
78 1.1 jtc if (preg->re_magic != MAGIC1) /* oops */
79 1.1 jtc return; /* nice to complain, but hard */
80 1.1 jtc
81 1.1 jtc g = preg->re_g;
82 1.1 jtc if (g == NULL || g->magic != MAGIC2) /* oops again */
83 1.1 jtc return;
84 1.1 jtc preg->re_magic = 0; /* mark it invalid */
85 1.1 jtc g->magic = 0; /* mark it invalid */
86 1.1 jtc
87 1.1 jtc if (g->strip != NULL)
88 1.17 christos free(g->strip);
89 1.16 christos if (g->sets != NULL) {
90 1.16 christos for (i = 0; i < g->ncsets; i++) {
91 1.16 christos free(g->sets[i].ranges);
92 1.16 christos free(g->sets[i].wides);
93 1.16 christos free(g->sets[i].types);
94 1.16 christos }
95 1.17 christos free(g->sets);
96 1.16 christos }
97 1.1 jtc if (g->must != NULL)
98 1.1 jtc free(g->must);
99 1.16 christos if (g->charjump != NULL)
100 1.16 christos free(&g->charjump[CHAR_MIN]);
101 1.16 christos if (g->matchjump != NULL)
102 1.16 christos free(g->matchjump);
103 1.17 christos free(g);
104 1.1 jtc }
105