summaryrefslogtreecommitdiffstats
path: root/source/lib/util_str.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/lib/util_str.c')
-rw-r--r--source/lib/util_str.c64
1 files changed, 44 insertions, 20 deletions
diff --git a/source/lib/util_str.c b/source/lib/util_str.c
index f1078c6383a..6d429e37196 100644
--- a/source/lib/util_str.c
+++ b/source/lib/util_str.c
@@ -1254,22 +1254,27 @@ char *realloc_string_sub(char *string, const char *pattern,
return string;
}
-/* Same as string_sub, but returns a talloc'ed string */
+/*
+ * Internal guts of talloc_string_sub and talloc_all_string_sub.
+ * 'filter' differentiates between them.
+ */
-char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
- const char *pattern, const char *insert)
+static char *talloc_string_sub_internal(TALLOC_CTX *mem_ctx, const char *src,
+ const char *pattern, const char *insert, bool filter)
{
char *p, *in;
char *s;
char *string;
ssize_t ls,lp,li,ld, i;
- if (!insert || !pattern || !*pattern || !src || !*src)
+ if (!insert || !pattern || !*pattern || !src || !*src) {
return NULL;
+ }
string = talloc_strdup(mem_ctx, src);
if (string == NULL) {
- DEBUG(0, ("talloc_strdup failed\n"));
+ DEBUG(0, ("talloc_string_sub_internal: "
+ "talloc_strdup failed\n"));
return NULL;
}
@@ -1277,27 +1282,30 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
in = SMB_STRDUP(insert);
if (!in) {
- DEBUG(0, ("talloc_string_sub: out of memory!\n"));
+ DEBUG(0, ("talloc_string_sub_internal: ENOMEM\n"));
return NULL;
}
ls = (ssize_t)strlen(s);
lp = (ssize_t)strlen(pattern);
li = (ssize_t)strlen(insert);
ld = li - lp;
- for (i=0;i<li;i++) {
- switch (in[i]) {
- case '`':
- case '"':
- case '\'':
- case ';':
- case '$':
- case '%':
- case '\r':
- case '\n':
- in[i] = '_';
- default:
- /* ok */
- break;
+
+ if (filter) {
+ for (i=0;i<li;i++) {
+ switch (in[i]) {
+ case '`':
+ case '"':
+ case '\'':
+ case ';':
+ case '$':
+ case '%':
+ case '\r':
+ case '\n':
+ in[i] = '_';
+ default:
+ /* ok */
+ break;
+ }
}
}
@@ -1325,6 +1333,14 @@ char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
return string;
}
+/* Same as string_sub, but returns a talloc'ed string */
+
+char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
+ const char *pattern, const char *insert)
+{
+ return talloc_string_sub_internal(mem_ctx, src, pattern, insert, true);
+}
+
/**
Similar to string_sub() but allows for any character to be substituted.
Use with caution!
@@ -1367,6 +1383,14 @@ void all_string_sub(char *s,const char *pattern,const char *insert, size_t len)
}
}
+char *talloc_all_string_sub(TALLOC_CTX *ctx,
+ const char *src,
+ const char *pattern,
+ const char *insert)
+{
+ return talloc_string_sub_internal(ctx, src, pattern, insert, false);
+}
+
/**
Similar to all_string_sub but for unicode strings.
Return a new allocated unicode string.