Basically my code looks like this:
int main(int ac, char **av) {
char *dir_name = get_dir_name(ac, av);
DIR *dir;
struct dirent *entry;
t_stat rd_stat;
dir = opendir(dir_name);
if (!dir) {
perror("diropen");
exit(EXIT_FAILURE);
}
t_group *group_info;
while ((entry = readdir(dir)) != NULL) {
if (lstat(entry->d_name, &rd_stat) == -1) {
perror("lstat");
exit(EXIT_FAILURE);
}
if ((group_info = getgrgid(rd_stat.st_gid)) == NULL) {
perror("getgrgid() error");
}
printf(%s\n", group_info);
}
closedir(dir);
}
And i'm getting error while trying to decode group id to char* type:
getgrgid() error: Undefined error: 0
I have no idea why this doesn't work, because everything worked well with user id
CodePudding user response:
getgrgid() returns NULL when there's an error or when the group ID isn't found. You need to check errno to tell the difference.
Also, group_info is a structure, not a string, you can't print it with printf(). Get the gr_name member to print the group name.
if ((group_info = getgrgid(rd_stat.st_gid)) == NULL) {
if (errno) {
perror("getgrgid() error");
} else {
printf("Unnamed group %d\n", rd_stat.st_gid);
}
} else {
printf(%s\n", group_info.gr_name);
}
