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