|
|
1.1 root 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: ********* This version is slightly altered to fit cleanly with Coherent
21: ********* libmisc.a. The original may be obtained freely on mwcbbs.
22: */
23: #include <stdio.h>
24: #include "local_misc.h"
25:
26: #ifndef CHARBITS
27: #define UCHARAT(p) ((int)*(unsigned char *)(p))
28: #else
29: #define UCHARAT(p) ((int)*(p)&CHARBITS)
30: #endif
31:
32: #define FAIL(msg) { regerror(msg); return; }
33:
34: /*
35: - regsub - perform substitutions after a regexp match
36: */
37: void
38: regsub(prog, source, dest)
39: regexp *prog;
40: char *source;
41: char *dest;
42: {
43: register char *src;
44: register char *dst;
45: register char c;
46: register int no;
47: register int len;
48: extern char *strncpy();
49:
50: if (prog == NULL || source == NULL || dest == NULL)
51: FAIL("NULL parm to regsub");
52: if (UCHARAT(prog->program) != (unsigned char)REG_MAGIC)
53: FAIL("damaged regexp fed to regsub");
54:
55: src = source;
56: dst = dest;
57: while ((c = *src++) != '\0') {
58: if (c == '&')
59: no = 0;
60: else if (c == '\\' && '0' <= *src && *src <= '9')
61: no = *src++ - '0';
62: else
63: no = -1;
64:
65: if (no < 0) /* Ordinary character. */
66: *dst++ = c;
67: else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
68: len = prog->endp[no] - prog->startp[no];
69: (void) strncpy(dst, prog->startp[no], len);
70: dst += len;
71: /* strncpy hit NUL? */
72: if (len != 0 && *(dst-1) == '\0')
73: FAIL("damaged mch string");
74: }
75: }
76: *dst++ = '\0';
77: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.