--- ray/src/util/rtcontrib.c 2010/10/10 09:25:04 1.60 +++ ray/src/util/rtcontrib.c 2011/04/16 00:39:07 1.64 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.60 2010/10/10 09:25:04 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.64 2011/04/16 00:39:07 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -20,7 +20,7 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.60 2 #define MAXMODLIST 1024 /* maximum modifiers we'll track */ #endif -size_t treebufsiz = BUFSIZ; /* current tree buffer size */ +ssize_t treebufsiz = BUFSIZ; /* current tree buffer size */ typedef double DCOLOR[3]; /* double-precision color */ @@ -107,8 +107,8 @@ int rtargc = 9; char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", "-dt", "0", "-as", "0", "-aa", "0", NULL }; -#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ -#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ +#define RTCOEFF "-o~~~TmWdp" /* compute coefficients only */ +#define RTCONTRIB "-o~~~TmVdp" /* compute ray contributions */ struct rtproc rt0; /* head of rtrace process list */ @@ -702,12 +702,19 @@ void printheader(FILE *fout, const char *info) { extern char VersionID[]; - FILE *fin = fopen(octree, "r"); - - if (fin == NULL) - quit(1); - checkheader(fin, "ignore", fout); /* copy octree header */ - fclose(fin); + /* copy octree header */ + if (octree[0] == '!') { + newheader("RADIANCE", fout); + fputs(octree+1, fout); + if (octree[strlen(octree)-1] != '\n') + fputc('\n', fout); + } else { + FILE *fin = fopen(octree, "r"); + if (fin == NULL) + quit(1); + checkheader(fin, "ignore", fout); + fclose(fin); + } printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); @@ -1106,23 +1113,29 @@ wait_rproc(void) continue; if (rt->buf == NULL) { rt->bsiz = treebufsiz; - rt->buf = (char *)malloc(treebufsiz); + rt->buf = (char *)malloc(rt->bsiz); } else if (rt->nbr + BUFSIZ > rt->bsiz) { if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; - else - treebufsiz = rt->bsiz += BUFSIZ; + else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0) + error(INTERNAL, + "ray buffer does not fit memory"); rt->buf = (char *)realloc(rt->buf, rt->bsiz); } if (rt->buf == NULL) error(SYSTEM, "out of memory in wait_rproc"); - nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr); - if (nr <= 0) + nr = rt->bsiz - rt->nbr; + if (nr & ~0x7fffffff) /* avoid 32-bit OS issues */ + nr = 0x7fffffff; + nr = read(rt->pd.r, rt->buf+rt->nbr, nr); + if (nr < 0) + error(SYSTEM, "read error from rtrace"); + if (!nr) error(USER, "rtrace process died"); rt->nbr += nr; /* advance & check */ - if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, - "~\t~\t", 4)) { - rt->nbr -= 4; /* elide terminator */ + if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6, + "~\t~\t~\t", 6)) { + rt->nbr -= 6; /* elide terminator */ queue_raytree(rt); rtfree = rt; /* ready for next ray */ } @@ -1164,8 +1177,7 @@ trace_contribs(FILE *fin) lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - if (lastray+1 < lastray) - lastdone = lastray = 0; + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ rtp->raynum = ++lastray; /* assign ray */