For /dev/mem, map in memory to be copied to memory's own address space
[minix3.git] / lib / other / v8regsub.c
blobc2b7819a765de38b77962ff9475ff8c5d08331bd
1 /* regsub
3 * Copyright (c) 1986 by University of Toronto.
4 * Written by Henry Spencer. Not derived from licensed software.
6 * Permission is granted to anyone to use this software for any
7 * purpose on any computer system, and to redistribute it freely,
8 * subject to the following restrictions:
10 * 1. The author is not responsible for the consequences of use of
11 * this software, no matter how awful, even if they arise
12 * from defects in it.
14 * 2. The origin of this software must not be misrepresented, either
15 * by explicit claim or by omission.
17 * 3. Altered versions must be plainly marked as such, and must not
18 * be misrepresented as being the original software.
21 #include <string.h>
22 #include <stdio.h>
23 #define const /* avoid "const poisoning" */
24 #include <regexp.h>
25 #undef const
27 /* The first byte of the regexp internal "program" is actually this magic
28 * number; the start node begins in the second byte.
30 #define MAGIC 0234
32 #define CHARBITS 0377
33 #ifndef CHARBITS
34 #define UCHARAT(p) ((int)*(unsigned char *)(p))
35 #else
36 #define UCHARAT(p) ((int)*(p)&CHARBITS)
37 #endif
40 - regsub - perform substitutions after a regexp match
42 void regsub(prog, source, dest)
43 regexp *prog;
44 char *source;
45 char *dest;
47 register char *src;
48 register char *dst;
49 register char c;
50 register int no;
51 register int len;
53 if (prog == (regexp *)NULL || source == (char *)NULL || dest == (char *)NULL) {
54 regerror("NULL parm to regsub");
55 return;
57 if (UCHARAT(prog->program) != MAGIC) {
58 regerror("damaged regexp fed to regsub");
59 return;
61 src = 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;
71 if (no < 0) { /* Ordinary character. */
72 if (c == '\\' && (*src == '\\' || *src == '&')) c = *src++;
73 *dst++ = c;
74 } else
75 if (prog->startp[no] != (char *)NULL && prog->endp[no] != (char *)NULL) {
76 len = (int) (prog->endp[no] - prog->startp[no]);
77 strncpy(dst, prog->startp[no], len);
78 dst += len;
79 if (len != 0 && *(dst - 1) == '\0') { /* strncpy hit NUL. */
80 regerror("damaged match string");
81 return;
85 *dst++ = '\0';
89 * $PchId: regsub.c,v 1.3 1995/11/27 20:18:16 philip Exp $