1 /* $DragonFly: src/lib/libcompat/regexp/regsub.c,v 1.2 2004/10/25 19:38:02 drhodus Exp $ */
5 * Copyright (c) 1986 by University of Toronto.
6 * Written by Henry Spencer. Not derived from licensed software.
8 * Permission is granted to anyone to use this software for any
9 * purpose on any computer system, and to redistribute it freely,
10 * subject to the following restrictions:
12 * 1. The author is not responsible for the consequences of use of
13 * this software, no matter how awful, even if they arise
16 * 2. The origin of this software must not be misrepresented, either
17 * by explicit claim or by omission.
19 * 3. Altered versions must be plainly marked as such, and must not
20 * be misrepresented as being the original software.
28 #define UCHARAT(p) ((int)*(unsigned char *)(p))
30 #define UCHARAT(p) ((int)*(p)&CHARBITS)
34 - regsub - perform substitutions after a regexp match
37 regsub(prog, source, dest)
47 extern char *strncpy();
49 if (prog == NULL || source == NULL || dest == NULL) {
50 regerror("NULL parm to regsub");
53 if (UCHARAT(prog->program) != MAGIC) {
54 regerror("damaged regexp fed to regsub");
60 while ((c = *src++) != '\0') {
63 else if (c == '\\' && '0' <= *src && *src <= '9')
67 if (no < 0) { /* Ordinary character. */
68 if (c == '\\' && (*src == '\\' || *src == '&'))
71 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
72 len = prog->endp[no] - prog->startp[no];
73 (void) strncpy(dst, prog->startp[no], len);
75 if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
76 regerror("damaged match string");