Implementation status: to be implemented

Synopsis

#include <unistd.h>

char *getlogin(void);

int getlogin_r(char *name, size_t namesize);

Description

The getlogin() and getlogin_r() function are used to get the login name of the current user.

Arguments:

name - the result array for the login name,
namesize - the size of the table.

The getlogin() function returns a pointer to a string containing the user name associated by the login activity with the controlling terminal of the current process. If getlogin() returns a non-null pointer, then that pointer points to the name that the user logged in under, even if there are several login names with the same user ID. The getlogin() function is not be thread-safe.

The getlogin_r() function puts 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_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 getlogin() and getlogin_r() functions 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.

Return value

Upon successful completion, getlogin() returns a pointer to the login name or a null pointer if the user's login name cannot be found. Otherwise, it returns a null pointer and set errno to indicate the error.

The application does 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 returns zero; otherwise, an error number is returned to indicate the error.

Errors

These functions fail if:

[EMFILE] All file descriptors available to the process are currently open.
[ENFILE] The maximum allowable number of files is currently open in the system.
[ENOTTY] None of the file descriptors 0, 1, or 2 is open to the controlling terminal of the current process.
[ENXIO] The calling process has no controlling terminal.

The getlogin_r() function fails if:

[ERANGE] The value of namesize is smaller than the length of the string to be returned including the terminating null character.

Implementation tasks

  • Implement getlogin().
  • Implement getlogin_r().