Write a program to implement following UNIX commands


Write a program to implement following UNIX commands


  • mv <source file> <target file> it moves file into target file.
  • cp <source file> <target file> it copies file into target file.
  • rm <target file> it deletes the file.


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

void main(){
 setbuf(stdout,NULL);
 FILE *fp1, *fp2;
 char s[50], cm[20], path1[20], path2[20], ch;
 while(1){
  printf("\nAP:>");
  gets(s);
  if(strcmp(s,"exit")==0){
   exit(0);
  }else{
   sscanf(s,"%s%s%s",&cm,&path1,&path2);
   if(strcmp(cm,"rm")==0){
    if(remove(path1)==0){
     printf("File Deleted.");
    }else{
     printf("File not deleted.");
    }
   }else if(strcmp(cm,"mv")==0){
    fp1 = fopen(path1,"r");
    fp2 = fopen(path2,"w");
    while(!feof(fp1)){
     ch = fgetc(fp1);
     fputc(ch,fp2);
    }
    remove(path1);
   }
  }
 }
}

Post a Comment

Previous Post Next Post