In: Computer Science
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
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...