C PROGRAMMING All programs must be written in ANSI C, compiled with the followin
ID: 3720677 • Letter: C
Question
C PROGRAMMING
All programs must be written in ANSI C, compiled with the following options: -O3 -Wall -Wextra -std=c99, and display no errors or warnings. Do not submit any executable files, but only the C source file(s).
Write a program ls-l that shall have the same functionality as the POSIX program ls -l.
The program shall take zero or more command line parameters. When called with no parameters, the programs shall produce the listing of the current working directory. Otherwise, it shall sequentially list all directories mentioned on the command line.
Explanation / Answer
#include<dirent.h>
#include<errno.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]){
struct dirent *direntp;
DIR *d;
if (argc == 1){
if ((d = opendir(".")) == NULL){
perror("Failed to open directory");
return 1;
}
}
else if ((d = opendir(argv[1])) == NULL){
perror("Failed to open directory");
return 1;
}
while((direntp = readdir(d)) != NULL){
printf("%s ",direntp->d_name);
}
while((closedir(d) == -1) && (errno == EINTR));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.