From bd8afa5eb160b56715b805befe850a5ba2131d28 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Wed, 16 May 2012 01:56:41 +0000 Subject: * DevNotes: Add entry cgf-000008. * fhandler_tty.cc (bytes_available): Simplify by returning the number of bytes available in the message unless that is zero. --- winsup/cygwin/fhandler_tty.cc | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'winsup/cygwin/fhandler_tty.cc') diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 1428a064e..38a0571cb 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -53,17 +53,20 @@ fhandler_pty_slave::get_unit () bool bytes_available (DWORD& n, HANDLE h) { - char buf[INP_BUFFER_SIZE]; - /* Apparently need to pass in a dummy buffer to read a real "record" from - the pipe. So buf is used and then discarded just so we can see how many - bytes will be read by the next ReadFile(). */ - bool succeeded = PeekNamedPipe (h, buf, sizeof (buf), &n, NULL, NULL); - if (!succeeded) + DWORD navail, nleft; + navail = nleft = 0; + bool succeeded = PeekNamedPipe (h, NULL, 0, NULL, &navail, &nleft); + if (succeeded) + /* nleft should always be the right choice unless something has written 0 + bytes to the pipe. In that pathological case we return the actual number + of bytes available in the pipe. See cgf-000008 for more details. */ + n = nleft ?: navail; + else { termios_printf ("PeekNamedPipe(%p) failed, %E", h); n = 0; } - debug_only_printf ("%u bytes available", n); + debug_only_printf ("n %u, nleft %u, navail %u"); return succeeded; } -- cgit v1.2.3