how to user C language to convert a known user id to a user name

how to user C language to convert a known user id to a user name

Post by Davi » Wed, 30 Apr 2003 00:18:31



Hi Everyone,

I am writing a progam that can find information for a file by using
stat() func. Although I can get user id and group id from this
function.I am trying to find if there's a function in C that can help
convert a known user id to a user name in Linux OS?And how to convert
a known group id to the group name? If there is no such a function,
can I write one and how to do it?
Thanks a lot!

David

 
 
 

how to user C language to convert a known user id to a user name

Post by Tony Curti » Wed, 30 Apr 2003 00:22:36


>> On 28 Apr 2003 08:18:31 -0700,

> Hi Everyone, I am writing a progam that can find
> information for a file by using stat() func. Although I
> can get user id and group id from this function.I am
> trying to find if there's a function in C that can help
> convert a known user id to a user name ...

getpwuid()

Quote:> And how to convert a known group id to the group name?

getgrgid()

hth
t

 
 
 

how to user C language to convert a known user id to a user name

Post by Lew Pitche » Wed, 30 Apr 2003 00:28:54



> Hi Everyone,

> I am writing a progam that can find information for a file by using
> stat() func. Although I can get user id and group id from this
> function.I am trying to find if there's a function in C that can help
> convert a known user id to a user name in Linux OS?

getpwuid(3), and reference the *pw_name field of the returned structure.

Quote:>And how to convert
> a known group id to the group name?

getgrgid(3), and reference the *gr_name field of the returned structure

Quote:> If there is no such a function,
> can I write one and how to do it?

For userid/username values, you can use the
   getpwent(3) set of functions, or open and read the /etc/passwd file

For groupid/groupname values, you can use the
   getgrent(3) set of functions, or open and read the /etc/group file

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)