Write a Program to implement "ls" command of UNIX with following options on DOS.
- "ls" to display files.
- "ls -a" to display all the hidden files.
- "ls -m" to display the files separated with commas.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
void ls(){
    DIR *d;
    struct dirent *dn;
    char dr[20];
    printf("Enter the Directory name:");
    gets(dr);
    if((d=opendir(dr))==NULL){
        printf("\nDirectory can't open");
    } else {
        while((dn=readdir(d))!=NULL){
            printf(" %s",dn->d_name);
        }
    }
}
void lsm(){
    DIR *d;
    struct dirent *dn;
    char dr[20];
    printf("Enter the Directory name:");
    gets(dr);
    if((d=opendir(dr))==NULL){
        printf("\nDirectory can't open");
    } else{
        while((dn=readdir(d))!=NULL){
            printf("%s,",dn->d_name);
        }
    }
}
int main1(){
 setbuf(stdout, NULL);
 char s[10],c[2],ar[2];
 while(1){
  printf("\nAP:>");
  gets(s);
  if(strcmp(s,"exit")==0){
   exit(0);
  } else{
   if(strcmp(s,"ls")==0){
    ls();
   }else {
    sscanf(s,"%s%s",c,ar);
    if(strcmp(ar,"-m")==0){
     lsm();
    }else{
     printf("Invalid command");
    }
   }
  }
 }
    return EXIT_SUCCESS;
}  
Comments
Post a Comment