Question

In: Computer Science

Using C language 0. (3pts.) Using opendir() and readdir(), open the current directory and output all...

Using C language

0. (3pts.)
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.)
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.

2. (3pts.)
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.

EX. (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

Solutions

Expert Solution

As per your question, The C 17 program is written perfectly and correctly as per your question requirement. Please have a look on the source code(please refer in-line comments for better understanding) and the run output below:-

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

//return only the extention of the passed full file name
char *get_filename_ext(const char *filename) {
    char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}

//setting color bold red
void bold_red () {
  printf("\033[1;31m");
}

//setting color light green
void light_green() {
  printf("\033[0;32m");
}

//setting color light blue
void light_blue() {
  printf("\033[0;34m");
}

//reset color
void reset () {
  printf("\033[0m");
}

int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;
 
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 1;
   else
      return 0;
}
int main()
{
    DIR *currentFolder; // creating directory DIR pointer
    struct dirent *entry; // pointer for directory entry
    struct stat filestat; // creating stat object variable
        // opendir() returns a pointer of DIR type. 
    currentFolder = opendir("."); 
    if(currentFolder == NULL)
    {
        perror("Unable to read directory");
        return(1);
    }

    // for readdir() 
    while( (entry=readdir(currentFolder)) )
    {
        stat(entry->d_name,&filestat);
        if( S_ISDIR(filestat.st_mode) )
        {
                light_blue();
                printf("./%s\n",entry->d_name);
                }
        else
        {
                char exe[] = "exe";
                char cpp[] = "cpp";
                char c[] = "c";
                char *extention = get_filename_ext(entry->d_name);
                size_t fileSize = (size_t)filestat.st_size;
                if(compare_string(extention, exe))
                        bold_red();
                else if(compare_string(extention, c) || compare_string(extention, cpp))
                        light_green();
                        else
                                reset();
                        printf("%s %i\n",entry->d_name, (int)fileSize);
                }
    }
    //closing the directory DIR pointer
    closedir(currentFolder);
    return(0);
}

Note (Optional): If required, please change the location of the current directory(".") to your desired directory location like "C:\\Users\\" .

=> The Run Output:-

Please don't forget to like it...

Thank You...


Related Solutions

Please code the program showing the output below. using C language 1. Using 'if' or 'while'...
Please code the program showing the output below. using C language 1. Using 'if' or 'while' or 'for' and 'break' statement / only using <stdio.h> A bC dEf GhIj KlMnO 2. a program that identifies the largest number that can be expressed in short. Using only loop (ex.for,if,while) and break statement only using <stdio.h>
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
List all files and directories in the current directory and store in a variable called malf....
List all files and directories in the current directory and store in a variable called malf. Count how many lines end with your firstname in the file /etc/conf Explain this command: mv pmu*.[ab]   cces/   Quick help in 5 min pleases with the UNIX program! I want only the final answer
4. a. Suppose Z~Normal(0,1). Find P(1<Z<2). (3pts) b. Suppose X~Normal(-2,1). Find P(X>0 or X<-3). (3pts) c....
4. a. Suppose Z~Normal(0,1). Find P(1<Z<2). (3pts) b. Suppose X~Normal(-2,1). Find P(X>0 or X<-3). (3pts) c. Suppose X~Normal(2,4). The middle 88% of the X values are between what two values? (3pts)
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description...
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description Tim was born in a leap year, so he would like to know when he could have his birthday party. Could you tell him? Given a positive integers Y indicating the starting year, and a positive integer N, your task is to tell the N-th leap year from year Y. Note: if year Y is a leap year, then the 1st leap year is...
Write a bash script to find all the files ending with .c recursively for every directory...
Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.
C program for copying data from one directory to another using multi threading for linux using...
C program for copying data from one directory to another using multi threading for linux using command line arrangement. Program will take two arguments: 1: Source 2: Destination it will copy all the files from source to destination recursively Deadline: 4 hours from now.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT