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