1fc17b349SJuli Mallett /* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */
2fc17b349SJuli Mallett
3fc17b349SJuli Mallett /*
4fc17b349SJuli Mallett * Copyright (c) 2002 J. Mallett. All rights reserved.
5fc17b349SJuli Mallett * You may do whatever you want with this file as long as
6fc17b349SJuli Mallett * the above copyright and this notice remain intact, along
7fc17b349SJuli Mallett * with the following statement:
8fc17b349SJuli Mallett * For the man who taught me vi, and who got too old, too young.
9fc17b349SJuli Mallett */
10fc17b349SJuli Mallett
11fc17b349SJuli Mallett #include <sys/cdefs.h>
12fc17b349SJuli Mallett #include <err.h>
13f058359bSTom Jones #include <stdbool.h>
14fc17b349SJuli Mallett #include <stdlib.h>
15fc17b349SJuli Mallett #include <string.h>
16fc17b349SJuli Mallett #include <unistd.h>
17fc17b349SJuli Mallett
18f058359bSTom Jones bool strnsubst(char **, const char *, const char *, size_t);
19fc17b349SJuli Mallett
20fc17b349SJuli Mallett /*
21fc17b349SJuli Mallett * Replaces str with a string consisting of str with match replaced with
22fc17b349SJuli Mallett * replstr as many times as can be done before the constructed string is
23fc17b349SJuli Mallett * maxsize bytes large. It does not free the string pointed to by str, it
24fc17b349SJuli Mallett * is up to the calling program to be sure that the original contents of
25fc17b349SJuli Mallett * str as well as the new contents are handled in an appropriate manner.
26986d829bSJuli Mallett * If replstr is NULL, then that internally is changed to a nil-string, so
27986d829bSJuli Mallett * that we can still pretend to do somewhat meaningful substitution.
28f058359bSTom Jones *
29*1e692b93STom Jones * Returns true if truncation was needed to do the replacement, false if
30f058359bSTom Jones * truncation was not required.
31fc17b349SJuli Mallett */
32f058359bSTom Jones bool
strnsubst(char ** str,const char * match,const char * replstr,size_t maxsize)33fc17b349SJuli Mallett strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
34fc17b349SJuli Mallett {
35299ea75aSJuli Mallett char *s1, *s2, *this;
36f058359bSTom Jones bool error = false;
37fc17b349SJuli Mallett
38fc17b349SJuli Mallett s1 = *str;
39fc17b349SJuli Mallett if (s1 == NULL)
40f058359bSTom Jones return false;
4138e62c69SJuli Mallett /*
42dae3a64fSEitan Adler * If maxsize is 0 then set it to the length of s1, because we have
4338e62c69SJuli Mallett * to duplicate s1. XXX we maybe should double-check whether the match
4438e62c69SJuli Mallett * appears in s1. If it doesn't, then we also have to set the length
4538e62c69SJuli Mallett * to the length of s1, to avoid modifying the argument. It may make
4638e62c69SJuli Mallett * sense to check if maxsize is <= strlen(s1), because in that case we
4738e62c69SJuli Mallett * want to return the unmodified string, too.
4838e62c69SJuli Mallett */
4938e62c69SJuli Mallett if (maxsize == 0) {
5038e62c69SJuli Mallett match = NULL;
5138e62c69SJuli Mallett maxsize = strlen(s1) + 1;
5238e62c69SJuli Mallett }
53df4110b6SXin LI s2 = calloc(1, maxsize);
54fc17b349SJuli Mallett if (s2 == NULL)
55fc17b349SJuli Mallett err(1, "calloc");
56fc17b349SJuli Mallett
57986d829bSJuli Mallett if (replstr == NULL)
58986d829bSJuli Mallett replstr = "";
59986d829bSJuli Mallett
60299ea75aSJuli Mallett if (match == NULL || replstr == NULL || maxsize == strlen(s1)) {
610fa5e8dcSJuli Mallett strlcpy(s2, s1, maxsize);
620fa5e8dcSJuli Mallett goto done;
630fa5e8dcSJuli Mallett }
640fa5e8dcSJuli Mallett
65fc17b349SJuli Mallett for (;;) {
66fc17b349SJuli Mallett this = strstr(s1, match);
67fc17b349SJuli Mallett if (this == NULL)
68fc17b349SJuli Mallett break;
695578bd8cSColin Percival if ((strlen(s2) + strlen(s1) + strlen(replstr) -
705578bd8cSColin Percival strlen(match) + 1) > maxsize) {
71fc17b349SJuli Mallett strlcat(s2, s1, maxsize);
72f058359bSTom Jones error = true;
73fc17b349SJuli Mallett goto done;
74fc17b349SJuli Mallett }
75fc17b349SJuli Mallett strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
76fc17b349SJuli Mallett strcat(s2, replstr);
77fc17b349SJuli Mallett s1 = this + strlen(match);
78fc17b349SJuli Mallett }
79fc17b349SJuli Mallett strcat(s2, s1);
80fc17b349SJuli Mallett done:
81fc17b349SJuli Mallett *str = s2;
82f058359bSTom Jones return error;
83fc17b349SJuli Mallett }
84fc17b349SJuli Mallett
85fc17b349SJuli Mallett #ifdef TEST
86fc17b349SJuli Mallett #include <stdio.h>
87fc17b349SJuli Mallett
88fc17b349SJuli Mallett int
main(void)89fc17b349SJuli Mallett main(void)
90fc17b349SJuli Mallett {
915058dcb4SJuli Mallett char *x, *y, *z, *za;
92fc17b349SJuli Mallett
935058dcb4SJuli Mallett x = "{}%$";
945058dcb4SJuli Mallett strnsubst(&x, "%$", "{} enpury!", 255);
955058dcb4SJuli Mallett y = x;
965058dcb4SJuli Mallett strnsubst(&y, "}{}", "ybir", 255);
975058dcb4SJuli Mallett z = y;
985058dcb4SJuli Mallett strnsubst(&z, "{", "v ", 255);
995058dcb4SJuli Mallett za = z;
1005058dcb4SJuli Mallett strnsubst(&z, NULL, za, 255);
1015058dcb4SJuli Mallett if (strcmp(z, "v ybir enpury!") == 0)
1025058dcb4SJuli Mallett printf("strnsubst() seems to work!\n");
1035058dcb4SJuli Mallett else
1045058dcb4SJuli Mallett printf("strnsubst() is broken.\n");
1055058dcb4SJuli Mallett printf("%s\n", z);
106fc17b349SJuli Mallett free(x);
1075058dcb4SJuli Mallett free(y);
1085058dcb4SJuli Mallett free(z);
1095058dcb4SJuli Mallett free(za);
110fc17b349SJuli Mallett return 0;
111fc17b349SJuli Mallett }
112fc17b349SJuli Mallett #endif
113