Operating System - Linux
1754019 Members
7368 Online
108811 Solutions
New Discussion юеВ

Re: get userlist from HPUX whose primary group is given

 
pjena
Occasional Contributor

get userlist from HPUX whose primary group is given

Hi,
I want to get the username list whose primary group is given on HPUX.
I used following program to get the username

main()
{
int i=0;
struct group* grp;
grp = getgrnam("adm_system");
if(grp)
{
printf("%s\n",grp->gr_name);
printf("%s\n",grp->gr_passwd);
printf("%d\n",grp->gr_gid);
while (grp->gr_mem[i] !=NULL)
{
printf("%s\n",grp->gr_mem[i])
i++;
}
}
}
But it is displaying the user list whose secondary group is "adm_system" and also tried with getgrgid(), but it doesn't give the proper result.

Please help me to get the user list whose primary group is "adm_system"

Regards,
Prasant
7 REPLIES 7
Victor BERRIDGE
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Hi

For example GID= bin :
cat /etc/passwd|grep ":2:"


All the best
Victor
Peter Godron
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Prasant,
thread http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=849713 shows how to get the reverse of the info without C.

So for your case,
get the gid for "adm_system" from /etc/group then look in /etc/passwd for this gid.
Darrel Louis
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Hi Prasant,

Have you tried the logins command:
# logins -g

For more info see man logins.

Darrel
Peter Godron
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Prasant:
#include
#include
main()
{
int i=0;
struct group* grp;
struct passwd* pwd;
grp = getgrnam("adm_system");
if(grp)
{
printf("Group Name %s\n",grp->gr_name);
printf("GRPID %d\n",grp->gr_gid);
while (grp->gr_mem[i] !=NULL)
{
printf("Users from Group file : %s\n",grp->gr_mem[i]);
i++;
}
while (pwd=getpwent())
{
if (pwd->pw_gid == grp->gr_gid) printf("Users from Password file : %s\n",pwd->pw_name);
}
}
}
Stefan Schulz
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Hi,

do you really need to programm it by yourselfe? There is a tool in HP-UX called "logins" that allready does what you want.

Do you just need the information? Then logins should be helpfool to you.

Or do you need a function in your own programs?

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Victor BERRIDGE
Honored Contributor

Re: get userlist from HPUX whose primary group is given

Sorry
I missed the fact you could me using NIS...
In this case as suggested the use of logins -g | grep should do the trick under HPUX


All the best
Victor
A. Clay Stephenson
Acclaimed Contributor

Re: get userlist from HPUX whose primary group is given

Bear in mind that you don't need to search the group file (or map) for primary group membership. In fact, the presense of a login in the group files means nothing as it applies to primary group membership. Primary group member ship is determined solely by the GID value in the user's passwd entry. The only reason to consult the group file (or map) is to obtain the GID of the group. You then scan the passwd file (or map) for matching GID entries.

The standard libc passwd and group file functions will automatically include the NIS data, if applicable.


#include
#include
int main()
{

struct group *grp;

grp = getgrnam("adm_system");
if(grp != NULL)
{
struct passwd *pwd = NULL;

printf("Group Name %s\n",grp->gr_name);
printf("GRPID %d\n",grp->gr_gid);

do
{
pwd = getpwent;
if (pwd != NULL)
{
if (pwd->pw_gid == grp->gr_gid) printf("%s\n",pwd->pw_name);
}
}
while (pwd != NULL);
}
return(0);
}
If it ain't broke, I can fix that.