ftpd: code shrink, move replace_char() to libbb
No binary (build failed)
—
(log yok)
| @@ -1113,6 +1113,19 @@ char *bin2hex(char *dst, const char *src, int count) FAST_FUNC; | |
| /* Reverse */ | |
| char* hex2bin(char *dst, const char *src, int count) FAST_FUNC; | |
| + | /* Returns strlen as a bonus */ |
| + | //size_t replace_char(char *s, char what, char with) FAST_FUNC; |
| + | static inline size_t replace_char(char *str, char from, char to) |
| + | { |
| + | char *p = str; |
| + | while (*p) { |
| + | if (*p == from) |
| + | *p = to; |
| + | p++; |
| + | } |
| + | return p - str; |
| + | } |
| + | |
| extern const char c_escape_conv_str00[]; | |
| #define c_escape_conv_str07 (c_escape_conv_str00+3) | |
| @@ -46,3 +46,17 @@ char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char | |
| //dbg_msg("subst9:'%s'", buf); | |
| return buf; | |
| } | |
| + | |
| + | #if 0 /* inlined in libbb.h */ |
| + | /* Returns strlen as a bonus */ |
| + | size_t FAST_FUNC replace_char(char *str, char from, char to) |
| + | { |
| + | char *p = str; |
| + | while (*p) { |
| + | if (*p == from) |
| + | *p = to; |
| + | p++; |
| + | } |
| + | return p - str; |
| + | } |
| + | #endif |
| @@ -186,15 +186,6 @@ static char* find_keyword(char *ptr, size_t len, const char *word) | |
| return NULL; | |
| } | |
| - | static void replace(char *s, char what, char with) |
| - | { |
| - | while (*s) { |
| - | if (what == *s) |
| - | *s = with; |
| - | ++s; |
| - | } |
| - | } |
| - | |
| static char *filename2modname(const char *filename, char *modname) | |
| { | |
| int i; | |
| @@ -230,7 +221,7 @@ static char* str_2_list(const char *str) | |
| dst[len] = '\0'; | |
| memcpy(dst, str, len); | |
| //TODO: protect against 2+ spaces: "word word" | |
| - | replace(dst, ' ', '\0'); |
| + | replace_char(dst, ' ', '\0'); |
| return dst; | |
| } | |
| @@ -369,14 +360,14 @@ static int parse_module(module_info *info, const char *pathname) | |
| } | |
| bksp(); /* remove last ' ' */ | |
| info->aliases = copy_stringbuf(); | |
| - | replace(info->aliases, '-', '_'); |
| + | replace_char(info->aliases, '-', '_'); |
| /* "dependency1 depandency2" */ | |
| reset_stringbuf(); | |
| ptr = find_keyword(module_image, len, "depends="); | |
| if (ptr && *ptr) { | |
| - | replace(ptr, ',', ' '); |
| - | replace(ptr, '-', '_'); |
| + | replace_char(ptr, ',', ' '); |
| + | replace_char(ptr, '-', '_'); |
| dbg2_error_msg("dep:'%s'", ptr); | |
| append(ptr); | |
| } | |
| @@ -707,7 +698,7 @@ static int process_module(char *name, const char *cmdline_options) | |
| dbg1_error_msg("process_module('%s','%s')", name, cmdline_options); | |
| - | replace(name, '-', '_'); |
| + | replace_char(name, '-', '_'); |
| dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove); | |
| @@ -735,7 +726,7 @@ static int process_module(char *name, const char *cmdline_options) | |
| char *opt_filename = xasprintf("/etc/modules/%s", name); | |
| options = xmalloc_open_read_close(opt_filename, NULL); | |
| if (options) | |
| - | replace(options, '\n', ' '); |
| + | replace_char(options, '\n', ' '); |
| #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS | |
| if (cmdline_options) { | |
| /* NB: cmdline_options always have one leading ' ' | |
| @@ -579,7 +579,7 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv) | |
| parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read); | |
| for (i = 0; argv[i]; i++) | |
| - | replace(argv[i], '-', '_'); |
| + | replace_char(argv[i], '-', '_'); |
| while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) { | |
| colon = last_char_is(tokens[0], ':'); | |
| @@ -69,15 +69,6 @@ void FAST_FUNC moddb_free(module_db *db) | |
| } | |
| } | |
| - | void FAST_FUNC replace(char *s, char what, char with) |
| - | { |
| - | while (*s) { |
| - | if (what == *s) |
| - | *s = with; |
| - | ++s; |
| - | } |
| - | } |
| - | |
| int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim) | |
| { | |
| char *tok; | |
| @@ -47,7 +47,6 @@ module_entry *moddb_get(module_db *db, const char *s) FAST_FUNC; | |
| module_entry *moddb_get_or_create(module_db *db, const char *s) FAST_FUNC; | |
| void moddb_free(module_db *db) FAST_FUNC; | |
| - | void replace(char *s, char what, char with) FAST_FUNC; |
| int string_to_llist(char *string, llist_t **llist, const char *delim) FAST_FUNC; | |
| char *filename2modname(const char *filename, char *modname) FAST_FUNC; | |
| #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS | |
| @@ -190,54 +190,39 @@ struct globals { | |
| } while (0) | |
| + | /* escape_text("pfx:", str, (0xff << 8) + '\r') |
| + | * Duplicate 0xff, append \r ^^^^^^^^^^^^^^^^^^ |
| + | */ |
| static char * | |
| escape_text(const char *prepend, const char *str, unsigned escapee) | |
| { | |
| - | unsigned retlen, remainlen, chunklen; |
| - | char *ret, *found; |
| + | char *ret, *p; |
| char append; | |
| append = (char)escapee; | |
| escapee >>= 8; | |
| - | remainlen = strlen(str); |
| - | retlen = strlen(prepend); |
| - | ret = xmalloc(retlen + remainlen * 2 + 1 + 1); |
| - | strcpy(ret, prepend); |
| + | ret = xmalloc(strlen(prepend) + strlen(str) * 2 + 1 + 1); |
| + | p = stpcpy(ret, prepend); |
| for (;;) { | |
| - | found = strchrnul(str, escapee); |
| - | chunklen = found - str + 1; |
| + | char *found = strchrnul(str, escapee); |
| - | /* Copy chunk up to and including escapee (or NUL) to ret */ |
| - | memcpy(ret + retlen, str, chunklen); |
| - | retlen += chunklen; |
| + | /* Copy up to and including escapee (or NUL) */ |
| + | p = mempcpy(p, str, found - str + 1); |
| if (*found == '\0') { | |
| /* It wasn't escapee, it was NUL! */ | |
| - | ret[retlen - 1] = append; /* replace NUL */ |
| - | ret[retlen] = '\0'; /* add NUL */ |
| break; | |
| } | |
| - | ret[retlen++] = escapee; /* duplicate escapee */ |
| str = found + 1; | |
| + | *p++ = escapee; /* duplicate escapee */ |
| } | |
| + | p[-1] = append; /* replace NUL */ |
| + | *p = '\0'; /* add NUL */ |
| return ret; | |
| } | |
| - | /* Returns strlen as a bonus */ |
| - | static unsigned |
| - | replace_char(char *str, char from, char to) |
| - | { |
| - | char *p = str; |
| - | while (*p) { |
| - | if (*p == from) |
| - | *p = to; |
| - | p++; |
| - | } |
| - | return p - str; |
| - | } |
| - | |
| static void | |
| verbose_log(const char *str) | |
| { | |