2002-04-19 23:28:54 +00:00
|
|
|
/* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 J. Mallett. All rights reserved.
|
|
|
|
* You may do whatever you want with this file as long as
|
|
|
|
* the above copyright and this notice remain intact, along
|
|
|
|
* with the following statement:
|
|
|
|
* For the man who taught me vi, and who got too old, too young.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void strnsubst(char **, const char *, const char *, size_t);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replaces str with a string consisting of str with match replaced with
|
|
|
|
* replstr as many times as can be done before the constructed string is
|
|
|
|
* maxsize bytes large. It does not free the string pointed to by str, it
|
|
|
|
* is up to the calling program to be sure that the original contents of
|
|
|
|
* str as well as the new contents are handled in an appropriate manner.
|
2002-05-30 19:24:08 +00:00
|
|
|
* If replstr is NULL, then that internally is changed to a nil-string, so
|
|
|
|
* that we can still pretend to do somewhat meaningful substitution.
|
2002-04-19 23:28:54 +00:00
|
|
|
* No value is returned.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
|
|
|
|
{
|
2002-05-02 02:15:06 +00:00
|
|
|
char *s1, *s2, *this;
|
2002-04-19 23:28:54 +00:00
|
|
|
|
|
|
|
s1 = *str;
|
|
|
|
if (s1 == NULL)
|
|
|
|
return;
|
2005-12-30 23:22:50 +00:00
|
|
|
/*
|
2017-12-27 03:23:01 +00:00
|
|
|
* If maxsize is 0 then set it to the length of s1, because we have
|
2005-12-30 23:22:50 +00:00
|
|
|
* to duplicate s1. XXX we maybe should double-check whether the match
|
|
|
|
* appears in s1. If it doesn't, then we also have to set the length
|
|
|
|
* to the length of s1, to avoid modifying the argument. It may make
|
|
|
|
* sense to check if maxsize is <= strlen(s1), because in that case we
|
|
|
|
* want to return the unmodified string, too.
|
|
|
|
*/
|
|
|
|
if (maxsize == 0) {
|
|
|
|
match = NULL;
|
|
|
|
maxsize = strlen(s1) + 1;
|
|
|
|
}
|
2013-01-05 00:23:58 +00:00
|
|
|
s2 = calloc(1, maxsize);
|
2002-04-19 23:28:54 +00:00
|
|
|
if (s2 == NULL)
|
|
|
|
err(1, "calloc");
|
|
|
|
|
2002-05-30 19:24:08 +00:00
|
|
|
if (replstr == NULL)
|
|
|
|
replstr = "";
|
|
|
|
|
2002-05-02 02:15:06 +00:00
|
|
|
if (match == NULL || replstr == NULL || maxsize == strlen(s1)) {
|
2002-05-02 02:06:03 +00:00
|
|
|
strlcpy(s2, s1, maxsize);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-04-19 23:28:54 +00:00
|
|
|
for (;;) {
|
|
|
|
this = strstr(s1, match);
|
|
|
|
if (this == NULL)
|
|
|
|
break;
|
2004-10-18 15:40:47 +00:00
|
|
|
if ((strlen(s2) + strlen(s1) + strlen(replstr) -
|
|
|
|
strlen(match) + 1) > maxsize) {
|
2002-04-19 23:28:54 +00:00
|
|
|
strlcat(s2, s1, maxsize);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
|
|
|
|
strcat(s2, replstr);
|
|
|
|
s1 = this + strlen(match);
|
|
|
|
}
|
|
|
|
strcat(s2, s1);
|
|
|
|
done:
|
|
|
|
*str = s2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2002-06-22 12:58:42 +00:00
|
|
|
char *x, *y, *z, *za;
|
2002-04-19 23:28:54 +00:00
|
|
|
|
2002-06-22 12:58:42 +00:00
|
|
|
x = "{}%$";
|
|
|
|
strnsubst(&x, "%$", "{} enpury!", 255);
|
|
|
|
y = x;
|
|
|
|
strnsubst(&y, "}{}", "ybir", 255);
|
|
|
|
z = y;
|
|
|
|
strnsubst(&z, "{", "v ", 255);
|
|
|
|
za = z;
|
|
|
|
strnsubst(&z, NULL, za, 255);
|
|
|
|
if (strcmp(z, "v ybir enpury!") == 0)
|
|
|
|
printf("strnsubst() seems to work!\n");
|
|
|
|
else
|
|
|
|
printf("strnsubst() is broken.\n");
|
|
|
|
printf("%s\n", z);
|
2002-04-19 23:28:54 +00:00
|
|
|
free(x);
|
2002-06-22 12:58:42 +00:00
|
|
|
free(y);
|
|
|
|
free(z);
|
|
|
|
free(za);
|
2002-04-19 23:28:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|