I'm trying to find all groups to which user belongs in my UNIX system, and that for each user.Implementation has to be in C. Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
static void error_fatal(char* msg)
{ perror(msg); exit(EXIT_FAILURE); }
int main(int argc, char** argv) {
struct group* grp;
struct passwd* pwd;
char *name;
int i = 0;
setpwent();
while((pwd = getpwent()) != NULL){
if( ( name = (char*) malloc( (strlen(pwd->pw_name)+1)*sizeof(char))) == NULL ) error_fatal("malloc");
strcpy(name, pwd->pw_name);
printf("%s:\n", name);
setgrent();
while( (grp = getgrent()) != NULL ) {
for( i=0; i < (sizeof(grp->gr_mem)/sizeof(grp->gr_mem[0])); i++ ){
if( /*strlen(&grp->gr_mem[i][0]) == strlen(name) && */ !strcmp(grp->gr_mem[i], name) )
printf("%s\n", name);
} }
endgrent();
free(name);
}
endpwent();
return 0;
}
But I get segmentation fault after "root:" output. I'm pretty sure the problem is in accessing list of members in the fourth field of /etc/group file (see man 5 group for details).
So, basically my problem would be to find out how many members each group has, so my counter(i in program, the last for loop) would have nice upper boundary.