0
嗨,我試圖列出所有具有與您的用戶ID相匹配的組ID的用戶名。我正在考慮使用getpwent,但似乎沒有得到它的權利似乎得到一個無限循環,我不知道家裏只過濾出相同的組名。獲取用戶組ID然後打印具有相同組ID的用戶列表
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdio.h>
int main(){
uid_t myId;
myId = getuid();
struct passwd *pPwdInfo = NULL;
pPwdInfo = getpwuid(myId);
if((pPwdInfo = getpwuid(myId)) != NULL){
int gId = pPwdInfo->pw_gid;
struct passwd *pwd_entry = NULL;
pwd_entry = getpwent();
setpwent(); // go to the top of /etc/passwd
while(pwd_entry){
printf("Username: %s\n", pwd_entry->pw_name);
printf("Password: %s\n", pwd_entry->pw_passwd);
printf("User Id: %d\n", pwd_entry->pw_uid);
printf("Group Id: %d\n", pwd_entry->pw_gid);
printf("User info: %s\n", pwd_entry->pw_gecos);
printf("Home Directory: %s\n", pwd_entry->pw_dir);
printf("Shell Program: %s\n", pwd_entry->pw_shell);
}
endpwent();
}else{
}
return 0;
}
旁註:有什麼用? UID和GID之間沒有必要的關聯,即使您有用戶組。 – Olaf