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

In this assignment, you are to write a C program – let\'s call it myls.c – that

ID: 3685697 • Letter: I

Question

In this assignment, you are to write a C program – let's call it myls.c – that mimics the ls command.

Your program should implement the following ls options:

1. ls with no option: list the names of files and directories in the current directory

2. ls -i option: list the names and i-node numbers of files and directories in the current directory

3. ls -l option: list the names of files and directories in long format

4. ls -R option: recursively list the names of all files and directories in the current directory; that is, the command is also applied to the directories within the current directory, the directories within those directories, and so forth Make sure your executable program is named myls and not ls so that there's no confusion over which version of the program you are running. An example run is shown on the next page. If you're not familiar with these options, you should familiarize yourself with them. Also, you should look at the Linux man pages for their exact descriptions.

The following output assumes that your current working directory is Sample with the above tree structure:

Your program should not use fork/exec, but instead use the actual file system calls themselves:

• opendir() – open a directory

• readdir() – read a directory entry

• closedir() – close a directory

• rewinddir() – reset directory

• getcwd() – get current working directory

• chdir – change current working directory

• stat(), lstat(), fstat() – obtain file information

Hint: When implement the “-l” option, the line “total ##” that is displayed is the number of blocks that the listed files and directories take up on the disk in 1K blocks. The stat.st_blocks gives the number of 512B blocks allocated, which is double the number that we want (since we want 1K blocks), so for the value return stat.st_blocks/2 to get the correct number.

Explanation / Answer

Answer for Question:

This below c program will implemented the ls command option
See the below c code..
it used the opendir, readdir, closedir like system calls.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char* argv[])
{
DIR *mydir;
struct dirent *myfile;
struct stat mystat;

mydir = opendir(argv[1]);
while((myfile = readdir(mydir)) != NULL)
{
stat(myfile->d_name, &mystat);
printf("%d",mystat.st_size);
printf(" %s ", myfile->d_name);
}
closedir(mydir);
}