shell: more efficient check for EOL in read
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 9 Aug 2017 12:04:07 +0000 (14:04 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 9 Aug 2017 12:04:07 +0000 (14:04 +0200)
function                                             old     new   delta
shell_builtin_read                                  1334    1320     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/shell_common.c
shell/shell_common.h

index 2db8ea3e275bdecbab6882e430e6b14822b0ef3a..7a0799ed590938f102069d8dec8b7902ece1bf47 100644 (file)
@@ -65,6 +65,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        int nchars; /* -n NUM */
        char **pp;
        char *buffer;
+       char delim;
        struct termios tty, old_tty;
        const char *retval;
        int bufpos; /* need to be able to hold -1 */
@@ -185,6 +186,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
                end_ms += (unsigned)monotonic_ms();
        buffer = NULL;
        bufpos = 0;
+       delim = opt_d ? *opt_d : '\n';
        do {
                char c;
                int timeout;
@@ -238,10 +240,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
                                continue;
                        }
                }
-               if (opt_d) {
-                       if (c == *opt_d)
-                               break;
-               } else if (c == '\n')
+               if (c == delim) /* '\n' or -d CHAR */
                        break;
 
                /* $IFS splitting. NOT done if we run "read"
index 1b79bffcae845fc28f1548f24d1fd39375ac6ac6..875fd9ea7ba168ddfc46f1b9176870beaf8f577f 100644 (file)
@@ -34,6 +34,11 @@ enum {
        BUILTIN_READ_SILENT = 1 << 0,
        BUILTIN_READ_RAW    = 1 << 1,
 };
+//TODO? do not provide bashisms if not asked for:
+//#if !ENABLE_HUSH_BASH_COMPAT && !ENABLE_ASH_BASH_COMPAT
+//#define shell_builtin_read(setvar,argv,ifs,read_flags,n,p,t,u,d)
+//     shell_builtin_read(setvar,argv,ifs,read_flags)
+//#endif
 const char* FAST_FUNC
 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        char       **argv,