Created
August 25, 2024 08:03
-
-
Save mortymacs/b61648eede3ba1418a4cc0858902aa1d to your computer and use it in GitHub Desktop.
Get GNU/Linux user group in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pwd.h> | |
#include <grp.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
struct passwd *pwd; | |
struct group *grp; | |
if(argc <= 1) | |
{ | |
printf("Add user as second argument\n"); | |
exit(1); | |
} | |
pwd = getpwnam(argv[1]); | |
if (pwd == NULL) | |
{ | |
printf("Not found user!\n"); | |
} | |
else | |
{ | |
grp = getgrgid(pwd->pw_gid); | |
printf("User Info->\nName: %s\nGroup: %s\nHome: %s\nShell: %s\n", | |
pwd->pw_name, | |
grp->gr_name, | |
pwd->pw_dir, | |
pwd->pw_shell | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment