GETLOGIN(3)                POSIX Programmer's Manual               GETLOGIN(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       getlogin, getlogin_r -- get login name

SSYYNNOOPPSSIISS
       #include <unistd.h>

       char *getlogin(void);
       int getlogin_r(char *_n_a_m_e, size_t _n_a_m_e_s_i_z_e);

DDEESSCCRRIIPPTTIIOONN
       The _g_e_t_l_o_g_i_n() function shall return a pointer to a string containing
       the user name associated by the login activity with the controlling
       terminal of the current process. If _g_e_t_l_o_g_i_n() 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 _g_e_t_l_o_g_i_n() function need not be thread-safe.

       The _g_e_t_l_o_g_i_n___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 _n_a_m_e.  The array is _n_a_m_e_s_i_z_e 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 _g_e_t_l_o_g_i_n___r() is successful, _n_a_m_e points to the name the user used at
       login, even if there are several login names with the same user ID.

       The _g_e_t_l_o_g_i_n() and _g_e_t_l_o_g_i_n___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 //ddeevv//ttttyy.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _g_e_t_l_o_g_i_n() shall return a pointer to the
       login name or a null pointer if the user's login name cannot be found.
       Otherwise, it shall return a null pointer and set _e_r_r_n_o to indicate the
       error.

       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 _g_e_t_l_o_g_i_n().

       If successful, the _g_e_t_l_o_g_i_n___r() function shall return zero; otherwise,
       an error number shall be returned to indicate the error.

EERRRROORRSS
       These functions may fail if:

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       EENNFFIILLEE The maximum allowable number of files is currently open in the
              system.

       EENNOOTTTTYY None of the file descriptors 0, 1, or 2 is open to the
              controlling terminal of the current process.

       EENNXXIIOO  The calling process has no controlling terminal.

       The _g_e_t_l_o_g_i_n___r() function may fail if:

       EERRAANNGGEE The value of _n_a_m_e_s_i_z_e is smaller than the length of the string
              to be returned including the terminating null character.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   GGeettttiinngg tthhee UUsseerr LLooggiinn NNaammee SS
       The following example calls the _g_e_t_l_o_g_i_n() function to obtain the name
       of the user associated with the calling process, and passes this
       information to the _g_e_t_p_w_n_a_m() function to get the associated user
       database information.

           ##iinncclluuddee <<uunniissttdd..hh>>
           ##iinncclluuddee <<ssyyss//ttyyppeess..hh>>
           ##iinncclluuddee <<ppwwdd..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ......
           cchhaarr **llggnn;;
           ssttrruucctt ppaasssswwdd **ppww;;
           ......
           iiff ((((llggnn == ggeettllooggiinn(()))) ==== NNUULLLL |||| ((ppww == ggeettppwwnnaamm((llggnn)))) ==== NNUULLLL)) {{
               ffpprriinnttff((ssttddeerrrr,, ""GGeett ooff uusseerr iinnffoorrmmaattiioonn ffaaiilleedd..\\nn""));; eexxiitt((11));;
               }}

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Three names associated with the current process can be determined:
       _g_e_t_p_w_u_i_d(_g_e_t_e_u_i_d()) shall return the name associated with the effective
       user ID of the process; _g_e_t_l_o_g_i_n() shall return the name associated
       with the current login activity; and _g_e_t_p_w_u_i_d(_g_e_t_u_i_d()) shall return
       the name associated with the real user ID of the process.

       The _g_e_t_l_o_g_i_n___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.

RRAATTIIOONNAALLEE
       The _g_e_t_l_o_g_i_n() function returns a pointer to the user's login name. The
       same user ID may be shared by several login names. If it is desired to
       get the user database entry that is used during login, the result of
       _g_e_t_l_o_g_i_n() should be used to provide the argument to the _g_e_t_p_w_n_a_m()
       function. (This might be used to determine the user's login shell,
       particularly where a single user has multiple login shells with
       distinct login names, but the same user ID.)

       The information provided by the _c_u_s_e_r_i_d() function, which was
       originally defined in the POSIX.1-1988 standard and subsequently
       removed, can be obtained by the following:

           ggeettppwwuuiidd((ggeetteeuuiidd(())))

       while the information provided by historical implementations of
       _c_u_s_e_r_i_d() can be obtained by:

           ggeettppwwuuiidd((ggeettuuiidd(())))

       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.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _g_e_t_p_w_n_a_m(), _g_e_t_p_w_u_i_d(), _g_e_t_e_u_i_d(), _g_e_t_u_i_d()

       The Base Definitions volume of POSIX.1-2008, <<lliimmiittss..hh>>, <<uunniissttdd..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       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 .



IEEE/The Open Group                  2013                          GETLOGIN(3)
