keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libcompat / regexp / regsub.c
1 /* $DragonFly: src/lib/libcompat/regexp/regsub.c,v 1.3 2008/09/30 16:57:04 swildner Exp $                                                               */
2 /*
3  * regsub
4  *
5  *      Copyright (c) 1986 by University of Toronto.
6  *      Written by Henry Spencer.  Not derived from licensed software.
7  *
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:
11  *
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.
15  *
16  *      2. The origin of this software must not be misrepresented, either
17  *              by explicit claim or by omission.
18  *
19  *      3. Altered versions must be plainly marked as such, and must not
20  *              be misrepresented as being the original software.
21  */
22 #include <regexp.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "regmagic.h"
26
27 #ifndef CHARBITS
28 #define UCHARAT(p)      ((int)*(unsigned char *)(p))
29 #else
30 #define UCHARAT(p)      ((int)*(p)&CHARBITS)
31 #endif
32
33 /*
34  - regsub - perform substitutions after a regexp match
35  */
36 void
37 regsub(const regexp *prog, const char *source, char *dest)
38 {
39         char *src;
40         char *dst;
41         char c;
42         int no;
43         int len;
44         extern char *strncpy();
45
46         if (prog == NULL || source == NULL || dest == NULL) {
47                 regerror("NULL parm to regsub");
48                 return;
49         }
50         if (UCHARAT(prog->program) != MAGIC) {
51                 regerror("damaged regexp fed to regsub");
52                 return;
53         }
54
55         src = (char *)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                 if (no < 0) {   /* Ordinary character. */
65                         if (c == '\\' && (*src == '\\' || *src == '&'))
66                                 c = *src++;
67                         *dst++ = c;
68                 } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
69                         len = prog->endp[no] - prog->startp[no];
70                         (void) strncpy(dst, prog->startp[no], len);
71                         dst += len;
72                         if (len != 0 && *(dst-1) == '\0') {     /* strncpy hit NUL. */
73                                 regerror("damaged match string");
74                                 return;
75                         }
76                 }
77         }
78         *dst++ = '\0';
79 }