From fa6ab56353e84057bf1b2f740bc1e1a99af686ce Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 21 Jan 2014 13:44:21 +0100 Subject: libbb: if opening /dev/loopN returns ENXIO, don't try N++. function old new delta set_loop 639 635 -4 Signed-off-by: Denys Vlasenko --- libbb/loop.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'libbb') diff --git a/libbb/loop.c b/libbb/loop.c index 823fba079..c96c5e070 100644 --- a/libbb/loop.c +++ b/libbb/loop.c @@ -94,19 +94,19 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse /* Open the file. Barf if this doesn't work. */ mode = ro ? O_RDONLY : O_RDWR; + open_ffd: ffd = open(file, mode); if (ffd < 0) { if (mode != O_RDONLY) { mode = O_RDONLY; - ffd = open(file, mode); + goto open_ffd; } - if (ffd < 0) - return -errno; + return -errno; } /* Find a loop device. */ try = *device ? *device : dev; - /* 1048575 is a max possible minor number in Linux circa 2010 */ + /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */ for (i = 0; rc && i < 1048576; i++) { sprintf(dev, LOOP_FORMAT, i); @@ -121,7 +121,7 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse goto try_to_open; } /* Ran out of block devices, return failure. */ - rc = -ENOENT; + rc = -1; break; } try_to_open: @@ -131,8 +131,14 @@ int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offse mode = O_RDONLY; dfd = open(try, mode); } - if (dfd < 0) + if (dfd < 0) { + if (errno == ENXIO) { + /* Happens if loop module is not loaded */ + rc = -1; + break; + } goto try_again; + } rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo); -- cgit v1.2.3-55-g6feb From f1999b5a9d2788cdc120b1ee2ab1de18e95b38f2 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Fri, 31 Jan 2014 00:29:47 -0500 Subject: appletlib: fix set-but-unused warning When you build with FEATURE_INSTALLER disabled, you get a build warning like so: libbb/appletlib.c: In function 'busybox_main': libbb/appletlib.c:691:7: warning: variable 'use_symbolic_links' set but not used [-Wunused-but-set-variable] int use_symbolic_links; ^ Signed-off-by: Mike Frysinger --- libbb/appletlib.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 8f3a8a10b..f7c416ece 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -606,7 +606,11 @@ static void install_links(const char *busybox, int use_symbolic_links, } } # else -# define install_links(x,y,z) ((void)0) +static void install_links(const char *busybox UNUSED_PARAM, + int use_symbolic_links UNUSED_PARAM, + char *custom_install_dir UNUSED_PARAM) +{ +} # endif /* If we were called as "busybox..." */ -- cgit v1.2.3-55-g6feb From 8ed96726603a59969b99e4ea30dbd9b06955084b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 9 Feb 2014 14:38:03 +0100 Subject: libbb: don't die if crypt() returns NULL Signed-off-by: Denys Vlasenko --- libbb/pw_encrypt.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c index 39ffa084f..bfc7030a8 100644 --- a/libbb/pw_encrypt.c +++ b/libbb/pw_encrypt.c @@ -142,7 +142,14 @@ char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup) char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup) { - return xstrdup(crypt(clear, salt)); + char *s; + + s = crypt(clear, salt); + /* + * glibc used to return "" on malformed salts (for example, ""), + * but since 2.17 it returns NULL. + */ + return xstrdup(s ? s : ""); } #endif -- cgit v1.2.3-55-g6feb From 12916b922065f452a5b6c043200ac0863853c7a3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 25 Feb 2014 15:09:01 +0100 Subject: libbb: trivial code shrink function old new delta reset_ino_dev_hashtable 84 74 -10 Signed-off-by: Denys Vlasenko --- libbb/inode_hash.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'libbb') diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c index 715535ef5..2f02d0746 100644 --- a/libbb/inode_hash.c +++ b/libbb/inode_hash.c @@ -72,13 +72,18 @@ void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char * void FAST_FUNC reset_ino_dev_hashtable(void) { int i; - ino_dev_hashtable_bucket_t *bucket; + ino_dev_hashtable_bucket_t *bucket, *next; + + if (!ino_dev_hashtable) + return; + + for (i = 0; i < HASH_SIZE; i++) { + bucket = ino_dev_hashtable[i]; - for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) { - while (ino_dev_hashtable[i] != NULL) { - bucket = ino_dev_hashtable[i]->next; - free(ino_dev_hashtable[i]); - ino_dev_hashtable[i] = bucket; + while (bucket != NULL) { + next = bucket->next; + free(bucket); + bucket = next; } } free(ino_dev_hashtable); -- cgit v1.2.3-55-g6feb From 83bc4332e7d6d74293c1c41e047d2681a8350e1a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 25 Feb 2014 15:27:58 +0100 Subject: du, copy_file: fix file matching on cramfs. Closes 5456 function old new delta is_in_ino_dev_hashtable 88 108 +20 add_to_ino_dev_hashtable 150 142 -8 reset_ino_dev_hashtable 84 75 -9 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/2 up/down: 20/-17) Total: 3 bytes Signed-off-by: Denys Vlasenko --- libbb/inode_hash.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c index 2f02d0746..f11c2afb2 100644 --- a/libbb/inode_hash.c +++ b/libbb/inode_hash.c @@ -11,14 +11,23 @@ #include "libbb.h" typedef struct ino_dev_hash_bucket_struct { - struct ino_dev_hash_bucket_struct *next; ino_t ino; dev_t dev; + /* + * Above fields can be 64-bit, while pointer may be 32-bit. + * Putting "next" field here may reduce size of this struct: + */ + struct ino_dev_hash_bucket_struct *next; + /* + * Reportedly, on cramfs a file and a dir can have same ino. + * Need to also remember "file/dir" bit: + */ + char isdir; /* bool */ char name[1]; } ino_dev_hashtable_bucket_t; -#define HASH_SIZE 311 /* Should be prime */ -#define hash_inode(i) ((i) % HASH_SIZE) +#define HASH_SIZE 311u /* Should be prime */ +#define hash_inode(i) ((unsigned)(i) % HASH_SIZE) /* array of [HASH_SIZE] elements */ static ino_dev_hashtable_bucket_t **ino_dev_hashtable; @@ -38,6 +47,7 @@ char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf) while (bucket != NULL) { if ((bucket->ino == statbuf->st_ino) && (bucket->dev == statbuf->st_dev) + && (bucket->isdir == !!S_ISDIR(statbuf->st_mode)) ) { return bucket->name; } @@ -52,17 +62,18 @@ void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char * int i; ino_dev_hashtable_bucket_t *bucket; - i = hash_inode(statbuf->st_ino); if (!name) name = ""; bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name)); bucket->ino = statbuf->st_ino; bucket->dev = statbuf->st_dev; + bucket->isdir = !!S_ISDIR(statbuf->st_mode); strcpy(bucket->name, name); if (!ino_dev_hashtable) ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable)); + i = hash_inode(statbuf->st_ino); bucket->next = ino_dev_hashtable[i]; ino_dev_hashtable[i] = bucket; } -- cgit v1.2.3-55-g6feb From 6f068904dc142657bb596f91196f9113f1838cbe Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 27 Feb 2014 11:17:06 +0100 Subject: 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 --- findutils/find.c | 32 +----------- findutils/xargs.c | 153 ++++++++++++++++++++++++++++++++++++++++++++---------- include/libbb.h | 2 + libbb/replace.c | 45 ++++++++++++++++ 4 files changed, 176 insertions(+), 56 deletions(-) create mode 100644 libbb/replace.c (limited to 'libbb') diff --git a/findutils/find.c b/findutils/find.c index 044f010b0..6d34f4d68 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -402,34 +402,6 @@ struct globals { G.recurse_flags = ACTION_RECURSE; \ } while (0) -#if ENABLE_FEATURE_FIND_EXEC -static unsigned count_subst(const char *str) -{ - unsigned count = 0; - while ((str = strstr(str, "{}")) != NULL) { - count++; - str++; - } - return count; -} - - -static char* subst(const char *src, unsigned count, const char* filename) -{ - char *buf, *dst, *end; - size_t flen = strlen(filename); - /* we replace each '{}' with filename: growth by strlen-2 */ - buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); - while ((end = strstr(src, "{}")) != NULL) { - dst = mempcpy(dst, src, end - src); - dst = mempcpy(dst, filename, flen); - src = end + 2; - } - strcpy(dst, src); - return buf; -} -#endif - /* Return values of ACTFs ('action functions') are a bit mask: * bit 1=1: prune (use SKIP constant for setting it) * bit 0=1: matched successfully (TRUE) @@ -613,7 +585,7 @@ ACTF(exec) char *argv[ap->exec_argc + 1]; #endif for (i = 0; i < ap->exec_argc; i++) - argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); + argv[i] = xmalloc_substitute_string(ap->exec_argv[i], ap->subst_count[i], "{}", fileName); argv[i] = NULL; /* terminate the list */ rc = spawn_and_wait(argv); @@ -1091,7 +1063,7 @@ static action*** parse_params(char **argv) ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); i = ap->exec_argc; while (i--) - ap->subst_count[i] = count_subst(ap->exec_argv[i]); + ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}"); } #endif #if ENABLE_FEATURE_FIND_PAREN diff --git a/findutils/xargs.c b/findutils/xargs.c index 0d1bb43fc..ed6dbd33e 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -53,6 +53,13 @@ //config: Support -0: input items are terminated by a NUL character //config: instead of whitespace, and the quotes and backslash //config: are not special. +//config: +//config:config FEATURE_XARGS_SUPPORT_REPL_STR +//config: bool "Enable -I STR: string to replace" +//config: default y +//config: depends on XARGS +//config: help +//config: Support -I STR and -i[STR] options. //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs)) @@ -85,19 +92,22 @@ struct globals { char **args; +#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR + char **argv; + const char *repl_str; + char eol_ch; +#endif const char *eof_str; int idx; } FIX_ALIASING; #define G (*(struct globals*)&bb_common_bufsiz1) #define INIT_G() do { \ G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \ + IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \ + IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \ } while (0) -/* - * This function has special algorithm. - * Don't use fork and include to main! - */ static int xargs_exec(void) { int status; @@ -301,7 +311,7 @@ static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf) c = '\0'; } *p++ = c; - if (c == '\0') { /* word's delimiter or EOF detected */ + if (c == '\0') { /* NUL or EOF detected */ /* A full word is loaded */ store_param(s); dbg_msg("args[]:'%s'", s); @@ -323,10 +333,71 @@ static char* FAST_FUNC process0_stdin(int n_max_chars, int n_max_arg, char *buf) } #endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */ +#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR +/* + * Used if -I was specified. + * In this mode, words aren't appended to PROG ARGS. + * Instead, entire input line is read, then string + * in every PROG and ARG is replaced with the line: + * echo -e "ho ho\nhi" | xargs -I_ cmd __ _ + * results in "cmd 'ho hoho ho' 'ho ho'"; "cmd 'hihi' 'hi'". + * -n MAX_ARGS seems to be ignored. + * Tested with GNU findutils 4.5.10. + */ +//FIXME: n_max_chars is not handled the same way as in GNU findutils. +//FIXME: quoting is not implemented. +static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg UNUSED_PARAM, char *buf) +{ + int i; + char *end, *p; + + /* Free strings from last invocation, if any */ + for (i = 0; G.args && G.args[i]; i++) + if (G.args[i] != G.argv[i]) + free(G.args[i]); + + end = buf + n_max_chars; + p = buf; + + while (1) { + int c = getchar(); + if (c == EOF || c == G.eol_ch) { + if (p == buf) + goto ret; /* empty line */ + c = '\0'; + } + *p++ = c; + if (c == '\0') { /* EOL or EOF detected */ + i = 0; + while (G.argv[i]) { + char *arg = G.argv[i]; + int count = count_strstr(arg, G.repl_str); + if (count != 0) + arg = xmalloc_substitute_string(arg, count, G.repl_str, s); + store_param(arg); + dbg_msg("args[]:'%s'", arg); + i++; + } + p = buf; + goto ret; + } + if (p == end) { + goto ret; + } + } + ret: + *p = '\0'; + /* store_param(NULL) - caller will do it */ + dbg_msg("return:'%s'", buf); + return buf; +} +#endif + #if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION /* Prompt the user for a response, and - if the user responds affirmatively, return true; - otherwise, return false. Uses "/dev/tty", not stdin. */ + * if user responds affirmatively, return true; + * otherwise, return false. Uses "/dev/tty", not stdin. + */ static int xargs_ask_confirmation(void) { FILE *tty_stream; @@ -360,6 +431,9 @@ static int xargs_ask_confirmation(void) //usage: "\n -e[STR] STR stops input processing" //usage: "\n -n N Pass no more than N args to PROG" //usage: "\n -s N Pass command line of no more than N bytes" +//usage: IF_FEATURE_XARGS_SUPPORT_REPL_STR( +//usage: "\n -I STR Replace STR within PROG ARGS with input line" +//usage: ) //usage: IF_FEATURE_XARGS_SUPPORT_TERMOPT( //usage: "\n -x Exit if size is exceeded" //usage: ) @@ -378,6 +452,8 @@ enum { IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) IF_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) + IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR ,) + IF_FEATURE_XARGS_SUPPORT_REPL_STR( OPTBIT_REPLSTR1 ,) OPT_VERBOSE = 1 << OPTBIT_VERBOSE , OPT_NO_EMPTY = 1 << OPTBIT_NO_EMPTY , @@ -388,11 +464,14 @@ enum { OPT_INTERACTIVE = IF_FEATURE_XARGS_SUPPORT_CONFIRMATION((1 << OPTBIT_INTERACTIVE)) + 0, OPT_TERMINATE = IF_FEATURE_XARGS_SUPPORT_TERMOPT( (1 << OPTBIT_TERMINATE )) + 0, OPT_ZEROTERM = IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1 << OPTBIT_ZEROTERM )) + 0, + OPT_REPLSTR = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR )) + 0, + OPT_REPLSTR1 = IF_FEATURE_XARGS_SUPPORT_REPL_STR( (1 << OPTBIT_REPLSTR1 )) + 0, }; #define OPTION_STR "+trn:s:e::E:" \ IF_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ IF_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ - IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") + IF_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") \ + IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int xargs_main(int argc, char **argv) @@ -405,7 +484,8 @@ int xargs_main(int argc, char **argv) unsigned opt; int n_max_chars; int n_max_arg; -#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM +#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM \ + || ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR char* FAST_FUNC (*read_args)(int, int, char*) = process_stdin; #else #define read_args process_stdin @@ -419,7 +499,10 @@ int xargs_main(int argc, char **argv) "no-run-if-empty\0" No_argument "r" ; #endif - opt = getopt32(argv, OPTION_STR, &max_args, &max_chars, &G.eof_str, &G.eof_str); + opt = getopt32(argv, OPTION_STR, + &max_args, &max_chars, &G.eof_str, &G.eof_str + IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str) + ); /* -E ""? You may wonder why not just omit -E? * This is used for portability: @@ -427,8 +510,10 @@ int xargs_main(int argc, char **argv) if ((opt & OPT_EOF_STRING1) && G.eof_str[0] == '\0') G.eof_str = NULL; - if (opt & OPT_ZEROTERM) - IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); + if (opt & OPT_ZEROTERM) { + IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin;) + IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\0';) + } argv += optind; argc -= optind; @@ -486,20 +571,36 @@ int xargs_main(int argc, char **argv) /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */ } - /* Allocate pointers for execvp */ - /* We can statically allocate (argc + n_max_arg + 1) elements - * and do not bother with resizing args[], but on 64-bit machines - * this results in args[] vector which is ~8 times bigger - * than n_max_chars! That is, with n_max_chars == 20k, - * args[] will take 160k (!), which will most likely be - * almost entirely unused. - */ - /* See store_param() for matching 256-step growth logic */ - G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff)); - - /* Store the command to be executed, part 1 */ - for (i = 0; argv[i]; i++) - G.args[i] = argv[i]; +#if ENABLE_FEATURE_XARGS_SUPPORT_REPL_STR + if (opt & (OPT_REPLSTR | OPT_REPLSTR1)) { + /* + * -I: + * Unmodified args are kept in G.argv[i], + * G.args[i] receives malloced G.argv[i] with replaced + * with input line. Setting this up: + */ + G.args = NULL; + G.argv = argv; + argc = 0; + read_args = process_stdin_with_replace; + } else +#endif + { + /* Allocate pointers for execvp. + * We can statically allocate (argc + n_max_arg + 1) elements + * and do not bother with resizing args[], but on 64-bit machines + * this results in args[] vector which is ~8 times bigger + * than n_max_chars! That is, with n_max_chars == 20k, + * args[] will take 160k (!), which will most likely be + * almost entirely unused. + * + * See store_param() for matching 256-step growth logic + */ + G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff)); + /* Store the command to be executed, part 1 */ + for (i = 0; argv[i]; i++) + G.args[i] = argv[i]; + } while (1) { char *rem; diff --git a/include/libbb.h b/include/libbb.h index 96f33340e..1cbe2c8b4 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -650,6 +650,8 @@ char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC; void overlapping_strcpy(char *dst, const char *src) FAST_FUNC; char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC; char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC; +unsigned count_strstr(const char *str, const char *sub) FAST_FUNC; +char *xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl) FAST_FUNC; /* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc. * But potentially slow, don't use in one-billion-times loops */ int bb_putchar(int ch) FAST_FUNC; 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 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999-2004 by Erik Andersen + * + * 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; +} -- cgit v1.2.3-55-g6feb