This repository has been archived on 2025-02-12. You can view files and clone it, but cannot push or open issues or pull requests.
NeoStats-services/new.c
2002-02-27 12:21:26 +00:00

33 lines
870 B
C

/*************************************************************************/
/* strnrepl: Replace occurrences of `old' with `new' in string `s'. Stop
* replacing if a replacement would cause the string to exceed
* `size' bytes (including the null terminator). Return the
* string.
*/
char *strnrepl(char *s, int32 size, const char *old, const char *new)
{
char *ptr = s;
int32 left = strlen(s);
int32 avail = size - (left+1);
int32 oldlen = strlen(old);
int32 newlen = strlen(new);
int32 diff = newlen - oldlen;
while (left >= oldlen) {
if (strncmp(ptr, old, oldlen) != 0) {
left--;
ptr++;
continue;
}
if (diff > avail)
break;
if (diff != 0)
memmove(ptr+oldlen+diff, ptr+oldlen, left+1);
strncpy(ptr, new, newlen);
ptr += newlen;
left -= oldlen;
}
return s;
}