Program:
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
struct stat stbuf;
if (argc != 2)
printf("usage: ls directory_name\n");
if ((dp = opendir(argv[1])) == NULL)
printf("can’t open %s", argv[1]);
while ((dirp = readdir(dp)) != NULL){
stat(dirp->d_name,&stbuf);
if(S_ISDIR(stbuf.st_mode)){
printf("Directory: ");
}
printf("%s\n",dirp->d_name);
}
closedir(dp);
exit(0);
}
Output:
$ ./a.out test/
dir3
d
c
Directory: .
Directory: a
Directory: ..
Directory: dir4
Directory: dir2
Directory: dir0
Directory: b
Directory: e
Directory: dir1
$
The following are the list of files that the directory "test" contains.
$ ls -F test/
a b c d dir0/ dir1/ dir2/ dir3/ dir4/ e
$
The expected output is, if the file is directory the output will be "Directory: dir1/". Else only the file name. But, the output of the program is not as expected. Is the program contains any error?. Is there any let me know.
Thanks in Advance...