From: Ulrich Drepper Date: Wed, 10 Nov 1999 03:27:23 +0000 (+0000) Subject: (getlogin_r): Sync with getlogin implementation for ttyname_r call; fix X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ca4113b7bec284224c664a5e6a767b9bc797fa3;p=thirdparty%2Fglibc.git (getlogin_r): Sync with getlogin implementation for ttyname_r call; fix inverted condition; return ut_user. --- diff --git a/sysdeps/unix/getlogin_r.c b/sysdeps/unix/getlogin_r.c index cb0545e08a3..04b99056b0e 100644 --- a/sysdeps/unix/getlogin_r.c +++ b/sysdeps/unix/getlogin_r.c @@ -40,20 +40,20 @@ getlogin_r (name, name_len) int result = 0; struct utmp *ut, line, buffer; - { - int d = __open ("/dev/tty", 0); - if (d < 0) - return errno; + /* Get name of tty connected to fd 0. Return if not a tty or + if fd 0 isn't open. Note that a lot of documentation says that + getlogin() is based on the controlling terminal---what they + really mean is "the terminal connected to standard input". The + getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all + return NULL if fd 0 has been closed, so this is the compatible + thing to do. Note that ttyname(open("/dev/tty")) on those + systems returns /dev/tty, so that is not a possible solution for + getlogin(). */ - result = __ttyname_r (d, real_tty_path, sizeof (tty_pathname)); - (void) __close (d); - - if (result != 0) - { - __set_errno (result); - return result; - } - } + result = __ttyname_r (0, real_tty_path, sizeof (tty_pathname)); + + if (result != 0) + return result; real_tty_path += 5; /* Remove "/dev/". */ @@ -69,16 +69,16 @@ getlogin_r (name, name_len) } else { - size_t needed = strlen (ut->ut_line) + 1; + size_t needed = strlen (ut->ut_user) + 1; - if (needed < name_len) + if (needed > name_len) { __set_errno (ERANGE); result = ERANGE; } else { - memcpy (name, ut->ut_line, needed); + memcpy (name, ut->ut_user, needed); result = 0; } }