ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.67
Committed: Thu Sep 9 01:06:19 2004 UTC (19 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.66: +8 -11 lines
Log Message:
Got rid of silly cost-cutting (and corner-cutting) in hdfiluse()

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.67 static const char RCSid[] = "$Id: rholo.c,v 3.66 2004/09/09 00:25:59 greg Exp $";
3 gregl 3.1 #endif
4     /*
5     * Radiance holodeck generation controller
6     */
7    
8 schorsch 3.64 #include <stdio.h>
9     #include <time.h>
10 schorsch 3.59 #include <signal.h>
11     #include <sys/stat.h>
12     #include <string.h>
13    
14 greg 3.63 #include "platform.h"
15 schorsch 3.64 #include "rterror.h"
16     #include "resolu.h"
17 gregl 3.1 #include "rholo.h"
18 gregl 3.14 #include "random.h"
19 gregl 3.1
20 gwlarson 3.44 #ifndef FRAGWARN
21     #define FRAGWARN 20 /* fragmentation for warning (%) */
22     #endif
23 gregl 3.33 #ifndef MAXQTIME
24     #define MAXQTIME 5 /* target maximum seconds in queue */
25     #endif
26 gwlarson 3.49 /* manual cache flushing frequency */
27     #ifndef RTFLUSH
28     #if MAXQTIME
29     #define RTFLUSH (300/MAXQTIME*totqlen) /* <= 5 minutes */
30     #else
31     #define RTFLUSH (50*totqlen) /* just guess */
32     #endif
33     #endif
34 gregl 3.1 /* the following must be consistent with rholo.h */
35     int NVARS = NRHVARS; /* total number of variables */
36    
37     VARIABLE vv[] = RHVINIT; /* variable-value pairs */
38    
39     char *progname; /* our program name */
40     char *hdkfile; /* holodeck file name */
41 gregl 3.22 char froot[256]; /* root file name */
42 gregl 3.1
43     int ncprocs = 0; /* desired number of compute processes */
44    
45     char *outdev = NULL; /* output device name */
46    
47 gregl 3.17 int readinp = 0; /* read commands from stdin */
48    
49 gwlarson 3.51 int force = 0; /* allow overwrite of holodeck (-1 == read-only) */
50 gregl 3.20
51 gregl 3.1 time_t starttime; /* time we got started */
52     time_t endtime; /* time we should end by */
53     time_t reporttime; /* time for next report */
54    
55 greg 3.54 off_t maxdisk; /* maximum file space (bytes) */
56 gregl 3.8
57 gregl 3.1 int rtargc = 1; /* rtrace command */
58     char *rtargv[128] = {"rtrace", NULL};
59    
60 gregl 3.12 int orig_mode = -1; /* original file mode (-1 if unchanged) */
61    
62 gregl 3.1 long nraysdone = 0L; /* number of rays done */
63     long npacksdone = 0L; /* number of packets done */
64    
65     PACKET *freepacks; /* available packets */
66 gwlarson 3.49 int totqlen; /* maximum queue length (number of packets) */
67 gregl 3.1
68 gregl 3.12 char *sigerr[NSIG]; /* signal error messages */
69    
70 gwlarson 3.40 extern int nowarn; /* turn warnings off? */
71    
72 schorsch 3.64 static void onsig(int signo);
73     static void sigdie(int signo, char *msg);
74     static int resfmode(int fd, int mod);
75     static void initrholo(void);
76     static int rholo(void);
77     static void setdefaults(HDGRID *gp);
78     static void creatholo(HDGRID *gp);
79 schorsch 3.65 static gethfunc headline;
80 schorsch 3.64 static void loadholo(void);
81     static void rootname(char *rn, char *fn);
82     static void badvalue(int vc);
83 gregl 3.1
84    
85 schorsch 3.64 int
86     main(
87     int argc,
88     char *argv[]
89     )
90 gregl 3.1 {
91     int i;
92 gregl 3.33
93 gregl 3.18 initurand(16384); /* initialize urand */
94 gregl 3.1 progname = argv[0]; /* get arguments */
95     for (i = 1; i < argc && argv[i][0] == '-'; i++)
96     switch (argv[i][1]) {
97     case 'w': /* turn off warnings */
98     nowarn++;
99     break;
100     case 'f': /* force overwrite */
101 gwlarson 3.51 force = 1;
102 gregl 3.1 break;
103 gwlarson 3.51 case 'r': /* read-only mode */
104     force = -1;
105     break;
106 gregl 3.17 case 'i': /* read input from stdin */
107     readinp++;
108     break;
109 gregl 3.1 case 'n': /* compute processes */
110     if (i >= argc-2)
111     goto userr;
112     ncprocs = atoi(argv[++i]);
113     break;
114     case 'o': /* output display */
115     if (i >= argc-2)
116     goto userr;
117     outdev = argv[++i];
118     break;
119     default:
120     goto userr;
121     }
122     /* get root file name */
123 gregl 3.32 if (i >= argc)
124     goto userr;
125 gregl 3.1 rootname(froot, hdkfile=argv[i++]);
126 gregl 3.27 /* load variables? */
127     if (i < argc)
128     if (argv[i][0] != '-' && argv[i][0] != '+')
129     loadvars(argv[i]); /* load variables from file */
130    
131     if (i >= argc || argv[i][0] == '+')
132     loadholo(); /* load existing holodeck */
133    
134     while (++i < argc) /* get command line settings */
135     if (setvariable(argv[i], matchvar) < 0) {
136     sprintf(errmsg, "unknown variable: %s", argv[i]);
137     error(USER, errmsg);
138     }
139     /* check settings */
140     checkvalues();
141 gregl 3.36 /* load rad input file */
142     getradfile();
143 gregl 3.27
144 gregl 3.28 if (hdlist[0] == NULL) { /* create new holodeck */
145 gregl 3.29 HDGRID hdg[HDMAX];
146 gregl 3.1 /* set defaults */
147 gregl 3.29 setdefaults(hdg);
148 gwlarson 3.51 /* check read-only */
149     if (force < 0)
150     error(USER, "cannot create read-only holodeck");
151 gregl 3.1 /* holodeck exists? */
152     if (!force && access(hdkfile, R_OK|W_OK) == 0)
153     error(USER,
154     "holodeck file exists -- use -f to overwrite");
155     /* create holodeck */
156 gregl 3.29 creatholo(hdg);
157 gregl 3.27 } else /* else just set defaults */
158 gregl 3.15 setdefaults(NULL);
159 gregl 3.1 /* initialize */
160     initrholo();
161 gregl 3.15 /* main loop */
162 gregl 3.1 while (rholo())
163     ;
164     /* done */
165     quit(0);
166     userr:
167     fprintf(stderr,
168 gwlarson 3.51 "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
169 gregl 3.1 progname);
170     quit(1);
171 schorsch 3.64 return 1; /* pro forma return */
172 gregl 3.1 }
173    
174    
175 schorsch 3.64 static void
176     onsig( /* fatal signal */
177     int signo
178     )
179 gregl 3.12 {
180     static int gotsig = 0;
181    
182 gwlarson 3.47 if (gotsig > 1) /* we're going as fast as we can! */
183     return;
184     if (gotsig++) { /* two signals and we split */
185     hdsync(NULL, 0); /* don't leave w/o saying goodbye */
186 gregl 3.12 _exit(signo);
187 gwlarson 3.47 }
188     alarm(300); /* allow 5 minutes to clean up */
189 gregl 3.12 eputs("signal - ");
190     eputs(sigerr[signo]);
191     eputs("\n");
192     quit(3);
193     }
194    
195    
196 schorsch 3.64 static void
197     sigdie( /* set fatal signal */
198     int signo,
199     char *msg
200     )
201 gregl 3.12 {
202     if (signal(signo, onsig) == SIG_IGN)
203     signal(signo, SIG_IGN);
204     sigerr[signo] = msg;
205     }
206    
207    
208 schorsch 3.64 static int
209     resfmode( /* restrict open file access mode */
210     int fd,
211     int mod
212     )
213 gregl 3.12 {
214     struct stat stbuf;
215     /* get original mode */
216     if (fstat(fd, &stbuf) < 0)
217     error(SYSTEM, "cannot stat open holodeck file");
218     mod &= stbuf.st_mode; /* always more restrictive */
219 gregl 3.35 if (mod == (stbuf.st_mode & 0777))
220 gregl 3.12 return(-1); /* already set */
221     /* else change it */
222     if (fchmod(fd, mod) < 0) {
223     error(WARNING, "cannot change holodeck file access mode");
224     return(-1);
225     }
226     return(stbuf.st_mode); /* return original mode */
227     }
228    
229    
230 schorsch 3.64 static void
231     initrholo(void) /* get our holodeck running */
232 gregl 3.1 {
233     extern int global_packet();
234     register int i;
235 gregl 3.17 /* close holodeck on exec() */
236     fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
237 gregl 3.4
238     if (outdev != NULL) /* open output device */
239     disp_open(outdev);
240 gregl 3.1 else if (ncprocs > 0) /* else use global ray feed */
241     init_global();
242 gregl 3.8 /* record disk space limit */
243     if (!vdef(DISKSPACE))
244 greg 3.58 maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
245 gregl 3.8 else
246 gregl 3.9 maxdisk = 1024.*1024.*vflt(DISKSPACE);
247 gregl 3.1 /* set up memory cache */
248 gregl 3.5 if (outdev == NULL)
249 gwlarson 3.50 hdcachesize = 0; /* manual flushing */
250 gregl 3.5 else if (vdef(CACHE))
251     hdcachesize = 1024.*1024.*vflt(CACHE);
252 gregl 3.1 /* open report file */
253     if (vdef(REPORT)) {
254     register char *s = sskip2(vval(REPORT), 1);
255     if (*s && freopen(s, "a", stderr) == NULL)
256     quit(2);
257     }
258 gregl 3.33 /* mark the starting time */
259     starttime = time(NULL);
260     /* compute end time */
261     if (!vdef(TIME) || vflt(TIME) <= FTINY)
262     endtime = 0;
263     else
264     endtime = starttime + vflt(TIME)*3600. + .5;
265 gregl 3.1 /* start rtrace */
266     if (ncprocs > 0) {
267 gwlarson 3.38 totqlen = i = start_rtrace();
268 gregl 3.1 if (i < 1)
269 gregl 3.20 error(USER, "cannot start rtrace process(es)");
270 gregl 3.1 if (vdef(REPORT)) { /* make first report */
271     printargs(rtargc, rtargv, stderr);
272     report(0);
273     }
274     /* allocate packets */
275     freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
276     if (freepacks == NULL)
277     goto memerr;
278     freepacks[--i].nr = 0;
279     freepacks[i].next = NULL;
280 gregl 3.25 if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
281 gregl 3.1 freepacks[i].offset = (float *)bmalloc(
282     RPACKSIZ*sizeof(float)*(i+1) );
283     if (freepacks[i].offset == NULL)
284     goto memerr;
285     } else
286     freepacks[i].offset = NULL;
287     while (i--) {
288     freepacks[i].nr = 0;
289     freepacks[i].offset = freepacks[i+1].offset == NULL ?
290     NULL : freepacks[i+1].offset+RPACKSIZ ;
291     freepacks[i].next = &freepacks[i+1];
292     }
293     }
294 gregl 3.12 /* set up signal handling */
295     sigdie(SIGINT, "Interrupt");
296 schorsch 3.62 sigdie(SIGTERM, "Terminate");
297     #ifdef SIGHUP
298 gregl 3.12 sigdie(SIGHUP, "Hangup");
299 schorsch 3.62 #endif
300     #ifdef SIGPIPE
301 gregl 3.12 sigdie(SIGPIPE, "Broken pipe");
302 schorsch 3.62 #endif
303     #ifdef SIGALRM
304 gregl 3.12 sigdie(SIGALRM, "Alarm clock");
305 schorsch 3.62 #endif
306 gregl 3.12 #ifdef SIGXCPU
307     sigdie(SIGXCPU, "CPU limit exceeded");
308 schorsch 3.62 #endif
309     #ifdef SIGXFSZ
310 gregl 3.12 sigdie(SIGXFSZ, "File size exceeded");
311     #endif
312 gwlarson 3.50 /* protect holodeck file */
313 schorsch 3.60 orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
314 gregl 3.1 return;
315     memerr:
316     error(SYSTEM, "out of memory in initrholo");
317     }
318    
319    
320 schorsch 3.64 static int
321     rholo(void) /* holodeck main loop */
322 gregl 3.1 {
323 gwlarson 3.44 static long nextfragwarn = 100*(1L<<20);
324 gregl 3.30 static int idle = 0;
325 gregl 3.1 PACKET *pl = NULL, *plend;
326 greg 3.67 off_t fsiz, fuse;
327 gregl 3.33 int pksiz;
328 gregl 3.1 register PACKET *p;
329     time_t t;
330 gregl 3.30 /* check display */
331     if (nprocs <= 0)
332     idle = 1;
333     if (outdev != NULL) {
334 gregl 3.4 if (!disp_check(idle))
335 gregl 3.30 return(0); /* quit request */
336     if (nprocs <= 0)
337     return(1);
338     } else if (idle)
339     return(0); /* all done */
340 gwlarson 3.44 fsiz = hdfilen(hdlist[0]->fd); /* check file size */
341     if (maxdisk > 0 && fsiz >= maxdisk) {
342 gregl 3.8 error(WARNING, "file limit exceeded");
343 gregl 3.21 done_rtrace();
344     return(1); /* comes back */
345 gregl 3.8 }
346 gwlarson 3.44 #if FRAGWARN
347     if (fsiz >= nextfragwarn &&
348 greg 3.67 (fsiz-(fuse=hdfiluse(hdlist[0]->fd)))/(fsiz/100) > FRAGWARN) {
349     sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
350     100.*(fsiz-fuse)/fsiz);
351     error(WARNING, errmsg);
352     nextfragwarn = fsiz + (fsiz>>2);
353 gwlarson 3.44 }
354     #endif
355 gregl 3.33 t = time(NULL); /* check time */
356 gregl 3.8 if (endtime > 0 && t >= endtime) {
357     error(WARNING, "time limit exceeded");
358 gregl 3.21 done_rtrace();
359     return(1); /* comes back */
360 gregl 3.8 }
361 gregl 3.3 if (reporttime > 0 && t >= reporttime)
362 gregl 3.1 report(t);
363 gregl 3.33 /* figure out good packet size */
364 gwlarson 3.48 pksiz = RPACKSIZ;
365 gregl 3.33 #if MAXQTIME
366 gwlarson 3.48 if (!chunkycmp) {
367     pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
368     if (pksiz < 1) pksiz = 1;
369     else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
370     }
371 gregl 3.33 #endif
372 gregl 3.28 idle = 0; /* get packets to process */
373 gregl 3.1 while (freepacks != NULL) {
374     p = freepacks; freepacks = p->next; p->next = NULL;
375 gregl 3.33 if (!next_packet(p, pksiz)) {
376 gregl 3.1 p->next = freepacks; freepacks = p;
377 gregl 3.21 idle = 1;
378 gregl 3.1 break;
379     }
380     if (pl == NULL) pl = p;
381     else plend->next = p;
382     plend = p;
383     }
384 gregl 3.21 /* process packets */
385 gregl 3.1 done_packets(do_packets(pl));
386     return(1); /* and continue */
387     }
388    
389    
390 schorsch 3.64 static void
391     setdefaults( /* set default values */
392     register HDGRID *gp
393     )
394 gregl 3.1 {
395     extern char *atos();
396     register int i;
397 gregl 3.29 int n;
398 gregl 3.37 double len[3], d;
399 gregl 3.1
400     if (!vdef(SECTION)) {
401     sprintf(errmsg, "%s must be defined", vnam(SECTION));
402     error(USER, errmsg);
403     }
404     if (!vdef(OCTREE)) {
405     if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
406     error(SYSTEM, "out of memory");
407     sprintf(vval(OCTREE), "%s.oct", froot);
408     vdef(OCTREE)++;
409 gregl 3.14 }
410     if (!vdef(VDIST)) {
411     vval(VDIST) = "F";
412     vdef(VDIST)++;
413 gregl 3.1 }
414     /* append rendering options */
415     if (vdef(RENDER))
416     rtargc += wordstring(rtargv+rtargc, vval(RENDER));
417 gregl 3.15
418     if (gp == NULL) /* already initialized? */
419     return;
420     /* set grid parameters */
421 gregl 3.29 for (n = 0; n < vdef(SECTION); n++, gp++) {
422     gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
423     if (sscanf(nvalue(SECTION, n),
424 gregl 3.16 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
425 gregl 3.29 &gp->orig[0], &gp->orig[1], &gp->orig[2],
426     &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
427     &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
428     &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
429     &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
430     badvalue(SECTION);
431     for (i = 0; i < 3; i++)
432 gregl 3.37 len[i] = VLEN(gp->xv[i]);
433     if (!vdef(GRID)) {
434 gwlarson 3.43 d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
435 gregl 3.37 len[2]*len[2] + 4*len[1]*len[2])
436     + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
437     + 4*len[0]*len[1]*len[2]*len[2] );
438     d = sqrt(sqrt(d));
439     } else if ((d = vflt(GRID)) <= FTINY)
440 gregl 3.29 badvalue(GRID);
441     for (i = 0; i < 3; i++)
442     if (gp->grid[i] <= 0)
443     gp->grid[i] = len[i]/d + (1.-FTINY);
444 gregl 3.15 }
445 gregl 3.1 }
446    
447    
448 schorsch 3.64 static void
449     creatholo( /* create a holodeck output file */
450     HDGRID *gp
451     )
452 gregl 3.1 {
453 gregl 3.12 extern char VersionID[];
454 greg 3.57 int32 lastloc, nextloc;
455 gregl 3.29 int n;
456 gregl 3.10 int fd;
457 gregl 3.1 FILE *fp;
458     /* open & truncate file */
459     if ((fp = fopen(hdkfile, "w+")) == NULL) {
460     sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
461     error(SYSTEM, errmsg);
462     }
463     /* write information header */
464     newheader("RADIANCE", fp);
465 gregl 3.12 fprintf(fp, "SOFTWARE= %s\n", VersionID);
466 gregl 3.1 printvars(fp);
467     fputformat(HOLOFMT, fp);
468     fputc('\n', fp);
469 gregl 3.29 putw(HOLOMAGIC, fp); /* put magic number */
470 gregl 3.10 fd = dup(fileno(fp));
471     fclose(fp); /* flush and close stdio stream */
472 greg 3.61 lastloc = lseek(fd, (off_t)0, SEEK_END);
473 gregl 3.29 for (n = vdef(SECTION); n--; gp++) { /* initialize each section */
474     nextloc = 0L;
475     write(fd, (char *)&nextloc, sizeof(nextloc));
476     hdinit(fd, gp); /* writes beam index */
477     if (!n)
478     break;
479     nextloc = hdfilen(fd); /* write section pointer */
480 greg 3.61 if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
481 gregl 3.29 error(SYSTEM,
482     "cannot seek on holodeck file in creatholo");
483     write(fd, (char *)&nextloc, sizeof(nextloc));
484 greg 3.61 lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
485 gregl 3.29 }
486 gregl 3.1 }
487    
488    
489 schorsch 3.64 static int
490     headline( /* process information header line */
491 schorsch 3.65 char *s,
492     void *p
493 schorsch 3.64 )
494 gregl 3.1 {
495     extern char FMTSTR[];
496     register char *cp;
497     char fmt[32];
498    
499 gregl 3.2 if (formatval(fmt, s)) {
500 gregl 3.1 if (strcmp(fmt, HOLOFMT)) {
501     sprintf(errmsg, "%s file \"%s\" has %s%s",
502     HOLOFMT, hdkfile, FMTSTR, fmt);
503     error(USER, errmsg);
504     }
505 gwlarson 3.42 return(0);
506 gregl 3.1 }
507     for (cp = s; *cp; cp++) /* take off any comments */
508     if (*cp == '#') {
509     *cp = '\0';
510     break;
511     }
512     setvariable(s, matchvar); /* don't flag errors */
513 gwlarson 3.42 return(0);
514 gregl 3.1 }
515    
516    
517 schorsch 3.64 static void
518     loadholo(void) /* start loading a holodeck from fname */
519 gregl 3.1 {
520     FILE *fp;
521 gregl 3.10 int fd;
522 gregl 3.29 int n;
523 greg 3.57 int32 nextloc;
524 gwlarson 3.51
525 schorsch 3.60 if ((ncprocs > 0) & (force >= 0))
526 gwlarson 3.51 fp = fopen(hdkfile, "r+");
527     else
528     fp = NULL;
529     if (fp == NULL) {
530     if ((fp = fopen(hdkfile, "r")) == NULL) {
531     sprintf(errmsg, "cannot open \"%s\"", hdkfile);
532     error(SYSTEM, errmsg);
533     }
534     if (ncprocs > 0) {
535     sprintf(errmsg,
536     "\"%s\" opened read-only; new rays will be discarded",
537     hdkfile);
538     error(WARNING, errmsg);
539     force = -1;
540     }
541 gregl 3.1 }
542     /* load variables from header */
543     getheader(fp, headline, NULL);
544     /* check magic number */
545     if (getw(fp) != HOLOMAGIC) {
546     sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
547     hdkfile);
548     error(USER, errmsg);
549     }
550 gregl 3.29 nextloc = ftell(fp); /* get stdio position */
551 gregl 3.10 fd = dup(fileno(fp));
552     fclose(fp); /* done with stdio */
553 gregl 3.29 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
554 greg 3.61 lseek(fd, (off_t)nextloc, SEEK_SET);
555 gregl 3.29 read(fd, (char *)&nextloc, sizeof(nextloc));
556     hdinit(fd, NULL);
557     }
558     if (n != vdef(SECTION)) {
559     sprintf(errmsg, "number of sections does not match %s setting",
560     vnam(SECTION));
561     error(WARNING, errmsg);
562     }
563 gregl 3.1 }
564    
565    
566 schorsch 3.64 extern void
567     done_packets( /* handle finished packets */
568     PACKET *pl
569     )
570 gregl 3.1 {
571 gregl 3.13 static int n2flush = 0;
572 gregl 3.1 register PACKET *p;
573    
574     while (pl != NULL) {
575     p = pl; pl = p->next; p->next = NULL;
576     if (p->nr > 0) { /* add to holodeck */
577 schorsch 3.59 memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
578     (void *)p->ra,
579 gregl 3.1 p->nr*sizeof(RAYVAL));
580     if (outdev != NULL) /* display it */
581 gregl 3.7 disp_packet((PACKHEAD *)p);
582 gwlarson 3.49 if (hdcachesize <= 0)
583 gregl 3.33 n2flush++;
584 gregl 3.6 nraysdone += p->nr;
585     npacksdone++;
586 gregl 3.33 p->nr = 0;
587 gregl 3.1 }
588 gregl 3.33 p->next = freepacks; /* push onto free list */
589 gregl 3.1 freepacks = p;
590 gregl 3.5 }
591 gwlarson 3.50 if (n2flush >= RTFLUSH) {
592 gwlarson 3.46 if (outdev != NULL)
593     hdsync(NULL, 1);
594 gwlarson 3.41 else
595 gwlarson 3.46 hdflush(NULL);
596 gregl 3.13 n2flush = 0;
597 gregl 3.1 }
598     }
599    
600    
601 schorsch 3.64 static void
602     rootname( /* remove tail from end of fn */
603     register char *rn,
604     register char *fn
605     )
606 gregl 3.1 {
607     char *tp, *dp;
608    
609 schorsch 3.60 for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
610 gregl 3.22 if (*rn == '/')
611 gregl 3.1 dp = rn;
612     else if (*rn == '.')
613     tp = rn;
614 schorsch 3.60 }
615 gregl 3.1 if (tp != NULL && tp > dp)
616     *tp = '\0';
617     }
618    
619    
620 schorsch 3.64 static void
621     badvalue( /* report bad variable value and exit */
622     int vc
623     )
624 gregl 3.1 {
625     sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
626     error(USER, errmsg);
627     }
628    
629    
630 greg 3.52 void
631 gregl 3.1 eputs(s) /* put error message to stderr */
632     register char *s;
633     {
634     static int midline = 0;
635    
636     if (!*s)
637     return;
638     if (!midline++) { /* prepend line with program name */
639     fputs(progname, stderr);
640     fputs(": ", stderr);
641     }
642     fputs(s, stderr);
643     if (s[strlen(s)-1] == '\n') {
644     fflush(stderr);
645     midline = 0;
646     }
647     }
648    
649    
650 greg 3.52 void
651 gregl 3.1 quit(ec) /* exit program gracefully */
652     int ec;
653     {
654     int status = 0;
655    
656 gregl 3.24 if (hdlist[0] != NULL) { /* close holodeck */
657 gregl 3.21 if (nprocs > 0)
658 gregl 3.24 status = done_rtrace(); /* calls hdsync() */
659 schorsch 3.60 if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
660 greg 3.54 off_t fsiz, fuse;
661 gregl 3.19 fsiz = hdfilen(hdlist[0]->fd);
662 greg 3.67 fuse = hdfiluse(hdlist[0]->fd);
663 gregl 3.19 fprintf(stderr,
664 gregl 3.3 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
665 gregl 3.19 hdkfile, fsiz/(1024.*1024.),
666     100.*(fsiz-fuse)/fsiz);
667     }
668 gregl 3.1 }
669 gregl 3.12 if (orig_mode >= 0) /* reset holodeck access mode */
670     fchmod(hdlist[0]->fd, orig_mode);
671 gregl 3.11 if (outdev != NULL) /* close display */
672     disp_close();
673 gregl 3.1 exit(ec ? ec : status); /* exit */
674     }