In: Computer Science
Systems Programming in Linux using C
1). ANSWER :
GIVENTHAT :
Here is the C code for ls -ialR command.
C-PROGRAM :
// Including necessary or needed header files
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
// Starting of main function
int main(int argc, char* argv[])
{
// Declaring the variable DIR
DIR *alldir;
struct dirent *allfile; // all available files
struct stat allstat; // stats of the files
// buffer
char buf[512];
// opening the current dir
alldir = opendir(argv[1]);
// Dir must not equal to Null / Existance of dir must
while((allfile = readdir(alldir)) != NULL)
{
// Printing all files names
sprintf(buf, "%s/%s", argv[1], allfile->d_name);
stat(buf, &allstat);
// Printing Stats of the files
printf("%zu",allstat.st_size);
printf(" %s\n", allfile->d_name);
}
// Closing the Dir or stats on terminating of program
closedir(alldir);
}
The above program Output : -
Execute the program in linux by this command:- ./program.out Ashok
You will get total directories with name and stats.
Program compiled in CodeBlocks Screenshot:-
In bottom of the screen You can check all directries under that file. ( from "checking for existance")
First it will check for for existance than show all dir. .