The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 4 03-05-2009 04:25 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 04:30 PM
entry in /etc/group too long - problem using sudo with %group poli SUN Solaris 4 12-21-2004 09:50 AM
AIX Unix.. number of users on system in a particular group afiore UNIX for Advanced & Expert Users 1 06-26-2003 11:19 AM
NIS group vs local group vjsony UNIX for Dummies Questions & Answers 3 05-19-2003 09:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-27-2008
Andrewkl Andrewkl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 14
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
  #2 (permalink)  
Old 09-27-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
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 01:45 PM..
  #3 (permalink)  
Old 09-27-2008
nullwhat nullwhat is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 25
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
  #4 (permalink)  
Old 10-25-2008
Andrewkl Andrewkl is offline
Registered User
  
 

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

--Andrew
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0