From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin@cygwin.com
Subject: Re: Bug in Control-d handling?
Date: Mon, 20 Jun 2022 17:59:35 +0900 [thread overview]
Message-ID: <20220620175935.924a49feb13156f1a3cf3da4@nifty.ne.jp> (raw)
In-Reply-To: <827e2006-2aae-9f7f-9c3f-eef3a7c6e793@cornell.edu>
On Sun, 19 Jun 2022 15:08:19 -0400
Ken Brown wrote:
> Consider the following program, which reads from standard input a line at a time
> and then echoes the input back to the terminal:
>
> $ cat cat_line.c
> #include <stdio.h>
>
> int main ()
> {
> char buf[BUFSIZ];
>
> while (fgets (buf, BUFSIZ, stdin))
> fputs (buf, stdout);
> }
>
> Run the program, type one or more characters (without hitting Enter), and type
> Ctrl-d until the program exits. What I expect is that nothing visible happens
> on the first Ctrl-d [but the input is sent to the internal stdin buffer], and
> that the input is echoed and the program exits after the second Ctrl-d [the
> program sees EOF]. This is what happens on Linux. On Cygwin, however, the
> program keeps running after the second Ctrl-d and doesn't exit until Ctrl-d is
> pressed a third time.
>
> I observed this problem because of a failing Emacs test, in which the program
> "rev" was not seeing EOF after being sent Ctrl-d; "rev" does something like the
> test case above, but using fgetws instead of fgets.
Isn't this a bug of newlib? Try following code.
#include <stdio.h>
int main()
{
printf("%d\n", getchar());
printf("%d\n", feof(stdin));
printf("%d\n", getchar());
return 0;
}
If you press Ctrl-D at the first getchar(), the second getchar()
does not return EOF while it does in linux.
The following patch seems to resolve the issue.
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index ccedc7af7..843163b7e 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -47,11 +47,9 @@ __srefill_r (struct _reent * ptr,
fp->_r = 0; /* largely a convenience for callers */
-#ifndef __CYGWIN__
/* SysV does not make this test; take it out for compatibility */
if (fp->_flags & __SEOF)
return EOF;
-#endif
/* if not already reading, have to be reading and writing */
if ((fp->_flags & __SRD) == 0)
However, I am not sure what this #ifndef __CYGWIN__ is for.
--
Takashi Yano <takashi.yano@nifty.ne.jp>
next prev parent reply other threads:[~2022-06-20 9:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-19 19:08 Ken Brown
2022-06-20 8:59 ` Takashi Yano [this message]
2022-06-20 10:22 ` Takashi Yano
2022-06-20 13:24 ` Ken Brown
2022-06-20 13:53 ` Eliot Moss
2022-06-20 17:50 ` Ken Brown
2022-07-04 8:10 ` Corinna Vinschen
2022-06-20 17:12 ` Achim Gratz
2022-06-20 19:22 ` Brian Inglis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220620175935.924a49feb13156f1a3cf3da4@nifty.ne.jp \
--to=takashi.yano@nifty.ne.jp \
--cc=cygwin@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).