Task 3 (20 points) Modify myls.cto implement a simple ls program. Name your file
ID: 3774140 • Letter: T
Question
Task 3 (20 points) Modify myls.cto implement a simple ls program. Name your filels.c. Your program will take in a path to a directory and list the content of that directory (file name only). If no path is given, list the content of the current working directory. Your program should work like this. hboll gcc Wall ls.c -o ls 17euxb4 hbo 117 uxb4 /ls /boot vrlinuz-3.13.0-68-generic abi-3.13.0-68-generic grub wnlinuz-3.13.0-65-generic System. map-3.13.0-68-generic memtest86+.bin abi-3.13.0-65-generic initrd.img -3.13.0-68-generic config-3.13.0-66-generic System map-3.13.0-67-generic abi-3.13.0-48-generic initrd img-3.13.0-66-generic initrd.img-3.13.0-67-generic config- 3.13.0-65-generic vrlinuz-3.13.0- 67-generic wn linuz-3.13.0- 66-generic initrd.img-3.13.0-65-generic memtest86+ multiboot.bin memtest86+.elf abi-3.13.0-66-generic System map-3.13.0-48-generic System .map-3.13.0-65-generic vrlinuz-3.13.0-48-generic config-3.13.0-68-generic config- 3.13.0-67-generic System.map-3.13.0-66-generic lost found config-3.13.0-48-generic i nitrd.img-3.13.0-48-generic 3.13.0-67-generic hbl17Quxb4 /ls hbl17euxb4 /ls /boot/config-3. 13.0-48-generic Cannot open dir /boot/config-3.13.0-48-generic: Not a directoryExplanation / Answer
#include <stdio.h>
#include <dirent.h>
int main(int argc,char *argv[])
{
char * path;
if (argc ==1){
path="./";
}
else{
path= argv[1];
}
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(path);
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory so printing the current working DIR" );
}
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL)
printf("%s ", de->d_name);
closedir(dr);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.