#include<stdio.h>
#include<stdlib.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp;
void count(char *t2,char *t3)
{
int charcount=0,wordcount=0,linecount=0;
if((fp=fopen(t3,"r"))==NULL)
printf("File not found");
else
{
while(1)
{
ch=fgetc(fp);
printf("\nch=%c",ch);
if((ch==' ')||(ch=='\n')||(ch=='\t')||(ch==EOF))
wordcount++;
if((ch=='\n')||(ch==EOF))
linecount++;
if(ch==EOF)
break;
charcount++;
}
fclose(fp);
if(strcmp(t2,"-c")==0)
printf("The total no. of characters :%d\n",charcount);
else if(strcmp(t2,"-w")==0)
printf("The total no. of words :%d\n",wordcount);
else if(strcmp(t2,"-l")==0)
printf("The total no. of lines :%d\n",linecount);
else
printf("Command not found");
}
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"exit")==0)
exit(0);
else if(strcmp(t1,"wc")==0)
count(t2,t3);
}
}
Post a Comment