Linux getpwuid obtains the specified user information

Source: Internet
Author: User
Passwd structure related to system data

A passwd structure related to system data is defined as follows:

/* The passwd structure .*/
Struct passwd
{
Char * pw_name;/* User name */
Char * pw_passwd;/* password .*/
_ Uid_t pw_uid;/* User ID .*/
_ Gid_t pw_gid;/* Group ID .*/
Char * pw_gecos;/* real name */
Char * pw_dir;/* main directory .*/
Char * pw_shell;/* shell used */
};

This structure describes the/etc/passwd file record lines, including the user name, password, user ID, user group ID, real name, user home directory, and shell
Related APIs include
Struct passwd * getpwuid (UID)
Struct paswd * getpwnam (const char * name)

Getpwuid returns a structure pointing to passwd based on the input user ID. This struct initializes all the members in it.
Getpwnam is the same as getpwuid, but the input parameter is the user name.

Test Tool: This program only demonstrates and does not handle errors.

# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <PWD. h>
Int main (void)
{
Struct passwd * passwd;
Passwd = getpwuid (getuid ());
/** Passwd = getcwnam ("phpos ");**/
Printf ("username: % s/n", passwd-> pw_name );
Printf ("Password: % s/n", passwd-> pw_passwd );
Printf ("uid: % d/N", passwd-> pw_uid );
Printf ("GID: % d/N", passwd-> pw_gid );
Printf ("shell: % s/n", passwd-> pw_shell );
Printf ("dir: % s/n", passwd-> pw_dir );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.