#include #include #include #include #include #include extern const char *__progname; static char tfn[64]; static FILE *tf; static char *makeppsym(const char *fn, unsigned long int chksum) { const char *fp; char *tp; char *ppsym; int i; typedef unsigned char UC; fp = rindex(fn,'/'); if (fp) fp ++; else fp = fn; ppsym = malloc(1+strlen(fn)+1+8+1); tp = ppsym; *tp++ = '_'; for (;*fp;fp++) { if (!isascii(*fp) || !isalnum((UC)*fp)) *tp++ = '_'; else if (islower((UC)*fp) ) *tp++ = toupper((UC)*fp); else *tp++ = *fp; } *tp++ = '_'; for (i=28;i>=0;i-=4) { *tp++ = "0123456789abcdef"[chksum&0xf]; chksum >>= 4; } *tp++ = '_'; *tp = '\0'; return(ppsym); } static void dowrap(const char *fn) { FILE *ff; char *ppsym; int skip; int ppl; char *filebeg; char *tfilebeg; char *savfilebeg; unsigned int nfb; unsigned int fbx; char fileend[16]; unsigned int fex; int c; int i; unsigned long int chksum; unsigned long int ichksum; ff = fopen(fn,"r+"); if (ff == 0) { fprintf(stderr,"%s: can't open %s\n",__progname,fn); return; } ppsym = makeppsym(fn,0); ppl = strlen(ppsym); nfb = (ppl * 2) + 19; filebeg = malloc((nfb+1)*3); tfilebeg = filebeg + nfb + 1; savfilebeg = tfilebeg + nfb + 1; fbx = 0; fex = 0; chksum = 0; ichksum = 0; i = 0; while (1) { c = getc(ff); if (c == EOF) break; /* note: assumes stuff saved in fileend is shorter than filebeg! otherwise, ichksum will use as-yet-unset fileend[] elements. */ fex = (fex + 1) % 8; if (fbx < nfb) { filebeg[fbx++] = c; } else { if (i < 8) { i ++; } else { ichksum ^= fileend[fex]; ichksum = (ichksum & 1) ? (ichksum >> 1) ^ 0xedb88320 : (ichksum >> 1); } } chksum ^= c; chksum = (chksum & 1) ? (chksum >> 1) ^ 0xedb88320 : (chksum >> 1); fileend[fex] = c; } if (ferror(ff)) { fprintf(stderr,"%s: error reading %s - not changed\n",__progname,fn); fclose(ff); free(filebeg); return; } if ((fbx > 18) && !bcmp(filebeg,"/* NO WRAPHFILE */",18)) { fclose(ff); free(filebeg); return; } sprintf(tfilebeg,"#ifndef %s\n#define %s\n\n",ppsym,ppsym); skip = 0; if (fbx >= nfb) { bcopy(filebeg,savfilebeg,nfb); bcopy("00000000",filebeg+8+ppl-9,8); bcopy("00000000",filebeg+8+ppl+9+ppl-9,8); if (! bcmp(filebeg,tfilebeg,nfb)) { for (i=15;i>=8;i--) { fileend[i] = fileend[fex]; fex = (fex + 7) % 8; } if (! bcmp(&fileend[8],"\n#endif\n",8)) { ppsym = makeppsym(fn,ichksum); if ( !bcmp(savfilebeg+8,ppsym,ppl) && !bcmp(savfilebeg+8+ppl+9,ppsym,ppl) ) { fclose(ff); free(filebeg); return; } skip = nfb; } } } if (! skip) ppsym = makeppsym(fn,chksum); rewind(ff); rewind(tf); fprintf(tf,"#ifndef %s\n#define %s\n\n",ppsym,ppsym); if (skip) { for (i=0;i0;i--) { c = getc(tf); if (c == EOF) break; putc(c,ff); } if (ferror(tf)) { fprintf(stderr,"%s: error reading temp file - %s may be corrupted\n", __progname,fn); } fclose(ff); free(filebeg); } int main(int, char **); int main(int ac, char **av) { sprintf(tfn,"/tmp/wraphfile.%d",getpid()); tf = fopen(tfn,"w+"); if (tf == 0) { fprintf(stderr,"%s: can't open temporary file %s\n",__progname,tfn); exit(1); } unlink(tfn); for (ac--,av++;ac;ac--,av++) { dowrap(av[0]); } exit(0); }