getlogin, getlogin_r --- get login name
#include <unistd.h> char *getlogin(void); int getlogin_r(char *name, size_t namesize);
The getlogin() 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 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 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.
Upon successful completion, getlogin() 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 errno 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 getlogin(). If successful, the getlogin_r() function shall return zero; otherwise, an error number shall be returned to indicate the error.
These functions may 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 may fail if: ERANGE The value of namesize is smaller than the length of the string to be returned including the terminating null character. The following sections are informative.
Getting the User Login Name S 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); }
Three names associated with the current process can be determined: getpwuid(geteuid()) shall return the name associated with the effective user ID of the process; getlogin() shall return the name associated with the current login activity; and getpwuid(getuid()) shall return the name associated with the real user ID of the process. 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() 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 getlogin() should be used to provide the argument to the getpwnam() 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 cuserid() function, which was originally defined in the POSIX.11988 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.
None.
getpwnam(), getpwuid(), geteuid(), getuid() The Base Definitions volume of POSIX.12008, <limits.h>, <unistd.h>
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 .
Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.
Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.
Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.
Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.
The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.
Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.
Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.
Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.