Thursday 29 September 2011


SIMULATION OF LS COMMAND

AIM:
To write a C program to simulate ls command.

ALGORITHM:

  1. Start the process.
  2. Create a directory entry using dirent structure.
  3. To define a pointer to a structure, dp predefined structure DIR and another pointer to a structure called ep.
  4. The directory is opened using the opendir() function.
  5. In the while loop read each entry using the readdir() function which returns a pointer.
  6. If no pointer is returned the program is exited, else it will list the files inside the directory.
  7. The closedir() function closes this open directory.
  8. 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:
 Thus the given C Program is executed and output is verified.

1 comments:

Anonymous said...

copy nai hota code...fuck

Post a Comment