#include int main(int argc,char*argv[]) { char pn[100],p[100],sp[100],*prog=argv[0];FILE*fp; int comp(char*,char*),sost(FILE*,char*,char*,char*); if(argc==2&&comp("--help",argv[1])) {printf("%s: usage: %s pathname pattern new_pattern\n",prog,prog);exit(0);} else if(argc==4) {strcpy(pn,argv[1]); strcpy(p,argv[2]); strcpy(sp,argv[3]);} else if(argc==1) {printf("digitare : pathname pattern nuovo_pattern : "); scanf("%s%s%s",pn,p,sp);for(;(getchar())!='\n';);} else {printf("%s: error: type %s --help for more information\n",p,p);exit(1);} if(!(fp=fopen(pn,"r+"))) {fprintf(stderr,"%s: file \"%s\" doesn't exist .\n",prog,pn);exit(1);} else printf("%s: %d sostituzioni effettuate .\n",prog,sost(fp,p,sp,pn)); printf("\n\n\n"); system("cat ciao"); } int comp(char*a,char*b) { for(;*a!='\0';) if(*a++!=*b++) return 0; return 1; } int sost(FILE*fp,char*p,char*sp,char*pn) { char c;int fcomp(FILE*,char*),count=0; void write(FILE*,char*,int,int*,char*); for(;(c=getc(fp))!=EOF;) if(c==*p&&fcomp(fp,p)) write(fp,sp,strlen(p),&count,pn); fclose(fp); if(fp=fopen(".~tmp","r+")) {fclose(fp); system("rm .~tmp");} return count; } int fcomp(FILE*fp,char*p) { int l=strlen(p),i=0;char tmp[100],*t=tmp; for(fseek(fp,-1,SEEK_CUR);ip) {fseek(fp,p-1,SEEK_CUR); for(t=-2;(c=getc(fp))!=EOF;++t); for(i=sps-p,pp=t,ppp=i;i>0;--i,t=pp) for(fseek(fp,-1,SEEK_END);t>-1;--t) {c=getc(fp); putc(c,fp); fseek(fp,-3,SEEK_CUR);} fseek(fp,-1-pp-ppp-p,SEEK_END); fwrite(sp,sps,1,fp);} } int copia(FILE*fp,char*pn) { FILE*tmp; int t=0; char c; if(!(tmp=fopen(".~tmp","w+"))) return 0; for(;!fseek(fp,-1,SEEK_CUR);++t); for(;(c=getc(fp))!='\0';putc(c,tmp));putc('\0',tmp); fclose(fp); fp=fopen(pn,"w+"); for(fseek(tmp,0,SEEK_SET);(c=getc(tmp))!=EOF;putc(c,fp));putc('\0',fp); fclose(tmp); fseek(fp,t,SEEK_SET); return 1; } // gestisco errori in scrittura su file e ferror