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

Jordi Sanfeliu jordi@fibranet.cat
Thu Jul 13 08:06:58 GMT 2023


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


More information about the Newlib mailing list