Re: File ownership



On Fri, 2 Mar 2001, Marco Quezada wrote:

Hi,

Does anyone know of a function call that will return the text name of
the owner of a file? stat() returns the numeric UID but not the name of
the user.
<...>

You can look up the numeric UID in the passwd file to get the username,
use the function getpwuid(3):

#include <sys/types.h>
#include <pwd.h>

...

struct passwd *pwent;
struct stat stbuf;

...

/* run stat() to fill stbuf */

pwent = getpwuid(stbuf.st_uid);
if(pwent)
  /* the username is in pwent->pw_name */
else
  /* UID is not in passwd file, print the numeric UID instead */

Similarly, getgrgid(3) can be used to get the group name for a GID.

Note that both return pointers to static storage, which is overwritten on
the next call (so keep a copy if necessary).  According to the man page
(on FreeBSD) they work with NIS as well.

Jonathan







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]