+ -
当前位置:首页 → 问答吧 → linux获取登录当前系统的用户名和UID

linux获取登录当前系统的用户名和UID

时间:2011-10-10

来源:互联网

请问一下linux中如何用函数获取登录当前系统的UID和用户名啊,就是开机时输入的那个。 我的系统是Fedora 14.

作者: linux_6   发布时间: 2011-10-10

getuid

作者: bluejays   发布时间: 2011-10-10

getlogin

作者: bluejays   发布时间: 2011-10-10

引用 2 楼 bluejays 的回复:

getlogin

getlogin只可以获取用户名啊,那uid该如何获取呢?

作者: linux_6   发布时间: 2011-10-10

引用 1 楼 bluejays 的回复:

getuid

getuid只能获取当前进程的属主的uid, 和这个问题好像没有什么关系吧。

作者: linux_6   发布时间: 2011-10-10

那就先getlogin, 然后从用户名获取uid, 用getpwnam

NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry  
SYNOPSIS
#include <sys/types.h>
#include <pwd.h>

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 <pwd.h> 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 */
};

作者: bluejays   发布时间: 2011-10-10

getpwent()直接获取passwd结构体,其中的pw_name,pw_uid

作者: fangang001   发布时间: 2011-10-10

热门下载

更多