+1 (315) 557-6473 

Create A Program to Create A List Directory Program Under Linux That Displays Different File Types In Different Colours In C Assignment Solution.


Instructions

Objective
Write a program to to create a list directory program under Linux that displays different file types in different colours in C.

Requirements and Specifications

Using opendir() and readdir(), open the current directory and output all filenames until there are no more. Include a main that does this below.
  1. (4pts.)
  2. Parse the dirent struct to see if an entry is a directory or a file. If it is a directory, prepend "./" to the filename before printing it out. Include a main that does this below.

  3. (3pts.)
  4. Open a file handle to each file, use lseek to determine the file's size in bytes and print out the file's size in bytes next to its name. Include a main that does this below.

  5. (3pts.)
Add a color formatting element. Print out executable binaries in bold red, regular files in light green, directories in light blue and anything else in white. Be careful - changing the console printout color is a durable operation that is global in scope, so once you change the color, anything printed from anywhere in your Process will be in in that color. It is a good practice to always presume the color is incorrectly set before you print and to set it to what you want before you print anywhere (especially in informational or error outputs). You will also need to find a way to determine if a file is executable or not.
Make sure the code is original and must be done in C programming language. This is due October 21 at 11:59pm. Please send me a zip file.
Screenshots of output
Create a list directory program under Linux that displays different file types in different colour in C

Source Code

Program 0

#include <stdio.h>

#include <dirent.h>

int main()

{

    DIR *dir;

    struct dirent *dirent;

    dir = opendir("."); /* open current directory */

    if (dir == NULL)

    {

        printf("Error opening current directory\n");

        return 1;

    }

    printf("Filenames:\n\n");

    /* read all files in directory */

    while ((dirent = readdir(dir)) != NULL)

    {

        printf("%s\n", dirent->d_name);

    }

    printf("\n");

    closedir(dir);

    return 0;

}

Program 1

#include <stdio.h>

#include <dirent.h>

int main()

{

    DIR *dir;

    struct dirent *dirent;

    dir = opendir("."); /* open current directory */

    if (dir == NULL)

    {

        printf("Error opening current directory\n");

        return 1;

    }

    printf("Filenames:\n\n");

    /* read all files in directory */

    while ((dirent = readdir(dir)) != NULL)

    {

        if (dirent->d_type == DT_DIR)

            printf("./"); /* append ./ to directories */

        printf("%s\n", dirent->d_name);

    }

    printf("\n");

    closedir(dir);

    return 0;

}

Program 2

#include <stdio.h>

#include <dirent.h>

#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

int main()

{

    DIR *dir;

    struct dirent *dirent;

    int fd;

    off_t filesize;

    dir = opendir("."); /* open current directory */

    if (dir == NULL)

    {

        printf("Error opening current directory\n");

        return 1;

    }

    printf("Filename\tSize\n\n");

    /* read all files in directory */

    while ((dirent = readdir(dir)) != NULL)

    {

        /* print only files and directories */

        if (dirent->d_type == DT_DIR)

            printf("./"); /* append ./ to directories */

        printf("%s", dirent->d_name); /* always print name */

        if (dirent->d_type == DT_REG) {

            fd = open(dirent->d_name, O_RDONLY); /* open read only */

            if (fd != -1)

            {

                filesize = lseek(fd, 0, SEEK_END); /* move pointer to end of file */

                close(fd);

                printf("\t%ld", filesize);

            }

        }

        printf("\n");

    }

    printf("\n");

    closedir(dir);

    return 0;

}

Program 3

#include <stdio.h>

#include <dirent.h>

#include <sys/types.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/stat.h>

int main()

{

    DIR *dir;

    struct dirent *entry;

    int fd;

    off_t filesize;

    struct stat st;

    dir = opendir("."); /* open current directory */

    if (dir == NULL)

    {

        printf("Error opening current directory\n");

        return 1;

    }

    printf("Filename\tSize\n\n");

    /* read all files in directory */

    while ((entry = readdir(dir)) != NULL)

    {

        /* print only files and directories */

        if (stat(entry->d_name, &st) != -1) /* read file information */

        {

            switch (st.st_mode & S_IFMT) /* get file type */

            {

            case S_IFDIR:

                printf("\x1B[34m"); /* set to light blue */

                break;

            case S_IFREG:

                if (st.st_mode & S_IXUSR) /* if executable */

                    printf("\x1B[1;31m"); /* set to bold red */

                else

                    printf("\x1B[92m"); /* set to light green */

                break;

            default:

                printf("\x1B[37m"); /* set to white */

            }

        }

        else /* unable to get stat */

            printf("\x1B[37m"); /* set to white */

        if (entry->d_type == DT_DIR)

            printf("./"); /* append ./ to directories */

        printf("%s", entry->d_name); /* always print name */

        if (entry->d_type == DT_REG) {

            fd = open(entry->d_name, O_RDONLY); /* open read only */

            if (fd != -1)

            {

                filesize = lseek(fd, 0, SEEK_END); /* move pointer to end of file */

                close(fd);

                printf("\t%ld", filesize);

            }

        }

        printf("\x1B[0m"); /* reset color */

        printf("\n");

    }

    printf("\n");

    closedir(dir);

    return 0;

}