1755140 Members
3022 Online
108830 Solutions
New Discussion юеВ

Getgid function

 
SOLVED
Go to solution
Krishnan Viswanathan
Frequent Advisor

Getgid function

I am using a c program to get the group id of the calling process, but the problem is I need to check for one of the secondary group ids (and not the primary gid).

Is there any function which can accomplish this ?

Thanks
2 REPLIES 2
Tim Clemens
New Member
Solution

Re: Getgid function

See if getgrent solves your problem.
man 1 getgrent

Looks like successive calls return the next gid.

Good luck!
A. Clay Stephenson
Acclaimed Contributor

Re: Getgid function

Actually the getgrent function alone does nothing except return the next group entry in the file. It tells you nothing about whether this user is a member of a given group. What you actually have to do is examine each gr_mem value looking for a string match on the user name.

I attached a small piece of code I yanked from one of my programs that will be fairly close; it also has a small main to illustrate it's use. It should be very easy to modify it to return the gid when a hit is found. This will search first the user's primary group and then
look through /etc/groups for a matching login.

Use it like this:

rslt = checkgrp("this_user","this_group",FALSE);

rslt will be set to 0 if "this_user" is a member of "this_group"); (FALSE says no verbose output on error).


Compile the test program like this:
cc -Ae checkgrp.c -o checkgrp after modifying it with sample values to fit your system.


If it ain't broke, I can fix that.