]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libcompat/regexp/regsub.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libcompat / regexp / regsub.c
1 /*
2  * regsub
3  *
4  *      Copyright (c) 1986 by University of Toronto.
5  *      Written by Henry Spencer.  Not derived from licensed software.
6  *
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:
10  *
11  *      1. The author is not responsible for the consequences of use of
12  *              this software, no matter how awful, even if they arise
13  *              from defects in it.
14  *
15  *      2. The origin of this software must not be misrepresented, either
16  *              by explicit claim or by omission.
17  *
18  *      3. Altered versions must be plainly marked as such, and must not
19  *              be misrepresented as being the original software.
20  */
21
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24
25 #include <regexp.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "regmagic.h"
29
30 #ifndef CHARBITS
31 #define UCHARAT(p)      ((int)*(unsigned char *)(p))
32 #else
33 #define UCHARAT(p)      ((int)*(p)&CHARBITS)
34 #endif
35
36 /*
37  - regsub - perform substitutions after a regexp match
38  */
39 void
40 regsub(prog, source, dest)
41 const regexp *prog;
42 const char *source;
43 char *dest;
44 {
45         char *src;
46         char *dst;
47         char c;
48         int no;
49         int len;
50         extern char *strncpy();
51
52         if (prog == NULL || source == NULL || dest == NULL) {
53                 regerror("NULL parm to regsub");
54                 return;
55         }
56         if (UCHARAT(prog->program) != MAGIC) {
57                 regerror("damaged regexp fed to regsub");
58                 return;
59         }
60
61         src = (char *)source;
62         dst = dest;
63         while ((c = *src++) != '\0') {
64                 if (c == '&')
65                         no = 0;
66                 else if (c == '\\' && '0' <= *src && *src <= '9')
67                         no = *src++ - '0';
68                 else
69                         no = -1;
70                 if (no < 0) {   /* Ordinary character. */
71                         if (c == '\\' && (*src == '\\' || *src == '&'))
72                                 c = *src++;
73                         *dst++ = c;
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);
77                         dst += len;
78                         if (len != 0 && *(dst-1) == '\0') {     /* strncpy hit NUL. */
79                                 regerror("damaged match string");
80                                 return;
81                         }
82                 }
83         }
84         *dst++ = '\0';
85 }