Google Search

Custom Search

Sunday, February 14, 2010

UNIX Cat Like Utility

#include
#include
#include

int main(int argc,char* argv[]){
FILE* fp;
int i=0;
char c;
if(argc==1){
fprintf(stderr,"Error using cat...
");
exit(2);
}
if(!strcmp(argv[1],"-w")){ /* This returns 1 if match is found */

if(argc!=3){
fprintf(stderr,"Invalid no. of arguments
");
exit(2);
}
fp=fopen(argv[2],"w");
if(fp==NULL)
{
fprintf(stderr,"Error in opening file...
");
exit(2);
}
c=getchar();
while(c!=EOF)
{
fputc(c,fp);
c=getchar();
}
fclose(fp);
}
else{
for(--argc;argc>0;argc--){
fp=fopen(argv[++i],"r");
if(fp==NULL){
fprintf(stderr,"File not found...Aborting
");
exit(2);
}
while((c=fgetc(fp))!=EOF)
fputc(c,stdout);
fclose(fp);
}
}

}

No comments: