当前位置: 技术问答>linux和unix
linux获取登录当前系统的用户名和UID
来源: 互联网 发布时间:2017-01-06
本文导语: 请问一下linux中如何用函数获取登录当前系统的UID和用户名啊,就是开机时输入的那个。 我的系统是Fedora 14. | 那就先getlogin, 然后从用户名获取uid, 用getpwnam NAME getpwnam, getpwnam_r, getpwuid, ge...
请问一下linux中如何用函数获取登录当前系统的UID和用户名啊,就是开机时输入的那个。 我的系统是Fedora 14.
|
那就先getlogin, 然后从用户名获取uid, 用getpwnam
NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
SYNOPSIS
#include
#include
struct passwd *getpwnam(const char *name);
DESCRIPTION
The getpwnam() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user name name.
The getpwuid() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user uid uid.
The passwd structure is defined in as follows:
struct passwd {
char *pw_name; /* user name */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user id */
gid_t pw_gid; /* group id */
char *pw_gecos; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
SYNOPSIS
#include
#include
struct passwd *getpwnam(const char *name);
DESCRIPTION
The getpwnam() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user name name.
The getpwuid() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user uid uid.
The passwd structure is defined in as follows:
struct passwd {
char *pw_name; /* user name */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user id */
gid_t pw_gid; /* group id */
char *pw_gecos; /* real name */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
|
getpwent()直接获取passwd结构体,其中的pw_name,pw_uid