aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-02-08 09:54:21 +0000
committerRon Yorston <rmy@pobox.com>2019-02-08 10:02:22 +0000
commit0eda390d68c456975289471e68b615ae096ab33b (patch)
tree88e613b3df0bc00e932aab5eeade1a78c8cdfeaa
parent8501e324596fbad9040718d568d12a460e7cf798 (diff)
downloadbusybox-w32-0eda390d68c456975289471e68b615ae096ab33b.tar.gz
busybox-w32-0eda390d68c456975289471e68b615ae096ab33b.tar.bz2
busybox-w32-0eda390d68c456975289471e68b615ae096ab33b.zip
ash: improve handling of 'read -t 0'
Instead of always returning that no input is available: - for a disk file return 'available'; - for the console return 'not available'; - for anything else (e.g. pipe) return whatever poll says. This fixes the test ash-read/read_t0.tests.
-rw-r--r--shell/shell_common.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/shell/shell_common.c b/shell/shell_common.c
index e7c07e6a9..70ecf2095 100644
--- a/shell/shell_common.c
+++ b/shell/shell_common.c
@@ -136,20 +136,28 @@ shell_builtin_read(struct builtin_read_params *params)
136 } 136 }
137 137
138 if (params->opt_t && end_ms == 0) { 138 if (params->opt_t && end_ms == 0) {
139#if !ENABLE_PLATFORM_MINGW32
140 /* "If timeout is 0, read returns immediately, without trying 139 /* "If timeout is 0, read returns immediately, without trying
141 * to read any data. The exit status is 0 if input is available 140 * to read any data. The exit status is 0 if input is available
142 * on the specified file descriptor, non-zero otherwise." 141 * on the specified file descriptor, non-zero otherwise."
143 * bash seems to ignore -p PROMPT for this use case. 142 * bash seems to ignore -p PROMPT for this use case.
144 */ 143 */
145 int r; 144 int r;
145#if ENABLE_PLATFORM_MINGW32
146 HANDLE handle = (HANDLE)_get_osfhandle(fd);
147 DWORD filetype = FILE_TYPE_UNKNOWN;
148
149 if (handle != INVALID_HANDLE_VALUE)
150 filetype = GetFileType(handle);
151 /* poll uses WaitForSingleObject which can't handle disk files */
152 if (filetype == FILE_TYPE_DISK || filetype == FILE_TYPE_UNKNOWN)
153 return (const char *)(uintptr_t)(0);
154 if (isatty(fd))
155 return (const char *)(uintptr_t)(1);
156#endif
146 pfd[0].events = POLLIN; 157 pfd[0].events = POLLIN;
147 r = poll(pfd, 1, /*timeout:*/ 0); 158 r = poll(pfd, 1, /*timeout:*/ 0);
148 /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */ 159 /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */
149 return (const char *)(uintptr_t)(r <= 0); 160 return (const char *)(uintptr_t)(r <= 0);
150#else
151 return (const char *)(uintptr_t)(1);
152#endif
153 } 161 }
154 162
155 if (params->opt_p && isatty(fd)) { 163 if (params->opt_p && isatty(fd)) {