-1

I am having a bit of trouble creating a C program that reads the current directory, prints the file path, and contents.

For each file found in the directory the contents should be printed based on whether they are a directory, a file, or executable.

I have the main components working just unsure how to sort the files output after using the opendir() / closedir() command

eg. end output:

/home/documents/folder1
File:       help.txt
File:       me.txt
Executable: plz
File:       thankyou.c  

Current code:

struct dirent *de;  // Pointer for directory entry

// opendir() returns a pointer of DIR type.
DIR *dir = opendir(".");//opens current direcotry

if (dir == NULL)  // opendir returns NULL if couldn't open directory
{
    printf("ERROR: Could not open current directory" );
    return 1;
}

// for readdir()
while ((de = readdir(dir)) != NULL){

      //if (Executable){}
      //else if (File){}
      //else if (Directory){}
        printf("%s\n", de->d_name);
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    ***Show your work*** – abelenky Aug 18 '18 at 07:11
  • sorry just updated – player_unknown Aug 18 '18 at 07:18
  • "a directory, a file, or executable" - directories and files can have the execute access bit set, what do you mean by this? – cdarke Aug 18 '18 at 07:21
  • When the user starts the shell, it should print the filenames of all the files in the current directory, except hidden files. For each filename, also print whether it is a directory, executable program, or ordinary file. – player_unknown Aug 18 '18 at 07:24
  • Possible duplicate of [How can I get the list of files in a directory using C or C++?](https://stackoverflow.com/q/612097/608639), [How to list files in a directory in a C program?](https://stackoverflow.com/q/4204666/608639), [How do you get a directory listing in C?](https://stackoverflow.com/q/12489/608639), etc. – jww Aug 18 '18 at 10:29

1 Answers1

1

You can use d_type from struct dirent to check the type of the file and access with X_OK to check whether file is executable.

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>

int main()
{
    DIR *dir;
    struct dirent *dp;
    char * file_name;
    dir = opendir(".");
    while ((dp=readdir(dir)) != NULL) {
        if ( !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..") )
        {
            // do nothing (straight logic)
        } else {
            file_name = dp->d_name; // use it
            if (access(file_name, X_OK) != -1) {
             printf("executable:");
            }
            else if (dp->d_type == DT_DIR)
            {
               printf("directory:");
            }
            else if(dp->d_type == DT_REG)
            {
               printf("file:");
            }
            printf("     \"%s\"\n",file_name);
        }
    }
    closedir(dir);
    return 0;
}
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • 2
    This is one of those questions that has been asked and answered so many times it is hard to avoid an existing answer. It is better to close it as a duplicate. Existing answers provide a richer body of knowledge. For example, your program is not sensitive to hard and soft links, it fails to answer the question of executable, etc. – jww Aug 18 '18 at 10:31