4 * Copyright (c) 1986 by University of Toronto.
5 * Written by Henry Spencer. Not derived from licensed software.
7 * Permission is granted to anyone to use this software for any
8 * purpose on any computer system, and to redistribute it freely,
9 * subject to the following restrictions:
11 * 1. The author is not responsible for the consequences of use of
12 * this software, no matter how awful, even if they arise
15 * 2. The origin of this software must not be misrepresented, either
16 * by explicit claim or by omission.
18 * 3. Altered versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
31 #define UCHARAT(p) ((int)*(unsigned char *)(p))
33 #define UCHARAT(p) ((int)*(p)&CHARBITS)
37 - regsub - perform substitutions after a regexp match
40 regsub(prog, source, dest)
50 extern char *strncpy();
52 if (prog == NULL || source == NULL || dest == NULL) {
53 regerror("NULL parm to regsub");
56 if (UCHARAT(prog->program) != MAGIC) {
57 regerror("damaged regexp fed to regsub");
63 while ((c = *src++) != '\0') {
66 else if (c == '\\' && '0' <= *src && *src <= '9')
70 if (no < 0) { /* Ordinary character. */
71 if (c == '\\' && (*src == '\\' || *src == '&'))
74 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
75 len = prog->endp[no] - prog->startp[no];
76 (void) strncpy(dst, prog->startp[no], len);
78 if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
79 regerror("damaged match string");