libbb: make trim() return pointer to terminating NUL
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 5 Aug 2017 15:50:35 +0000 (17:50 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 5 Aug 2017 15:50:35 +0000 (17:50 +0200)
function                                             old     new   delta
trim                                                  80      90     +10
angle_address                                         56      50      -6
sysctl_main                                          282     273      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: +10/-15)           Total:  -5 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/trim.c
mailutils/sendmail.c
miscutils/lsscsi.c
procps/sysctl.c

index 6a2a2d640f8e8ec8f292a135824d61dc4f848891..6077f64c98cb98db312cc306b8eaac1ecbedb786 100644 (file)
@@ -347,7 +347,7 @@ unsigned long long monotonic_ms(void) FAST_FUNC;
 unsigned monotonic_sec(void) FAST_FUNC;
 
 extern void chomp(char *s) FAST_FUNC;
-extern void trim(char *s) FAST_FUNC;
+extern char *trim(char *s) FAST_FUNC;
 extern char *skip_whitespace(const char *) FAST_FUNC;
 extern char *skip_non_whitespace(const char *) FAST_FUNC;
 extern char *skip_dev_pfx(const char *tty_name) FAST_FUNC;
index 16cb4fbb07f256794e1047041d710d3b923dd816..e47fec74e4ccfa100cff62981d4133f40a1f5810 100644 (file)
 
 #include "libbb.h"
 
-void FAST_FUNC trim(char *s)
+char* FAST_FUNC trim(char *s)
 {
        size_t len = strlen(s);
+       size_t old = len;
 
        /* trim trailing whitespace */
        while (len && isspace(s[len-1]))
@@ -26,5 +27,12 @@ void FAST_FUNC trim(char *s)
                        memmove(s, nws, len);
                }
        }
-       s[len] = '\0';
+
+       s += len;
+       /* If it was a "const char*" which does not need trimming,
+        * avoid superfluous store */
+       if (old != len)
+               *s = '\0';
+
+       return s;
 }
index 346de2712009919ee47bb78320b1afb13e126ea3..65895f0ec81f4d38e391044d3f4e2d61400305a6 100644 (file)
@@ -166,9 +166,8 @@ static char *angle_address(char *str)
 {
        char *s, *e;
 
-       trim(str);
-       e = last_char_is(str, '>');
-       if (e) {
+       e = trim(str);
+       if (e != str && e[-1] == '>') {
                s = strrchr(str, '<');
                if (s) {
                        *e = '\0';
index b69ff1eefbcea4f138a24e8ead4ebedef0fe2906..c86630e31f6a96b0a665e2128215e917f2687c15 100644 (file)
@@ -37,9 +37,8 @@ static char *get_line(const char *filename, char *buf, unsigned *bufsize_p)
        if (sz < 0)
                sz = 0;
        buf[sz] = '\0';
-       trim(buf);
 
-       sz = strlen(buf) + 1;
+       sz = (trim(buf) - buf) + 1;
        bufsize -= sz;
        buf += sz;
        buf[0] = '\0';
index 619f4f1e48c54faa48be9f66de26c44d137b28d0..ef1a1b99f8c31c48608773bdb32ecb986bc8e8de 100644 (file)
@@ -257,12 +257,16 @@ static int sysctl_handle_preload_file(const char *filename)
        parse_flags &= ~PARSE_EOL_COMMENTS; // NO (only first char) - comments are recognized even if not first char
        while (config_read(parser, token, 2, 2, "#=", parse_flags)) {
                char *tp;
-               trim(token[0]);
+
                trim(token[1]);
+               tp = trim(token[0]);
                sysctl_dots_to_slashes(token[0]);
-               tp = xasprintf("%s=%s", token[0], token[1]);
-               sysctl_act_on_setting(tp);
-               free(tp);
+               /* ^^^converted in-place. tp still points to NUL */
+               /* now, add "=TOKEN1" */
+               *tp++ = '=';
+               overlapping_strcpy(tp, token[1]);
+
+               sysctl_act_on_setting(token[0]);
        }
        if (ENABLE_FEATURE_CLEAN_UP)
                config_close(parser);