Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program needs to be programmed in C. Please show screen shot of terminal. Write

ID: 3721057 • Letter: P

Question

Program needs to be programmed in C. Please show screen shot of terminal.

Write a basic file managment system with following capabilities. Suppose the name of the program is myFS. When it executes as below: Figure 6: First command format The program should provides the file listing of the given directory. The program should list the filename, file size, and time last modified for each file within the directory. The list is shown below: rwxr-xr-x 1 mamal None drwxr-xr-x+ 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None drwxr-xr-x+ 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None rwxr-xr-x 1 mamal None 62645 Jan 19 2017 a.exe 0 Jan 15 2017 bin 171 Feb 27 2017 bingo. c 62848 Feb 27 2017 bin exe 187 Feb 28 2017 buffer.c 62657 Feb 28 2017 buffer. exe 0 Jan 15 2017 build-gcc 891 Jan 24 2017 crash1.exe.stackdump 885 Jan 24 2017 crash2. exe. stackdump 110 Jan 19 2017 crash1.c 62645 Jan 24 2017 crash1. exe 131 Jan 19 2017 crash2.c 63157 Jan 24 2017 crash2. exe Figure 7: Files list with their properties. The program should provide the user with the option to sort the listing based on filename, on file size, or on time last modified. The program should run once and quit (it does not loop). The syntax (usage for the program should be where -s indicates sort by file size and -t indicates sort by time last modified. For example s myFS 1s-1 -t /home

Explanation / Answer

//C Program for file mangament system

#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#define FILE_SIZE 255

struct FileSt
{
char name[32];
int size;
};

void moveFile(char *cmd, char *srcfile, char* desfile)
{

FILE *fsrc,*fdes;
char c;
fsrc=fopen(srcfile,"r");
if(fsrc == NULL)
{
fprintf(stderr,"File not found");
}
else
{
fdes = fopen(desfile,"w");
if(fdes == NULL)
{
fprintf(stderr,"Copy failed..!");
}
else
{
c = fgetc(fsrc);
while (c != EOF)
{
fputc(c, fdes);
c = fgetc(fsrc);
}
}
close(fsrc);
close(fdes);
}

remove(srcfile);

}

// Copy File Program

./fs touch filename

void touchFile (char* cmd, char *srcfile)

{

File *fp = fopen(srcfile,"w");

if(fp==NULL)

printf("touch failed');

close(fp);

}

// copy file name

void copyFile(char *cmd, char *srcfile, char* desfile)
{

FILE *fsrc,*fdes;
char c;
fsrc=fopen(srcfile,"r");
if(fsrc == NULL)
{
fprintf(stderr,"File not found");
}
else
{
fdes = fopen(desfile,"w");
if(fdes == NULL)
{
fprintf(stderr,"Copy failed..!");
}
else
{
c = fgetc(fsrc);
while (c != EOF)
{
fputc(c, fdes);
c = fgetc(fsrc);
}
}
close(fsrc);
close(fdes);
}

}

// list file program

void lsfiles(char *dirName, int count , char *argv[])
{
struct dirent *de; // Pointer for directory entry
struct stat st;
char fname[32];
struct FileSt sortlist[100];
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(dirName);

if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
}
else
{
// for readdir()
int it=0;

while ((de = readdir(dr)) != NULL)
{
if(stat(de->d_name, &st) != 0) {}
else
{
int status=0;
// sortlist.size = (int) st.st_size;
// strcpy (sortlist.name,de->d_name);
for(it=2;it < count-1;it++)
{
if(strcmp(argv[it], "-l") == 0)
{
fprintf(stderr,"%10s %10d %s ",de->d_name, (int) st.st_size, ctime(&st.st_atime));
status = 1;
}
else if(strcmp(argv[it], "-s") == 0)
{

fprintf(stderr,"%10s %10d %s ",de->d_name, (int) st.st_size, ctime(&st.st_atime));
status = 1;
}

else if(strcmp(argv[it], "-t") == 0)
{

fprintf(stderr,"%10s %10d %s ",de->d_name, (int) st.st_size, ctime(&st.st_atime));
status = 1;

}

}
if(status == 0 )
{
fprintf(stderr,"%s ",de->d_name);
}
}
}
}

closedir(dr);
}

// Main Program

int main(int argc, char *argv[])
{
int i=0;
char cmd[10];
char dirName[FILE_SIZE];
strcpy(cmd,argv[1]);
if(strcmp(cmd,"cp") == 0)
{
copyFile(cmd,argv[argc-2],argv[argc-1]);
}
else
{
strcpy(dirName,argv[argc-1]);
lsfiles(dirName,argc,argv);
}

// move and touch also supported
return 0;
}