C programming All programs must be written in ANSI C, compiled with the followin
ID: 3719810 • 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
This is a simple program to do directory listing without the system() command.
CODE
#include <stdio.h>
#include <dirent.h>
int main(void)
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
// 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;
}
and this is a bit different approach but very robust, my personal suggestion is to use this:
CODE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.