1GETLOGIN(3) Linux Programmer's Manual GETLOGIN(3)
2
3
4
6 getlogin, getlogin_r, cuserid - get user name
7
9 #include <unistd.h>
10
11 char *getlogin(void);
12 int getlogin_r(char *buf, size_t bufsize);
13
14 #include <stdio.h>
15
16 char *cuserid(char *string);
17
19 getlogin() returns a pointer to a string containing the name of the
20 user logged in on the controlling terminal of the process, or a null
21 pointer if this information cannot be determined. The string is stati‐
22 cally allocated and might be overwritten on subsequent calls to this
23 function or to cuserid().
24
25 getlogin_r() returns this same user name in the array buf of size buf‐
26 size.
27
28 cuserid() returns a pointer to a string containing a user name associ‐
29 ated with the effective user ID of the process. If string is not a
30 null pointer, it should be an array that can hold at least L_cuserid
31 characters; the string is returned in this array. Otherwise, a pointer
32 to a string in a static area is returned. This string is statically
33 allocated and might be overwritten on subsequent calls to this function
34 or to getlogin().
35
36 The macro L_cuserid is an integer constant that indicates how long an
37 array you might need to store a user name. L_cuserid is declared in
38 stdio.h.
39
40 These functions let your program identify positively the user who is
41 running (cuserid()) or the user who logged in this session (getlo‐
42 gin()). (These can differ when set-user-ID programs are involved.)
43
44 For most purposes, it is more useful to use the environment variable
45 LOGNAME to find out who the user is. This is more flexible precisely
46 because the user can set LOGNAME arbitrarily.
47
49 getlogin() returns a pointer to the user name when successful, and NULL
50 on failure. getlogin_r() returns 0 when successful, and non-zero on
51 failure.
52
54 POSIX specifies
55
56 EMFILE The calling process already has the maximum allowed number of
57 open files.
58
59 ENFILE The system already has the maximum allowed number of open files.
60
61 ENXIO The calling process has no controlling tty.
62
63 ERANGE (getlogin_r) The length of the user name, including the termi‐
64 nating null byte, is larger than bufsize.
65
66 Linux/glibc also has
67
68 ENOENT There was no corresponding entry in the utmp-file.
69
70 ENOMEM Insufficient memory to allocate passwd structure.
71
73 /etc/passwd password database file
74 /var/run/utmp (traditionally /etc/utmp;
75 some libc versions used /var/adm/utmp)
76
78 getlogin() and getlogin_r() specified in POSIX.1-2001.
79
80 System V has a cuserid() function whi