Write a program to implement ls command of UNIX with following options on DOS.


Write a program to implement ls command of UNIX with following options on DOS.


  • ls -l to display long listing of files rowwise.
  • ls -Q to display the filename in double quotes.
  • ls -c to sort the files on last modification time of the files


Program:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

void main(){
 setbuf(stdout,NULL);
 DIR *dir;
 struct dirent *ent;
 char s[80], c[5], f[50], ar[2], ch[2];
 while(1){
  printf("\nAP:>");
  gets(s);
  if(strcmp(s,"exit")==0){
   exit(0);
  }
  sscanf(s,"%s %s %s",&c,&ch,&f);
  if(strcmp(c,"ls")!=0){
   printf("Invalid Command");
  }else if(strcmp(ch,"-l")==0){
   if((dir = opendir(f))==NULL){
    perror("Unable to open directory.");
    exit(1);
   }
   while((ent=readdir(dir))!=NULL){
    printf("\n%s",ent->d_name);
   }
   if(closedir(dir)!=0){
    perror("Unable to close directory.");
   }
  }else if(strcmp(ch,"-Q")==0){
   if((dir = opendir(f))==NULL){
    perror("Unable to open directory.");
    exit(1);
   }
   strcpy(ar,"''");
   while((ent=readdir(dir)) != NULL){
    printf("\n%s%s%s",ar,ent->d_name,ar);
   }
   if(closedir(dir)!=0){
    perror("Unable to close directory.");
   }
  }
 }
}

Post a Comment

Previous Post Next Post