shell: more efficient check for EOL in read

function                                             old     new   delta
shell_builtin_read                                  1334    1320     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-08-09 14:04:07 +02:00
parent 1f41c885fc
commit cde46f75cb
2 changed files with 8 additions and 4 deletions

View File

@ -65,6 +65,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
int nchars; /* -n NUM */ int nchars; /* -n NUM */
char **pp; char **pp;
char *buffer; char *buffer;
char delim;
struct termios tty, old_tty; struct termios tty, old_tty;
const char *retval; const char *retval;
int bufpos; /* need to be able to hold -1 */ 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(); end_ms += (unsigned)monotonic_ms();
buffer = NULL; buffer = NULL;
bufpos = 0; bufpos = 0;
delim = opt_d ? *opt_d : '\n';
do { do {
char c; char c;
int timeout; int timeout;
@ -238,10 +240,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
continue; continue;
} }
} }
if (opt_d) { if (c == delim) /* '\n' or -d CHAR */
if (c == *opt_d)
break;
} else if (c == '\n')
break; break;
/* $IFS splitting. NOT done if we run "read" /* $IFS splitting. NOT done if we run "read"

View File

@ -34,6 +34,11 @@ enum {
BUILTIN_READ_SILENT = 1 << 0, BUILTIN_READ_SILENT = 1 << 0,
BUILTIN_READ_RAW = 1 << 1, 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 const char* FAST_FUNC
shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
char **argv, char **argv,