End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Etc.getlogin
getlogin → String
ext/etc/etc.c
View on GitHub ↗
static VALUE
etc_getlogin(VALUE obj)
{
char *login;
#ifdef HAVE_GETLOGIN
login = getlogin();
if (!login) login = getenv("USER");
#else
login = getenv("USER");
#endif
if (login) {
#ifdef _WIN32
rb_encoding *extenc = rb_utf8_encoding();
#else
rb_encoding *extenc = rb_locale_encoding();
#endif
return rb_external_str_new_with_enc(login, strlen(login), extenc);
}
return Qnil;
}
Returns the short user name of the currently logged in user. Unfortunately, it is often rather easy to fool ::getlogin.
Avoid ::getlogin for security-related purposes.
If ::getlogin fails, try ::getpwuid.
See the unix manpage for getpwuid(3) for more detail.
e.g.
Etc.getlogin -> 'guest'