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

Write a C program that mimics the ls command. Your program should implement the

ID: 3685745 • Letter: W

Question

Write a C program 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...

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

filel file2 Research Teaching % myls -i 50652508 filel 50652507 file2 50652505 Research 50652506 Teaching myls -1 total 16 -rw-1 joe smith 615 Mar 11 15:27 filel -rw-1 joe smith 1882 Mar 11 15:27 file2 drwx-2 joe smith 4096 Mar 11 16:44 Research drwx-2 joe smith 4096 Mar 11 16:45 Teaching % myls -R filel file2 Research Teaching . /Research: paperl paper2 paper3 ./Teaching: filel paperl

Explanation / Answer

/*
* C Program to List Files in Directory
*/
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
char *cwd = getcwd( NULL, 0 );
struct stat buffer;
int status;
  
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s ", dir->d_name);
if (stat(dir->d_name, &statbuf) == -1)
continue;
status = fstat(dir->d_name, &buffer);
}
iterate(dir);

rewinddir(dir);

iterate(dir);

status = lstat(dir->d_name, &buffer);

closedir(d);
}
listdir(d, 0);

chdir(d);
return(0);
}
void listdir(const char *name, int level)
{
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;

do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%*s[%s] ", level*2, "", entry->d_name);
listdir(path, level + 1);
}
else
printf("%*s- %s ", level*2, "", entry->d_name);
} while (entry = readdir(dir));
closedir(dir);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote