![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
thanks for the responses. I am able to get what i need now.
--Andrew |
|||
| Google The UNIX and Linux Forums |