The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing root group to group from other mjkroner SUN Solaris 3 07-03-2008 07:39 AM
Monkcast #12: IBM HW group OEMs Solaris to chagrin of SW group & a ... - ZDNet.com bl iBot UNIX and Linux RSS News 0 08-17-2007 01:30 PM
entry in /etc/group too long - problem using sudo with %group poli SUN Solaris 4 12-21-2004 06:50 AM
AIX Unix.. number of users on system in a particular group afiore UNIX for Advanced & Expert Users 1 06-26-2003 08:19 AM
NIS group vs local group vjsony UNIX for Dummies Questions & Answers 3 05-19-2003 06:54 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-27-2008
Registered User
 

Join Date: Mar 2008
Posts: 7
retrieving all group names with a given group number

hi,

which Unix/C function can i use to retrieve all group names with a particular group id?

The following C code prints out the group id number of a particular group name:
------------------------------------------------------------------------
#include <stdio.h>
#include <grp.h>

int main(int argc, char * argv[]){
struct group * info;
char ** members;
int i=1;
if(argc < 2){
fprintf(stderr, "usage: %s groupname\n", argv[0]);
exit(-1);
}
info = getgrnam(argv[1]);
if(info == NULL){
printf("%s: no such group\n", argv[1]);
}
else{
printf("group name: %s\n", info -> gr_name);
printf("group id number: %d\n", info -> gr_gid);
}
}
------------------------------------------------------------------

% a.out vulcan1
group name: vulcan1
group id number: 15100

% a.out vulcan2
group name: vulcan2
group id number: 15100

% a.out vulcan3
group name: vulcan3
group id number: 15100

///////////////////////////////////////////////////
I'm looking for a C function that returns "vulcan1", "vulcan2", and "vulcan3"
when I pass in the group id of 15100 (there are only 3 groups with group id of 15100).


Thanks
--Andrew
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
You were nearly there. All you had to do was walk the array of pointers (**gr_mem) to the individual users.
To use a group id instead of a group name, replace getgrnam() with getgrgid()

Code:
#include <stdio.ho
#include <stdlib.h>
#include <grp.h>

int main(int argc, char * argv[])
{
   struct group *info;
   char **members;

   if (argc < 2) {
      fprintf(stderr, "usage: %s groupname\n", argv[0]);
      exit(1);
   }

   if ((info = getgrnam(argv[1])) == (struct group *)NULL) {
       printf("%s: no such group\n", argv[1]);
       exit(2);
   }

   printf("group name: %s\n", info->gr_name);
   printf("group id number: %d\n", info->gr_gid);
   members = info->gr_mem;
   while (*members)
      printf ("group member: %s\n", *members++);
}
~

Last edited by fpmurphy; 09-27-2008 at 10:45 AM.
Reply With Quote
  #3  
Old 09-27-2008
Registered User
 

Join Date: Sep 2008
Posts: 22
or you could use lsuser and grep out what ever you want.


for example
lsuser ALL | grep goups=struct | awk '{print $1 " " $2 " " $3}'

see man lsuser
Reply With Quote
  #4  
Old 10-25-2008
Registered User
 

Join Date: Mar 2008
Posts: 7
thanks for the responses. I am able to get what i need now.

--Andrew
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0