Home | History | Annotate | Line # | Download | only in sed
misc.c revision 1.1.1.2
      1      1.1       alm /*-
      2      1.1       alm  * Copyright (c) 1992 Diomidis Spinellis.
      3  1.1.1.1       mrg  * Copyright (c) 1992, 1993
      4  1.1.1.1       mrg  *	The Regents of the University of California.  All rights reserved.
      5      1.1       alm  *
      6      1.1       alm  * This code is derived from software contributed to Berkeley by
      7      1.1       alm  * Diomidis Spinellis of Imperial College, University of London.
      8      1.1       alm  *
      9      1.1       alm  * Redistribution and use in source and binary forms, with or without
     10      1.1       alm  * modification, are permitted provided that the following conditions
     11      1.1       alm  * are met:
     12      1.1       alm  * 1. Redistributions of source code must retain the above copyright
     13      1.1       alm  *    notice, this list of conditions and the following disclaimer.
     14      1.1       alm  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1       alm  *    notice, this list of conditions and the following disclaimer in the
     16      1.1       alm  *    documentation and/or other materials provided with the distribution.
     17      1.1       alm  * 4. Neither the name of the University nor the names of its contributors
     18      1.1       alm  *    may be used to endorse or promote products derived from this software
     19      1.1       alm  *    without specific prior written permission.
     20      1.1       alm  *
     21      1.1       alm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1       alm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1       alm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1       alm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1       alm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1       alm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1       alm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1       alm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1       alm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1       alm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1       alm  * SUCH DAMAGE.
     32      1.1       alm  */
     33      1.1       alm 
     34  1.1.1.2  christos #include <sys/cdefs.h>
     35  1.1.1.2  christos __FBSDID("$FreeBSD: head/usr.bin/sed/misc.c 200462 2009-12-13 03:14:06Z delphij $");
     36  1.1.1.2  christos 
     37      1.1       alm #ifndef lint
     38  1.1.1.2  christos static const char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
     39  1.1.1.2  christos #endif
     40      1.1       alm 
     41      1.1       alm #include <sys/types.h>
     42      1.1       alm 
     43  1.1.1.2  christos #include <err.h>
     44  1.1.1.2  christos #include <limits.h>
     45      1.1       alm #include <regex.h>
     46      1.1       alm #include <stdio.h>
     47      1.1       alm #include <stdlib.h>
     48      1.1       alm #include <string.h>
     49      1.1       alm 
     50      1.1       alm #include "defs.h"
     51      1.1       alm #include "extern.h"
     52      1.1       alm 
     53      1.1       alm /*
     54  1.1.1.2  christos  * Return a string for a regular expression error passed.  This is overkill,
     55      1.1       alm  * because of the silly semantics of regerror (we can never know the size of
     56      1.1       alm  * the buffer).
     57      1.1       alm  */
     58      1.1       alm char *
     59  1.1.1.2  christos strregerror(int errcode, regex_t *preg)
     60      1.1       alm {
     61      1.1       alm 	static char *oe;
     62      1.1       alm 	size_t s;
     63      1.1       alm 
     64      1.1       alm 	if (oe != NULL)
     65      1.1       alm 		free(oe);
     66  1.1.1.2  christos 	s = regerror(errcode, preg, NULL, 0);
     67  1.1.1.2  christos 	if ((oe = malloc(s)) == NULL)
     68  1.1.1.2  christos 		err(1, "malloc");
     69      1.1       alm 	(void)regerror(errcode, preg, oe, s);
     70      1.1       alm 	return (oe);
     71      1.1       alm }
     72