Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created August 25, 2024 08:03
Show Gist options
  • Save mortymacs/b61648eede3ba1418a4cc0858902aa1d to your computer and use it in GitHub Desktop.
Save mortymacs/b61648eede3ba1418a4cc0858902aa1d to your computer and use it in GitHub Desktop.
Get GNU/Linux user group in C
#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