#include "comppp.h" #include /*#include */ char *toolname = "comp-pp"; char *version = "<< Demeter Tools/C++ 5.0 >>"; int mv_if_change(mv_mode m,char *newfile,char *oldfile) { FILE *nfp,*ofp; char buf[MAXDIRLEN+1]; char c,nc,oc; int nr,or; assert( (newfile != NULL) && (oldfile != NULL) && (buf != NULL) ); if ((nfp = fopen(newfile,"r")) == NULL) { sprintf(buf,"%s: read %s",toolname,newfile); perror(buf); if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 1; } if ((ofp = fopen(oldfile,"r")) == NULL) { if ((ofp = fopen(oldfile,"w"))==NULL) { sprintf(buf,"%s: write %s",toolname,oldfile); perror(buf); if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 1; } while (fread(&c,1,1,nfp) == 1) fwrite(&c,1,1,ofp); if (m == mv_interactive) cout << toolname << ": " << oldfile << " created." << endl; fclose(nfp); fclose(ofp); if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 0; } nr = fread(&nc,1,1,nfp); or = fread(&oc,1,1,ofp); while ( (nr == or) && (nr != 0) && (nc == oc)) { nr = fread(&nc,1,1,nfp); or = fread(&oc,1,1,ofp); } fclose(nfp); fclose(ofp); if ((nr == or) && (nr == 0)) { /* if (m == mv_interactive) cout << toolname << ": " << oldfile << " is up to date." << endl; */ if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 0; } if ((nfp = fopen(newfile,"r")) == NULL) { sprintf(buf,"%s: open %s",toolname,newfile); perror(buf); if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 1; } if ((ofp = fopen(oldfile,"w"))==NULL) { sprintf(buf,"%s: open %s",toolname,oldfile); perror(buf); if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } return 1; } while (fread(&c,1,1,nfp) == 1) fwrite(&c,1,1,ofp); if (m == mv_interactive) cout << toolname << ": " << oldfile << " recreated." << endl; if (unlink(newfile)) { sprintf(buf,"%s: warning : rm %s",toolname,newfile); perror(buf); } fclose(nfp); fclose(ofp); return 0; } void abort_with_help() { assert(toolname != NULL); cout << "\n" << toolname << " [ nosuffix ] cd-file current-component-name [source-file]\n"; cout << "\n" << toolname << " [ nosuffix ] cd-file current-component-name -p target-path source-file ... \n"; cout << toolname << " -help \n" << endl; exit(0); } FILE *xfopen(char *s,char *p) { FILE *fp; fp = fopen(s,p); if (fp == NULL) { fprintf(stderr,"%s : unable to open file %s. \n",toolname,s); perror(s); exit(1); } return fp; }