Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main(int argc, char const *argv[])
- {
- char directory[PATH_MAX];
- char path[PATH_MAX];
- DIR *dir;
- struct dirent *ent;
- struct stat buffer;
- int status;
- int can_read;
- printf("Please enter directory name: ");
- gets(directory);
- strtok(directory, "\n");
- printf("Directory: %s \n", directory);
- if ((dir = opendir (directory)) != NULL) {
- /* print all the files and directories within directory */
- while ((ent = readdir (dir)) != NULL) {
- if ( !strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..") ) {
- // do nothing
- } else {
- sprintf(path, "%s/%s", directory, ent->d_name);
- status = lstat(path, &buffer);
- can_read = euidaccess(path, R_OK||X_OK);
- if(!S_ISDIR(buffer.st_mode) && can_read == 0) {
- printf ("%s\n", ent->d_name);
- }
- }
- }
- closedir (dir);
- } else {
- /* could not open directory */
- perror ("");
- return(0);
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment