Search for the owner and group of files (as a string)

I am trying to get owner line C and file group. After I do stat(), I will get the user id and group id, but how do I get the name?

+5
source share
2 answers

You can use getgrgid()to get the group name and getpwuid()to get the username:

#include <pwd.h>
#include <grp.h>

/* ... */

struct group *grp;
struct passwd *pwd;

grp = getgrgid(gid);
printf("group: %s\n", grp->gr_name);

pwd = getpwuid(uid);
printf("username: %s\n", pwd->pw_name);
+6
source
+3
source

All Articles