Remove #ifndef __cplusplus around wchar related stuff in include/wchar.h
[dragonfly.git] / lib / libcompat / regexp / regsub.c
blob57b9eb0fcb76744ebdedb30af6c3901855c598e2
1 /* $DragonFly: src/lib/libcompat/regexp/regsub.c,v 1.2 2004/10/25 19:38:02 drhodus Exp $ */
2 /*
3 * regsub
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.
22 #include <regexp.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "regmagic.h"
27 #ifndef CHARBITS
28 #define UCHARAT(p) ((int)*(unsigned char *)(p))
29 #else
30 #define UCHARAT(p) ((int)*(p)&CHARBITS)
31 #endif
34 - regsub - perform substitutions after a regexp match
36 void
37 regsub(prog, source, dest)
38 const regexp *prog;
39 const char *source;
40 char *dest;
42 char *src;
43 char *dst;
44 char c;
45 int no;
46 int len;
47 extern char *strncpy();
49 if (prog == NULL || source == NULL || dest == NULL) {
50 regerror("NULL parm to regsub");
51 return;
53 if (UCHARAT(prog->program) != MAGIC) {
54 regerror("damaged regexp fed to regsub");
55 return;
58 src = (char *)source;
59 dst = dest;
60 while ((c = *src++) != '\0') {
61 if (c == '&')
62 no = 0;
63 else if (c == '\\' && '0' <= *src && *src <= '9')
64 no = *src++ - '0';
65 else
66 no = -1;
67 if (no < 0) { /* Ordinary character. */
68 if (c == '\\' && (*src == '\\' || *src == '&'))
69 c = *src++;
70 *dst++ = c;
71 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
72 len = prog->endp[no] - prog->startp[no];
73 (void) strncpy(dst, prog->startp[no], len);
74 dst += len;
75 if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
76 regerror("damaged match string");
77 return;
81 *dst++ = '\0';