Re: [PATCH] Fix getlogin() to check only stdin to get a valid tty

Jordi Sanfeliu via Newlib <[email protected]>
Newsgroups gmane.comp.lib.newlib
Organization Fibranet NSP, SL
Message-ID <[email protected]>
Hello Torbjörn,

Thanks for you reply.

It seems to me that this code returns 0 as soon as one of the 3 fds is 
0, regardless if the rest are valid tty names.

I think that what you meant is this:

         if ((tty = ttyname (0)) == 0)
             if ((tty = ttyname (1)) == 0)
                 if ((tty = ttyname (2)) == 0)
                     return 0;

which IMO would be even a better patch.

What do you think?

Best regards.


On 7/12/23 20:50, Torbjorn SVENSSON wrote:

>> -  if (((tty = ttyname (0)) == 0)
>> -      || ((tty = ttyname (1)) == 0)
>> -      || ((tty = ttyname (2)) == 0))
> 
> These 3 lines of code checks if one of stdin, stdout or stderr is 
> connected to a terminal device. If the return value of ttyname is 0, it 
> means that there is no terminal device connected to that fd.
> As I read the code, it first tries with stdin. If stdin is closed or 
> redirected, it tries with stdout instead and then lastly, falls back to 
> trying with stderr. If none of the 3 fd's provides a terminal device, 
> then the getlogin will return 0.

-- 
Jordi Sanfeliu
FIBRANET Network Services Provider
https://www.fibranet.cat
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.