#include <stdlib.h> char *getlogin(void);#include <unistd.h> int getlogin_r(char *name, size_t namelen);
If getlogin is called within a process that is not attached to a terminal, it returns a null pointer. The correct procedure for determining the login name is to call cuserid, or to call getlogin and if it fails to call getpwuid.
getlogin_r puts the name associated by the login activity with the control terminal of the current process in the character string pointed to by name. As the same user ID may be shared by several login names, use getlogin_r with getpwnam[see getpwent(3C)] to ensure that the correct user database entry is found. If successful, getlogin_r returns name which points to the name the user logged in under, even if there are several login names with the same user ID.
getlogin_r returns NULL and ERANGE when namelen is smaller than the length of the string.
Use getlogin_r for multi-threaded applications.
The older getlogin_r interface:
char *getlogin_r(char *name, size_t namelen);is available if _SIMPLE_R is defined.