regfree.c revision 317894
1126756Smlaier/*-
2225452Sdelphij * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3126756Smlaier * Copyright (c) 1992, 1993, 1994
4126756Smlaier *	The Regents of the University of California.  All rights reserved.
5126756Smlaier *
6126756Smlaier * This code is derived from software contributed to Berkeley by
7126756Smlaier * Henry Spencer.
8126756Smlaier *
9126756Smlaier * Redistribution and use in source and binary forms, with or without
10126756Smlaier * modification, are permitted provided that the following conditions
11126756Smlaier * are met:
12126756Smlaier * 1. Redistributions of source code must retain the above copyright
13126756Smlaier *    notice, this list of conditions and the following disclaimer.
14126756Smlaier * 2. Redistributions in binary form must reproduce the above copyright
15126756Smlaier *    notice, this list of conditions and the following disclaimer in the
16126756Smlaier *    documentation and/or other materials provided with the distribution.
17126756Smlaier * 4. Neither the name of the University nor the names of its contributors
18126756Smlaier *    may be used to endorse or promote products derived from this software
19126756Smlaier *    without specific prior written permission.
20126756Smlaier *
21126756Smlaier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22126756Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23126756Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24126756Smlaier * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25135183Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26135183Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27126756Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28126756Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29126756Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30126756Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31126756Smlaier * SUCH DAMAGE.
32126756Smlaier *
33126756Smlaier *	@(#)regfree.c	8.3 (Berkeley) 3/20/94
34126756Smlaier */
35126756Smlaier
36126756Smlaier#if defined(LIBC_SCCS) && !defined(lint)
37126756Smlaierstatic char sccsid[] = "@(#)regfree.c	8.3 (Berkeley) 3/20/94";
38126756Smlaier#endif /* LIBC_SCCS and not lint */
39126756Smlaier#include <sys/cdefs.h>
40126756Smlaier__FBSDID("$FreeBSD: stable/11/lib/libc/regex/regfree.c 317894 2017-05-07 01:31:42Z pfg $");
41126756Smlaier
42126756Smlaier#include <sys/types.h>
43126756Smlaier#include <stdio.h>
44126756Smlaier#include <stdlib.h>
45126756Smlaier#include <limits.h>
46126756Smlaier#include <regex.h>
47126756Smlaier#include <wchar.h>
48126756Smlaier#include <wctype.h>
49126756Smlaier
50126756Smlaier#include "utils.h"
51126756Smlaier#include "regex2.h"
52126756Smlaier
53126756Smlaier/*
54126756Smlaier - regfree - free everything
55126756Smlaier = extern void regfree(regex_t *);
56126756Smlaier */
57126756Smlaiervoid
58126756Smlaierregfree(regex_t *preg)
59126756Smlaier{
60126756Smlaier	struct re_guts *g;
61126756Smlaier	unsigned int i;
62126756Smlaier
63126756Smlaier	if (preg->re_magic != MAGIC1)	/* oops */
64126756Smlaier		return;			/* nice to complain, but hard */
65126756Smlaier
66126756Smlaier	g = preg->re_g;
67126756Smlaier	if (g == NULL || g->magic != MAGIC2)	/* oops again */
68126756Smlaier		return;
69126756Smlaier	preg->re_magic = 0;		/* mark it invalid */
70126756Smlaier	g->magic = 0;			/* mark it invalid */
71126756Smlaier
72126756Smlaier	if (g->strip != NULL)
73126756Smlaier		free((char *)g->strip);
74126756Smlaier	if (g->sets != NULL) {
75126756Smlaier		for (i = 0; i < g->ncsets; i++) {
76126756Smlaier			free(g->sets[i].ranges);
77126756Smlaier			free(g->sets[i].wides);
78126756Smlaier			free(g->sets[i].types);
79126756Smlaier		}
80126756Smlaier		free((char *)g->sets);
81126756Smlaier	}
82126756Smlaier	if (g->must != NULL)
83126756Smlaier		free(g->must);
84126756Smlaier	if (g->charjump != NULL)
85126756Smlaier		free(&g->charjump[CHAR_MIN]);
86126756Smlaier	if (g->matchjump != NULL)
87126756Smlaier		free(g->matchjump);
88126756Smlaier	free((char *)g);
89126756Smlaier}
90126756Smlaier