Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Jan 2015 21:01:04 +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:  <54BACD8C.3050703@missouri.edu>
In-Reply-To: <54BAC302.7050800@missouri.edu>
References:  <CAGcjGpP0t3%2BcJLRHS5Ggfjao9Vcy0xosAg2KqBWgVA_NtKoNgA@mail.gmail.com> <54BAC302.7050800@missouri.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
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 currentl=
y
>> have an issue where I send EOF (ctrl-d) to some simple K&R2 exercises an=
d
>> the terminal returns the D character appended to my data when EOF is sen=
t.
>> 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
>> $
>=20
> 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:
>=20
> #include <stdio.h>
>=20
> int main() {
>=20
>     while (getchar() !=3D EOF) {
>         ; /* syntactic null statement */
>     }
>=20
>     printf("\n");
> }
>=20
> In this case I get:
>=20
> % ./a.out
> ^D
> %
>=20
> However, if I remove that last printf statement, then no ^D is displayed.
>=20
> 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.

OK, that last printf is NOT responsible for the ^D.  It is just that the
prompt wipes it out.  Try this code:

#include <stdio.h>
#include <unistd.h>

int main() {

    while (getchar() !=3D EOF) {
        ; /* syntactic null statement */
    }
    sleep(10);
}

Then the ^D shows until the prompt appears 10 seconds later.



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