Hello i want to make a program in c that recieves the name of a directory as an argument, show all files in it and their permissions in rwx format, here's the code:
DIR* midir;
if ((midir=opendir(argv[1])) < 0) {
perror("\nError en opendir\n");
exit(-1);
}
struct dirent* info_archivo;
struct stat fileStat;
while ((info_archivo = readdir(midir)) != NULL) {
stat(info_archivo->d_name, &fileStat);
printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf((fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
}
closedir(midir);
the output is wrong it gets the files name right but the permissions wrong:
..: permissions: drwxr-xr-x
file1: permissions: drwxr-xr-x
file3: permissons: drwxr-xr-x
.: permissions: drwxr-xr-x
file2: permissions: drwxr-xr-x
Help is appreciated.