List Files: Write a C program that lists files in the given directory. When call
ID: 3833533 • Letter: L
Question
List Files: Write a C program that lists files in the given directory. When called without any arguments, the program should just print the file names. When invoked with the -l flag, the program should print out information about each file, such as the owner, group, permissions, and other information obtained from the stat() system call. The program should take one additional argument, which is the directory to read, e.g., myls -l directory. If no directory is given, the program should just use the current working directory. Useful interfaces: stat(), opendir(), readdir(), getcwd().
Your code should take a directory name argument on the command line, or if no command-line argument is provided, it should use the current working directory. Here is an example of how your code should work:
$ ls
Makefile myls myls.c tempdir
$ ./myls
tempdir
.
Makefile
..
myls
myls.c
$ ls tempdir
baz.txt
$ ./myls tempdir
.
..
baz.txt
$ ls .
Makefile myls myls.c tempdir
$ ./myls .
tempdir
.
Makefile
..
myls
myls.c
$
The files can be listed in any order, and it will be easiest if you output ‘.’ and ‘..’. For extra credit, also implement the ‘-l’ flag. When this flag is used, your code should output owner permissions, inode, and name of each file, like this:
$ ./myls -l
rwx 1844983 tempdir
rwx 1844964 .
rw 1844980 Makefile
rwx 1065059 ..
rwx 1844977 myls
rw 1844986 myls.c
$ ./myls -l tempdir
rwx 1844983 .
rwx 1844964 ..
rw 1844985 baz.txt
$ ./myls -l .
rwx 1844983 tempdir
rwx 1844964 .
rw 1844980 Makefile
rwx 1065059 ..
rwx 1844977 myls
rw 1844986 myls.c
$
Note the suggestions in the OSTEP question about which system calls to use (such as getcwd). Your code should run correctly on the hosting machine.
Hint: you only need stat() if you support the -l option.
You code should be named ‘myls.c’ and should be compiled like this:
$ gcc -o myls myls.c
Explanation / Answer
Code with the comments explaining it:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char* argv[])
{
DIR *dir;
struct dirent *file;
struct stat fileStat;
int longForm = 0;
char *dirName;
if (argc == 1) { // no arguments specified
dirName = "."; // default to current directory
}
else if (argc == 2) { // one argument
// check if its "-l" option or a directory
if(strcmp(argv[1], "-l") == 0) { // "-l" option
longForm = 1;
dirName = ".";
}
else { // directory
dirName = argv[1];
}
} else { // both option and directory
longForm = 1;
dirName = argv[2];
}
// get the files of the directory
dir = opendir(dirName);
while((file = readdir(dir)) != NULL)
{
if (longForm == 1) {
stat(dirName, &fileStat);
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "");
printf(" %llu", fileStat.st_ino);
}
printf(" %s ", file->d_name);
}
closedir(dir);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.