ace
ace

Reputation: 2161

How to you find out what group the current user belongs to via c++?

Using my c++ program how can I find out what group the current user running my program belongs to? So my program need to figure out a couple of things :

  1. The current username of the user
  2. The group the user belongs to

How can do the above 2 using c++ on a RedHat / Linux machine?

Upvotes: 0

Views: 3547

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753675

You can find some of the information via getgid() (real GID) and getegid() (effective GID). For the other auxilliary groups, you need to use getgroups().

In practice, the real and effective GID are normally the same, but it is the effective GID that is used when creating a file. Usually, the group list returned by getgroups() includes the real group - though it is not clear that it actually has to do so.

Upvotes: 3

Chris Dodd
Chris Dodd

Reputation: 126203

You use getuid(2) and getgid(2) to get the numeric user and group ids, then use getpwuid(3) and getgrgid(3) to look up those ids in the user/group databases and turn them into text names.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

With getuid(2) and getgid(2). See credentials(7) for more information.

Use getpwuid(3) and getgrgid(3) for the names.

Upvotes: 5

Related Questions