Write a program to implement cat command of UNIX on DOS.
- cat <filename> to display files on the screen.
- cat < filename it is same as type command.
- cat > filename it is same as copy command.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
void main(){
setbuf(stdout,NULL);
DIR *dir;
struct dirent *ent;
FILE *fp;
char s[80],temp[80],c[5],f[50],a[2],p[2];
while(1){
printf("\nAP:>");
gets(s);
if(strcmp(s,"exit")==0){
exit(0);
}
sscanf(s,"%s%s%s%s",c,a,f,p);
printf("%s|%s|%s|%s",c,a,f,p);
if(strcmp(c,"cat")!=0){
printf("invalid command");
}else if(strcmp(a,"<")==0 && strcmp(p,">")==0){
if((dir=opendir(f))==NULL){
perror("Unable to open direcotry.");
exit(0);
}
while((ent= readdir(dir))!=NULL){
printf("%s\n",ent->d_name);
}
if(closedir(dir)!=0){
perror("Unable to close directory.");
}
}else if(strcmp(a,"<")==0){
fp=fopen(f,"r");
if(fp==NULL){
printf("File Not Found.");
}else{
while(!feof(fp)){
fgets(temp,80,fp);
printf("%s",temp);
}
}
}else if(strcmp(a,">")==0){
fp=fopen(f,"w");
if(fp==NULL){
printf("File Not created.");
}else{
printf("\nEnter the content:");
while(strlen(gets(temp))>0){
fputs(temp,fp);
fputs("\n",fp);
}
printf("\nOne file created;");
fclose(fp);
}
}
}
}
Comments
Post a Comment