Home | History | Annotate | Line # | Download | only in regex
regfree.c revision 1.16
      1  1.16  christos /*	$NetBSD: regfree.c,v 1.16 2021/02/23 22:14:59 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.5  christos #include <sys/cdefs.h>
     41   1.4       cgd #if 0
     42   1.3       cgd static char sccsid[] = "@(#)regfree.c	8.3 (Berkeley) 3/20/94";
     43  1.16  christos __FBSDID("$FreeBSD: head/lib/libc/regex/regfree.c 326025 2017-11-20 19:49:47Z pfg $");
     44   1.4       cgd #endif
     45  1.16  christos __RCSID("$NetBSD: regfree.c,v 1.16 2021/02/23 22:14:59 christos Exp $");
     46   1.3       cgd 
     47   1.6       jtc #include "namespace.h"
     48   1.1       jtc #include <sys/types.h>
     49   1.1       jtc #include <stdio.h>
     50   1.1       jtc #include <stdlib.h>
     51  1.16  christos #include <limits.h>
     52  1.15  junyoung #include <regex.h>
     53   1.6       jtc 
     54   1.6       jtc #ifdef __weak_alias
     55  1.12   mycroft __weak_alias(regfree,_regfree)
     56   1.6       jtc #endif
     57  1.16  christos #include <wchar.h>
     58  1.16  christos #include <wctype.h>
     59   1.1       jtc 
     60   1.1       jtc #include "utils.h"
     61   1.1       jtc #include "regex2.h"
     62   1.1       jtc 
     63   1.1       jtc /*
     64   1.1       jtc  - regfree - free everything
     65   1.2       jtc  = extern void regfree(regex_t *);
     66   1.1       jtc  */
     67   1.1       jtc void
     68  1.16  christos regfree(regex_t *preg)
     69   1.1       jtc {
     70   1.7     perry 	struct re_guts *g;
     71  1.16  christos 	unsigned int i;
     72   1.1       jtc 
     73  1.10     lukem 	_DIAGASSERT(preg != NULL);
     74  1.10     lukem 
     75  1.10     lukem 	_DIAGASSERT(preg->re_magic == MAGIC1);
     76   1.1       jtc 	if (preg->re_magic != MAGIC1)	/* oops */
     77   1.1       jtc 		return;			/* nice to complain, but hard */
     78   1.1       jtc 
     79   1.1       jtc 	g = preg->re_g;
     80   1.1       jtc 	if (g == NULL || g->magic != MAGIC2)	/* oops again */
     81   1.1       jtc 		return;
     82   1.1       jtc 	preg->re_magic = 0;		/* mark it invalid */
     83   1.1       jtc 	g->magic = 0;			/* mark it invalid */
     84   1.1       jtc 
     85   1.1       jtc 	if (g->strip != NULL)
     86  1.16  christos 		free((char *)g->strip);
     87  1.16  christos 	if (g->sets != NULL) {
     88  1.16  christos 		for (i = 0; i < g->ncsets; i++) {
     89  1.16  christos 			free(g->sets[i].ranges);
     90  1.16  christos 			free(g->sets[i].wides);
     91  1.16  christos 			free(g->sets[i].types);
     92  1.16  christos 		}
     93  1.16  christos 		free((char *)g->sets);
     94  1.16  christos 	}
     95   1.1       jtc 	if (g->must != NULL)
     96   1.1       jtc 		free(g->must);
     97  1.16  christos 	if (g->charjump != NULL)
     98  1.16  christos 		free(&g->charjump[CHAR_MIN]);
     99  1.16  christos 	if (g->matchjump != NULL)
    100  1.16  christos 		free(g->matchjump);
    101  1.16  christos 	free((char *)g);
    102   1.1       jtc }
    103