diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2014-02-27 11:17:06 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-02-27 11:17:06 +0100 |
commit | 6f068904dc142657bb596f91196f9113f1838cbe (patch) | |
tree | ac02741b3c8b74bf3a078aeb13ef7ff1c5e1e23f /libbb | |
parent | 6885e49ba596239a0b0d3631fd72fc2692fbb65c (diff) | |
download | busybox-w32-6f068904dc142657bb596f91196f9113f1838cbe.tar.gz busybox-w32-6f068904dc142657bb596f91196f9113f1838cbe.tar.bz2 busybox-w32-6f068904dc142657bb596f91196f9113f1838cbe.zip |
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.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libbb/replace.c b/libbb/replace.c new file mode 100644 index 000000000..8711f957d --- /dev/null +++ b/libbb/replace.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
8 | */ | ||
9 | |||
10 | //kbuild:lib-y += replace.o | ||
11 | |||
12 | #include "libbb.h" | ||
13 | |||
14 | unsigned FAST_FUNC count_strstr(const char *str, const char *sub) | ||
15 | { | ||
16 | size_t sub_len = strlen(sub); | ||
17 | unsigned count = 0; | ||
18 | |||
19 | while ((str = strstr(str, sub)) != NULL) { | ||
20 | count++; | ||
21 | str += sub_len; | ||
22 | } | ||
23 | return count; | ||
24 | } | ||
25 | |||
26 | char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl) | ||
27 | { | ||
28 | char *buf, *dst, *end; | ||
29 | size_t sub_len = strlen(sub); | ||
30 | size_t repl_len = strlen(repl); | ||
31 | |||
32 | //dbg_msg("subst(s:'%s',count:%d,sub:'%s',repl:'%s'", src, count, sub, repl); | ||
33 | |||
34 | buf = dst = xmalloc(strlen(src) + count * ((int)repl_len - (int)sub_len) + 1); | ||
35 | /* we replace each sub with repl */ | ||
36 | while ((end = strstr(src, sub)) != NULL) { | ||
37 | dst = mempcpy(dst, src, end - src); | ||
38 | dst = mempcpy(dst, repl, repl_len); | ||
39 | /*src = end + 1; - GNU findutils 4.5.10 doesn't do this... */ | ||
40 | src = end + sub_len; /* but this. Try "xargs -Iaa echo aaa" */ | ||
41 | } | ||
42 | strcpy(dst, src); | ||
43 | //dbg_msg("subst9:'%s'", buf); | ||
44 | return buf; | ||
45 | } | ||