I am trying to get a C string of the owner and group of a file, After I do a stat()
I get the user ID and group ID, but how do I get the name?
On some versions of UNIX, typing ls -l shows you who owns the file, but not the name of the group that the file belongs to. To see the name of the group, run ls -lg on the file.
If you have a file opened in nano and need to find a particular string, there's no need to exit the file and use grep on it. Just press Ctrl + W on your keyboard, type the search string, and hit Enter .
A. You can use ls -l command (list information about the FILEs) to find our the file / directory owner and group names. The -l option is known as long format which displays Unix / Linux / BSD file types, permissions, number of hard links, owner, group, size, date, and filename.
You can use getgrgid()
to get the group name and getpwuid()
to get the user name:
#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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With