MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / net / ipv4 / netfilter / regexp / regsub.c
blob339631f06f004e3263c7755593007306ed553b80
1 /*
2 * regsub
3 * @(#)regsub.c 1.3 of 2 April 86
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
14 * from defects in it.
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.
23 * This code was modified by Ethan Sommer to work within the kernel
24 * (it now uses kmalloc etc..)
27 #include "regexp.h"
28 #include "regmagic.h"
29 #include <linux/string.h>
32 #ifndef CHARBITS
33 #define UCHARAT(p) ((int)*(unsigned char *)(p))
34 #else
35 #define UCHARAT(p) ((int)*(p)&CHARBITS)
36 #endif
38 #if 0
39 //void regerror(char * s)
40 //{
41 // printk("regexp(3): %s", s);
42 // /* NOTREACHED */
43 //}
44 #endif
47 - regsub - perform substitutions after a regexp match
49 void
50 regsub(regexp * prog, char * source, char * dest)
52 register char *src;
53 register char *dst;
54 register char c;
55 register int no;
56 register int len;
58 /* Not necessary and gcc doesn't like it -MLS */
59 /*extern char *strncpy();*/
61 if (prog == NULL || source == NULL || dest == NULL) {
62 regerror("NULL parm to regsub");
63 return;
65 if (UCHARAT(prog->program) != MAGIC) {
66 regerror("damaged regexp fed to regsub");
67 return;
70 src = source;
71 dst = dest;
72 while ((c = *src++) != '\0') {
73 if (c == '&')
74 no = 0;
75 else if (c == '\\' && '0' <= *src && *src <= '9')
76 no = *src++ - '0';
77 else
78 no = -1;
80 if (no < 0) { /* Ordinary character. */
81 if (c == '\\' && (*src == '\\' || *src == '&'))
82 c = *src++;
83 *dst++ = c;
84 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
85 len = prog->endp[no] - prog->startp[no];
86 (void) strncpy(dst, prog->startp[no], len);
87 dst += len;
88 if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
89 regerror("damaged match string");
90 return;
94 *dst++ = '\0';