Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Created December 25, 2013 19:11
Show Gist options
  • Save luqmaan/e9b0269754b32d3f2ab3 to your computer and use it in GitHub Desktop.
Save luqmaan/e9b0269754b32d3f2ab3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/dir.h>
int main(int argc, char **argv) {
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(" %s", info_archivo->d_name);
printf("\n");
}
closedir(midir);
}
@luqmaan
Copy link
Author

luqmaan commented Dec 25, 2013

ouptut

drwxr-xr-x .
drwxr-xr-x ..
-rwxr-xr-x fileperms
-rw-r--r-- fileperms.c

ls -l

total 16
drwxr-xr-x+  4 luqmaan staff  136 Dec 25 14:09 .
drwxr-xr-x+ 22 luqmaan staff  748 Dec 25 14:07 ..
-rwxr-xr-x+  1 luqmaan staff 8824 Dec 25 14:09 fileperms
-rw-r--r--+  1 luqmaan staff 1099 Dec 25 14:09 fileperms.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment