Re: GCD code in plain old C, and a correctness question
Re: GCD code in plain old C, and a correctness question
- Subject: Re: GCD code in plain old C, and a correctness question
- From: Gwynne Raskind <email@hidden>
- Date: Tue, 8 Sep 2009 14:08:06 -0400
On Sep 8, 2009, at 1:33 PM, Kevin Van Vechten wrote:
I believe the "????" is the result of writing (int)-1 to stdout.
Actually it's much worse. I had used calloc to allocate the
context, which had the write fd. I had written -1 to "stdin" (0 is
stdin right?)
Writing to stdin causes the console to barf up ?'s ? My program is
wrong, but that's interesting behavior all the same.
You're right, stdin is file descriptor zero, which is where the
(int)-1 is being written to causing the "????".
I don't know this for certain, but I suspect there's nothing unusual
about this behavior of writing to stdin:
For a program run from a terminal, ssh session, etc., stdin, as well
as stdout and stderr, is a tty - or more exactly, a pty. If you look
look into how pseudo-terminals are used, especially the login_tty()
function, you find that the pty slave fd is dup2()d to stdin, stdout,
and stderr. This fd was, in turn, opened with O_RDWR. The result is
that, in your program, you're reading from and writing to the same
descriptor regardless of which stream you use. Therefore, writing to
stdin is exactly the same as writing to stdout and stderr, as long as
no shell redirections or pipes are in place (more exactly, as long as
isatty(STDIN_FILENO) returns 1).
So, why four question marks? The hexadecimal value of (int)-1 is
0xFFFFFFFF (two's complement signed integer). Write that to a byte
stream (such as a pseudo terminal), and you've output four 0xFF bytes.
0xFF is an illegal character in UTF-8 (the default encoding for
Terminal and Xcode) and some other encodings, so whatever your console
is, it spits out a "nonprintable character" character. Depending on
your font and console, this could be ?, <FF> in inverse video, the
question mark in a black diamond, etc.
You can prove it by running this command from Terminal:
/usr/bin/php -r 'fwrite(STDIN, "Hello, world!\n");'
-- Gwynne
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden