/*************************************************************************/ /* 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; }