SIMULATION OF LS COMMAND
AIM:
To write a C program to simulate ls command.
ALGORITHM:
- Start the process.
- Create a directory entry using dirent structure.
- To define a pointer to a structure, dp predefined structure DIR and another pointer to a structure called ep.
- The directory is opened using the opendir() function.
- In the while loop read each entry using the readdir() function which returns a pointer.
- If no pointer is returned the program is exited, else it will list the files inside the directory.
- The closedir() function closes this open directory.
- Stop the process.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
Struct dirent *dptr;
int main(int argc,char* argv[])
{
char buff[256];
DIR *dirp;
Dirp=opendir(“.”);
printf(“\n \n enter directory name”);
scanf(“%s”,buff);
if((disp=opendir(buff))==NULL)
{
printf(“error”);
exit(1);
}
while (dptr==readir(disp))
{
printf(“%s \n”,dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
Enter directory name ex3
a.out
ex1.c
ls
ex3a
ex1
ex3.c
RESULT:
1 comments:
copy nai hota code...fuck
Post a Comment