2011-09-07 90 views

回答

22

使用getpwuid()getgrgid()

#include <pwd.h> 
#include <grp.h> 
#include <sys/stat.h> 

struct stat info; 
stat(filename, &info); // Error check omitted 
struct passwd *pw = getpwuid(info.st_uid); 
struct group *gr = getgrgid(info.st_gid); 

// If pw != 0, pw->pw_name contains the user name 
// If gr != 0, gr->gr_name contains the group name 
+3

爲了完整注意到*「getpwnam()和getpwuid()搜索給定的登錄名或UID用戶的密碼數據庫,分別爲**總是返回**遇到的第一個」 *(強調),因爲一個UID可以與多個用戶名關聯(認爲這通常是皺眉)。 – dmckee

+0

公平評論。我的一個生活的詛咒的是本地組文件具有GID 1234不同名稱的多個條目。它傾向於指'getgrent()'來查找用戶'jdoe'是否實際上是組1234的成員。 –

相關問題