diff options
author | Andrew Bartlett <abartlet@samba.org> | 2011-04-29 13:20:51 +1000 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2011-04-29 16:38:15 +1000 |
commit | 93ace5cc2484b53fc33d4689ccc286defbe2b728 (patch) | |
tree | 8d1ccae212c54bc149fa6772c8ddaf0c0bb1c4c5 /source3/lib | |
parent | 9a9124b08760a6235059f517b9a138337754cd02 (diff) | |
download | samba-93ace5cc2484b53fc33d4689ccc286defbe2b728.tar.gz samba-93ace5cc2484b53fc33d4689ccc286defbe2b728.tar.bz2 samba-93ace5cc2484b53fc33d4689ccc286defbe2b728.zip |
lib/util Re-merge the string_sub() and all_string_sub() from source3
Andrew Bartlett
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/util_str.c | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 554f6a689b..07a058925d 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -508,92 +508,6 @@ bool string_set(char **dest,const char *src) return(string_init(dest,src)); } -/** - Substitute a string for a pattern in another string. Make sure there is - enough room! - - This routine looks for pattern in s and replaces it with - insert. It may do multiple replacements or just one. - - Any of " ; ' $ or ` in the insert string are replaced with _ - if len==0 then the string cannot be extended. This is different from the old - use of len==0 which was for no length checks to be done. -**/ - -void string_sub2(char *s,const char *pattern, const char *insert, size_t len, - bool remove_unsafe_characters, bool replace_once, - bool allow_trailing_dollar) -{ - char *p; - ssize_t ls,lp,li, i; - - if (!insert || !pattern || !*pattern || !s) - return; - - ls = (ssize_t)strlen(s); - lp = (ssize_t)strlen(pattern); - li = (ssize_t)strlen(insert); - - if (len == 0) - len = ls + 1; /* len is number of *bytes* */ - - while (lp <= ls && (p = strstr_m(s,pattern))) { - if (ls + (li-lp) >= len) { - DEBUG(0,("ERROR: string overflow by " - "%d in string_sub(%.50s, %d)\n", - (int)(ls + (li-lp) - len), - pattern, (int)len)); - break; - } - if (li != lp) { - memmove(p+li,p+lp,strlen(p+lp)+1); - } - for (i=0;i<li;i++) { - switch (insert[i]) { - case '$': - /* allow a trailing $ - * (as in machine accounts) */ - if (allow_trailing_dollar && (i == li - 1 )) { - p[i] = insert[i]; - break; - } - case '`': - case '"': - case '\'': - case ';': - case '%': - case '\r': - case '\n': - if ( remove_unsafe_characters ) { - p[i] = '_'; - /* yes this break should be here - * since we want to fall throw if - * not replacing unsafe chars */ - break; - } - default: - p[i] = insert[i]; - } - } - s = p + li; - ls += (li-lp); - - if (replace_once) - break; - } -} - -void string_sub_once(char *s, const char *pattern, - const char *insert, size_t len) -{ - string_sub2( s, pattern, insert, len, true, true, false ); -} - -void string_sub(char *s,const char *pattern, const char *insert, size_t len) -{ - string_sub2( s, pattern, insert, len, true, false, false ); -} - void fstring_sub(char *s,const char *pattern,const char *insert) { string_sub(s, pattern, insert, sizeof(fstring)); @@ -789,48 +703,6 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, true, false, false); } -/** - Similar to string_sub() but allows for any character to be substituted. - Use with caution! - if len==0 then the string cannot be extended. This is different from the old - use of len==0 which was for no length checks to be done. -**/ - -void all_string_sub(char *s,const char *pattern,const char *insert, size_t len) -{ - char *p; - ssize_t ls,lp,li; - - if (!insert || !pattern || !s) - return; - - ls = (ssize_t)strlen(s); - lp = (ssize_t)strlen(pattern); - li = (ssize_t)strlen(insert); - - if (!*pattern) - return; - - if (len == 0) - len = ls + 1; /* len is number of *bytes* */ - - while (lp <= ls && (p = strstr_m(s,pattern))) { - if (ls + (li-lp) >= len) { - DEBUG(0,("ERROR: string overflow by " - "%d in all_string_sub(%.50s, %d)\n", - (int)(ls + (li-lp) - len), - pattern, (int)len)); - break; - } - if (li != lp) { - memmove(p+li,p+lp,strlen(p+lp)+1); - } - memcpy(p, insert, li); - s = p + li; - ls += (li-lp); - } -} - char *talloc_all_string_sub(TALLOC_CTX *ctx, const char *src, const char *pattern, |