1 /* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */
2
3 /*
4 * Copyright (c) 2002 J. Mallett. All rights reserved.
5 * You may do whatever you want with this file as long as
6 * the above copyright and this notice remain intact, along
7 * with the following statement:
8 * For the man who taught me vi, and who got too old, too young.
9 */
10
11 #include <sys/cdefs.h>
12 #ifndef lint
13 #if 0
14 __FBSDID("$FreeBSD: src/usr.bin/xargs/strnsubst.c,v 1.8 2005/12/30 23:22:50 jmallett Exp $");
15 #endif
16 __RCSID("$NetBSD: strnsubst.c,v 1.1 2007/04/18 15:56:07 christos Exp $");
17 #endif /* not lint */
18
19 #include <err.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <stdint.h>
24
25 void strnsubst(char **, const char *, const char *, size_t);
26
27 /*
28 * Replaces str with a string consisting of str with match replaced with
29 * replstr as many times as can be done before the constructed string is
30 * maxsize bytes large. It does not free the string pointed to by str, it
31 * is up to the calling program to be sure that the original contents of
32 * str as well as the new contents are handled in an appropriate manner.
33 * If replstr is NULL, then that internally is changed to a nil-string, so
34 * that we can still pretend to do somewhat meaningful substitution.
35 * No value is returned.
36 */
37 void
strnsubst(char ** str,const char * match,const char * replstr,size_t maxsize)38 strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
39 {
40 char *s1, *s2, *this;
41
42 s1 = *str;
43 if (s1 == NULL)
44 return;
45 /*
46 * If maxsize is 0 then set it to to the length of s1, because we have
47 * to duplicate s1. XXX we maybe should double-check whether the match
48 * appears in s1. If it doesn't, then we also have to set the length
49 * to the length of s1, to avoid modifying the argument. It may make
50 * sense to check if maxsize is <= strlen(s1), because in that case we
51 * want to return the unmodified string, too.
52 */
53 if (maxsize == 0) {
54 match = NULL;
55 maxsize = strlen(s1) + 1;
56 }
57 s2 = calloc(maxsize, 1);
58 if (s2 == NULL)
59 err(1, "calloc");
60
61 if (replstr == NULL)
62 replstr = "";
63
64 if (match == NULL || replstr == NULL || maxsize == strlen(s1)) {
65 (void)strlcpy(s2, s1, maxsize);
66 goto done;
67 }
68
69 for (;;) {
70 this = strstr(s1, match);
71 if (this == NULL)
72 break;
73 if ((strlen(s2) + strlen(s1) + strlen(replstr) -
74 strlen(match) + 1) > maxsize) {
75 (void)strlcat(s2, s1, maxsize);
76 goto done;
77 }
78 (void)strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
79 (void)strcat(s2, replstr);
80 s1 = this + strlen(match);
81 }
82 (void)strcat(s2, s1);
83 done:
84 *str = s2;
85 return;
86 }
87
88 #ifdef TEST
89 #include <stdio.h>
90
91 int
main(void)92 main(void)
93 {
94 char *x, *y, *z, *za;
95
96 x = "{}%$";
97 strnsubst(&x, "%$", "{} enpury!", 255);
98 y = x;
99 strnsubst(&y, "}{}", "ybir", 255);
100 z = y;
101 strnsubst(&z, "{", "v ", 255);
102 za = z;
103 strnsubst(&z, NULL, za, 255);
104 if (strcmp(z, "v ybir enpury!") == 0)
105 (void)printf("strnsubst() seems to work!\n");
106 else
107 (void)printf("strnsubst() is broken.\n");
108 (void)printf("%s\n", z);
109 free(x);
110 free(y);
111 free(z);
112 free(za);
113 return 0;
114 }
115 #endif
116