From 2a4b086d4848616306f97f6378e0f10a48d41929 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 12 Jul 2023 11:52:06 +0100 Subject: ash: properly echo console input to 'read' built-in The 'read' shell built-in echoed console input to stdout. Echo directly to the console instead. Costs 124-136 bytes. --- include/mingw.h | 1 + shell/ash.c | 4 ++-- shell/shell_common.c | 4 ++-- win32/winansi.c | 11 +++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index f11316205..ae34666e8 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -174,6 +174,7 @@ size_t winansi_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); int winansi_getc(FILE *stream); int winansi_getchar(void); char *winansi_fgets(char *s, int size, FILE *stream); +void console_write(const char *str, int len); #define putchar winansi_putchar #define puts winansi_puts diff --git a/shell/ash.c b/shell/ash.c index 17af816d4..f3eb98d85 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -15775,8 +15775,8 @@ exitshell(void) out: #if ENABLE_SUW32 if (delayexit) { - freopen("CONOUT$", "w", stdout); - fputs_stdout("Press any key to exit..."); +#define EXIT_MSG "Press any key to exit..." + console_write(EXIT_MSG, sizeof(EXIT_MSG) - 1); _getch(); } #endif diff --git a/shell/shell_common.c b/shell/shell_common.c index 716d3aebb..99e56a050 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -235,7 +235,7 @@ shell_builtin_read(struct builtin_read_params *params) --bufpos; ++nchars; if (!(read_flags & BUILTIN_READ_SILENT)) { - printf("\b \b"); + console_write("\b \b", 3); } } goto loop; @@ -243,7 +243,7 @@ shell_builtin_read(struct builtin_read_params *params) buffer[bufpos] = key == '\r' ? '\n' : key; if (!(read_flags & BUILTIN_READ_SILENT)) { /* echo input if not in silent mode */ - putchar(buffer[bufpos]); + console_write(buffer + bufpos, 1); } } else { diff --git a/win32/winansi.c b/win32/winansi.c index 818ab1793..bc3e69163 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -1405,3 +1405,14 @@ BOOL readConsoleInput_utf8(HANDLE h, INPUT_RECORD *r, DWORD len, DWORD *got) return FALSE; } #endif + +void console_write(const char *str, int len) +{ + char *buf = xmemdup(str, len); + int fd = _open("CONOUT$", _O_WRONLY); + HANDLE fh = (HANDLE)_get_osfhandle(fd); + charToConBuffA(buf, len); + WriteConsole(fh, buf, len, NULL, NULL); + close(fd); + free(buf); +} -- cgit v1.2.3-55-g6feb