--- ray/src/hd/rhoptimize.c 2003/02/22 02:07:25 3.8 +++ ray/src/hd/rhoptimize.c 2018/10/05 19:19:16 3.22 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rhoptimize.c,v 3.8 2003/02/22 02:07:25 greg Exp $"; +static const char RCSid[] = "$Id: rhoptimize.c,v 3.22 2018/10/05 19:19:16 greg Exp $"; #endif /* * Optimize holodeck for quick access. @@ -7,10 +7,16 @@ static const char RCSid[] = "$Id: rhoptimize.c,v 3.8 2 * 11/4/98 Greg Ward Larson */ -#include "holo.h" - #include +#include +#include +#include "platform.h" +#include "rterror.h" +#include "resolu.h" +#include "rtprocess.h" /* getpid() */ +#include "holo.h" + #ifndef BKBSIZE #define BKBSIZE 256 /* beam clump size (kilobytes) */ #endif @@ -19,17 +25,22 @@ char *progname; char tempfile[128]; int dupchecking = 0; -extern char *rindex(); -extern long rhinitcopy(); +static long rhinitcopy(int hfd[2], char *infn, char *outfn); +static int nuniq(RAYVAL *rva, int n); +static int bpcmp(const void *b1p, const void *b2p); +static int xferclump(HOLO *hp, int *bq, int nb); +static void copysect(int ifd, int ofd); -main(argc, argv) -int argc; -char *argv[]; +int +main( + int argc, + char *argv[] +) { char *inpname, *outname; int hdfd[2]; - long nextipos, lastopos, thisopos; + off_t nextipos, lastopos, thisopos; progname = argv[0]; argv++; argc--; /* duplicate checking flag? */ @@ -37,7 +48,7 @@ char *argv[]; dupchecking++; argv++; argc--; } - if (argc < 1 | argc > 2) { + if ((argc < 1) | (argc > 2)) { fprintf(stderr, "Usage: %s [-u] input.hdk [output.hdk]\n", progname); exit(1); @@ -52,7 +63,7 @@ char *argv[]; error(SYSTEM, errmsg); } strcpy(tempfile, inpname); - if ((outname = rindex(tempfile, '/')) != NULL) + if ((outname = strrchr(tempfile, '/')) != NULL) outname++; else outname = tempfile; @@ -61,20 +72,20 @@ char *argv[]; } /* copy holodeck file header */ nextipos = rhinitcopy(hdfd, inpname, outname); - lastopos = 0L; /* copy sections one by one */ + lastopos = 0; /* copy sections one by one */ while (nextipos != 0L) { /* set input position; get next */ - lseek(hdfd[0], (off_t)nextipos, 0); + lseek(hdfd[0], nextipos, SEEK_SET); read(hdfd[0], (char *)&nextipos, sizeof(nextipos)); /* get output position; set last */ - thisopos = lseek(hdfd[1], (off_t)0L, 2); - if (lastopos > 0L) { - lseek(hdfd[1], (off_t)lastopos, 0); + thisopos = lseek(hdfd[1], (off_t)0, SEEK_END); + if (lastopos > 0) { + lseek(hdfd[1], lastopos, SEEK_SET); write(hdfd[1], (char *)&thisopos, sizeof(thisopos)); - lseek(hdfd[1], (off_t)0L, 2); + lseek(hdfd[1], (off_t)0, SEEK_END); } lastopos = thisopos; - thisopos = 0L; /* write place holder */ + thisopos = 0; /* write place holder */ write(hdfd[1], (char *)&thisopos, sizeof(thisopos)); /* copy holodeck section */ copysect(hdfd[0], hdfd[1]); @@ -87,14 +98,16 @@ char *argv[]; outname, inpname); error(SYSTEM, errmsg); } - exit(0); + return(0); } -long -rhinitcopy(hfd, infn, outfn) /* open files and copy header */ -int hfd[2]; /* returned file descriptors */ -char *infn, *outfn; +static long +rhinitcopy( /* open files and copy header */ + int hfd[2], /* returned file descriptors */ + char *infn, + char *outfn +) { FILE *infp, *outfp; long ifpos; @@ -112,9 +125,15 @@ char *infn, *outfn; error(SYSTEM, errmsg); } /* set up signal handling */ +#ifdef SIGINT if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); +#endif +#ifdef SIGHUP if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN); +#endif +#ifdef SIGTERM if (signal(SIGTERM, quit) == SIG_IGN) signal(SIGTERM, SIG_IGN); +#endif #ifdef SIGXCPU if (signal(SIGXCPU, quit) == SIG_IGN) signal(SIGXCPU, SIG_IGN); if (signal(SIGXFSZ, quit) == SIG_IGN) signal(SIGXFSZ, SIG_IGN); @@ -141,12 +160,13 @@ char *infn, *outfn; } -int -nuniq(rva, n) /* sort unique rays to front of beam list */ -register RAYVAL *rva; -int n; +static int +nuniq( /* sort unique rays to front of beam list */ + RAYVAL *rva, + int n +) { - register int i, j; + int i, j; RAYVAL rtmp; for (j = 0; j < n; j++) @@ -157,9 +177,9 @@ int n; rva[i].r[1][0]==rva[j].r[1][0] && rva[i].r[1][1]==rva[j].r[1][1] ) { n--; /* swap duplicate with end */ - copystruct(&rtmp, rva+n); - copystruct(rva+n, rva+i); - copystruct(rva+i, &rtmp); + rtmp = *(rva+n); + *(rva+n) = *(rva+i); + *(rva+i) = rtmp; i--; /* recheck one we swapped */ } return(n); @@ -169,10 +189,12 @@ int n; static BEAMI *beamdir; static int -bpcmp(b1p, b2p) /* compare beam positions on disk */ -int *b1p, *b2p; +bpcmp( /* compare beam positions on disk */ + const void *b1p, + const void *b2p +) { - register long pdif = beamdir[*b1p].fo - beamdir[*b2p].fo; + off_t pdif = beamdir[*(int*)b1p].fo - beamdir[*(int*)b2p].fo; if (pdif < 0L) return(-1); return(pdif > 0L); @@ -181,22 +203,24 @@ int *b1p, *b2p; static HOLO *hout; static int -xferclump(hp, bq, nb) /* transfer the given clump to hout and free */ -HOLO *hp; -int *bq, nb; +xferclump( /* transfer the given clump to hout and free */ + HOLO *hp, + int *bq, + int nb +) { - register int i; - register BEAM *bp; + int i; + BEAM *bp; int n; beamdir = hp->bi; /* sort based on file position */ - qsort((char *)bq, nb, sizeof(*bq), bpcmp); + qsort((void *)bq, nb, sizeof(*bq), bpcmp); /* transfer and free each beam */ for (i = 0; i < nb; i++) { bp = hdgetbeam(hp, bq[i]); DCHECK(bp==NULL, CONSISTENCY, "empty beam in xferclump"); n = dupchecking ? nuniq(hdbray(bp),bp->nrm) : bp->nrm; - bcopy((char *)hdbray(bp), (char *)hdnewrays(hout,bq[i],n), + memcpy((void *)hdnewrays(hout,bq[i],n),(void *)hdbray(bp), n*sizeof(RAYVAL)); hdfreebeam(hp, bq[i]); } @@ -204,8 +228,11 @@ int *bq, nb; return(0); } -copysect(ifd, ofd) /* copy holodeck section from ifd to ofd */ -int ifd, ofd; +static void +copysect( /* copy holodeck section from ifd to ofd */ + int ifd, + int ofd +) { HOLO *hinp; /* load input section directory */ @@ -222,7 +249,7 @@ int ifd, ofd; void eputs(s) /* put error message to stderr */ -register char *s; +char *s; { static int midline = 0;