strgsub(3) substitute all occurrences of pattern with another string


#include <publib.h>
int strgsub(char *str, const char *pat, const char *sub, size_t max);


strgsub finds all occurrences of the pattern pat in the string str (using a method similar to strstr(3) to find the occurrences, i.e., no regular expressions), and replaces each with sub. If pat does not occur in str, no substitution is made. The size (including the terminating '\0') of the string after the substitutions may be at most max chars. If it would be larger, no substitutions are made.

Of course, if sub is an empty string, the occurrences of the pattern are deleted from the string.


strgsub returns the number of substitutions made, or -1 if the result would not have fit into max chars.


To substitute every "foo" with "bar" in a line, one might do the following.

strgsub(line, "foo", "bar", strlen(line)+1);


Lars Wirzenius ([email protected])