#include <unistd.h> char *getlogin(void); int getlogin_r(char *name, size_t namesize);
The getlogin() function need not be thread-safe.
The getlogin_r() function shall put the name associated by the login activity with the controlling terminal of the current process in the character array pointed to by name. The array is namesize characters long and should have space for the name and the terminating null character. The maximum size of the login name is {LOGIN_NAME_MAX}.
If getlogin_r() is successful, name points to the name the user used at login, even if there are several login names with the same user ID.
The getlogin() and getlogin_r() functions may make use of file descriptors 0, 1, and 2 to find the controlling terminal of the current process, examining each in turn until the terminal is found. If in this case none of these three file descriptors is open to the controlling terminal, these functions may fail. The method used to find the terminal associated with a file descriptor may depend on the file descriptor being open to the actual terminal device, not /dev/tty.
The application shall not modify the string returned. The returned pointer might be invalidated or the string content might be overwritten by a subsequent call to getlogin(). The returned pointer and the string content might also be invalidated if the calling thread is terminated.
If successful, the getlogin_r() function shall return zero; otherwise, an error number shall be returned to indicate the error.
The getlogin_r() function may fail if:
The following sections are informative.
The following example calls the getlogin() function to obtain the name of the user associated with the calling process, and passes this information to the getpwnam() function to get the associated user database information.
#include <unistd.h> #include <sys/types.h> #include <pwd.h> #include <stdio.h> ... char *lgn; struct passwd *pw; ... if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) { fprintf(stderr, "Get of user information failed.\n"); exit(1); }
The getlogin_r() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call.
The information provided by the cuserid() function, which was originally defined in the POSIX.1-1988 standard and subsequently removed, can be obtained by the following:
getpwuid(geteuid())
while the information provided by historical implementations of cuserid() can be obtained by:
getpwuid(getuid())
The thread-safe version of this function places the user name in a user-supplied buffer and returns a non-zero value if it fails. The non-thread-safe version may return the name in a static data area that may be overwritten by each call.
The Base Definitions volume of POSIX.1-2017, <limits.h>, <unistd.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .