Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the owner and group of a file (as a string)

Tags:

c

linux

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?

like image 365
Radu Avatar asked Oct 02 '11 01:10

Radu


People also ask

How do I find the group of a file?

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.

How do I search for a file in a string in Linux?

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 .

How do I find the owner of a file in Linux?

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.


1 Answers

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);
like image 194
makes Avatar answered Nov 09 '22 12:11

makes