Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Jan 2015 21:06:44 +0000
From:      "Montgomery-Smith, Stephen" <stephen@missouri.edu>
To:        less xss <less.xss@gmail.com>, "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
Subject:   Re: ctrl-d appends characters to output
Message-ID:  <54BACEE3.9080500@missouri.edu>
In-Reply-To: <54BACD8C.3050703@missouri.edu>
References:  <CAGcjGpP0t3%2BcJLRHS5Ggfjao9Vcy0xosAg2KqBWgVA_NtKoNgA@mail.gmail.com> <54BAC302.7050800@missouri.edu> <54BACD8C.3050703@missouri.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On 01/17/2015 03:01 PM, Montgomery-Smith, Stephen wrote:
> On 01/17/2015 02:16 PM, Montgomery-Smith, Stephen wrote:
>> On 01/17/2015 11:59 AM, less xss wrote:
>>> I've searched around quite a bit with no luck on this matter. I current=
ly
>>> have an issue where I send EOF (ctrl-d) to some simple K&R2 exercises a=
nd
>>> the terminal returns the D character appended to my data when EOF is se=
nt.
>>> I wish to prevent any and all extra characters from being appended and =
I
>>> would also like to understand why it's happening. The following code
>>> is an example exercise from K&R2 that yield said problem.
>>>
>>> #include <stdio.h>
>>>
>>> int main() {
>>>     double nc;
>>>
>>>     for (nc =3D 0; getchar() !=3D EOF; ++nc) {
>>>         ; /* syntactic null statement */
>>>     }
>>>
>>>     printf("%.0f\n", nc);
>>> }
>>>
>>> $ ./a.out
>>> 0D
>>> $
>>
>> I did a bit of experimenting with this issue.  First, I cannot reproduce
>> it on my Linux box.  Second, this simpler program does the same thing:
>>
>> #include <stdio.h>
>>
>> int main() {
>>
>>     while (getchar() !=3D EOF) {
>>         ; /* syntactic null statement */
>>     }
>>
>>     printf("\n");
>> }
>>
>> In this case I get:
>>
>> % ./a.out
>> ^D
>> %
>>
>> However, if I remove that last printf statement, then no ^D is displayed=
.
>>
>> Considering the inconsistent nature of when this ^D appears, I would
>> prefer to call it a bug than a feature.  But it must have been put there
>> by design.
>=20
> OK, that last printf is NOT responsible for the ^D.  It is just that the
> prompt wipes it out.  Try this code:
>=20
> #include <stdio.h>
> #include <unistd.h>
>=20
> int main() {
>=20
>     while (getchar() !=3D EOF) {
>         ; /* syntactic null statement */
>     }
>     sleep(10);
> }
>=20
> Then the ^D shows until the prompt appears 10 seconds later.

Even simpler - you don't even have to write a program:

cat | sleep 10



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?54BACEE3.9080500>