summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko2014-02-27 11:17:06 +0100
committerDenys Vlasenko2014-02-27 11:17:06 +0100
commit6f068904dc142657bb596f91196f9113f1838cbe (patch)
treeac02741b3c8b74bf3a078aeb13ef7ff1c5e1e23f /libbb
parent6885e49ba596239a0b0d3631fd72fc2692fbb65c (diff)
downloadbusybox-6f068904dc142657bb596f91196f9113f1838cbe.zip
busybox-6f068904dc142657bb596f91196f9113f1838cbe.tar.gz
xargs: add support for -I and -i. Closes 493
function old new delta process_stdin_with_replace - 195 +195 xmalloc_substitute_string - 145 +145 xargs_main 808 884 +76 count_strstr - 45 +45 packed_usage 29580 29571 -9 parse_params 1445 1416 -29 func_exec 285 138 -147 ------------------------------------------------------------------------------ (add/remove: 4/0 grow/shrink: 1/3 up/down: 461/-185) Total: 276 bytes text data bss dec hex filename 922156 932 17692 940780 e5aec busybox_old 922440 932 17692 941064 e5c08 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/replace.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/libbb/replace.c b/libbb/replace.c
new file mode 100644
index 0000000..8711f95
--- /dev/null
+++ b/libbb/replace.c
@@ -0,0 +1,45 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//kbuild:lib-y += replace.o
+
+#include "libbb.h"
+
+unsigned FAST_FUNC count_strstr(const char *str, const char *sub)
+{
+ size_t sub_len = strlen(sub);
+ unsigned count = 0;
+
+ while ((str = strstr(str, sub)) != NULL) {
+ count++;
+ str += sub_len;
+ }
+ return count;
+}
+
+char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl)
+{
+ char *buf, *dst, *end;
+ size_t sub_len = strlen(sub);
+ size_t repl_len = strlen(repl);
+
+ //dbg_msg("subst(s:'%s',count:%d,sub:'%s',repl:'%s'", src, count, sub, repl);
+
+ buf = dst = xmalloc(strlen(src) + count * ((int)repl_len - (int)sub_len) + 1);
+ /* we replace each sub with repl */
+ while ((end = strstr(src, sub)) != NULL) {
+ dst = mempcpy(dst, src, end - src);
+ dst = mempcpy(dst, repl, repl_len);
+ /*src = end + 1; - GNU findutils 4.5.10 doesn't do this... */
+ src = end + sub_len; /* but this. Try "xargs -Iaa echo aaa" */
+ }
+ strcpy(dst, src);
+ //dbg_msg("subst9:'%s'", buf);
+ return buf;
+}