--- ray/src/util/rpiece.c 1992/08/09 23:05:17 2.11 +++ ray/src/util/rpiece.c 1993/04/16 12:03:59 2.18 @@ -10,16 +10,44 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" #include + +#ifndef F_SETLKW + +main(argc, argv) +int argc; +char *argv[]; +{ + fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); + exit(1); +} + +#else + #include #include "color.h" #include "view.h" #include "resolu.h" +#ifndef NFS +#define NFS 1 +#endif /* set the following to 0 to forgo forking */ #ifndef MAXFORK +#if NFS #define MAXFORK 3 /* allotment of duped processes */ +#else +#define MAXFORK 0 #endif - +#endif + /* protection from SYSV signals(!) */ +#if defined(sgi) || defined(hpux) +#define guard_io() sighold(SIGALRM) +#define unguard() sigrelse(SIGALRM) +#endif +#ifndef guard_io +#define guard_io() 0 +#define unguard() 0 +#endif /* rpict command */ char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; int rpargc = 9; @@ -102,12 +130,17 @@ char *argv[]; case 'F': /* syncronization file */ if (argv[i][2]) break; - if ((syncfd = open(argv[++i], O_RDWR)) < 0) { + if ((syncfd = open(argv[++i], + O_RDWR|O_CREAT, 0666)) < 0) { fprintf(stderr, "%s: cannot open\n", argv[i]); exit(1); } continue; + case 'z': /* z-file ist verbotten */ + fprintf(stderr, "%s: -z option not allowed\n", + argv[0]); + exit(1); case 'o': /* output file */ if (argv[i][2]) break; @@ -123,8 +156,7 @@ char *argv[]; } init(argc, argv); rpiece(); - rval = cleanup(0); - exit(rval); + exit(cleanup(0)); } @@ -164,8 +196,8 @@ char **av; } else if ((outfd = open(outfile, O_RDWR)) >= 0) { if ((fp = fdopen(dup(outfd), "r+")) == NULL) goto filerr; - getheader(fp, NULL); /* skip header */ - if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ + getheader(fp, NULL, NULL); /* skip header */ + if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ hr != hres*hmult || vr != vres*vmult) { fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", progname, outfile); @@ -179,7 +211,9 @@ char **av; scanorig = ftell(fp); /* record position of first scanline */ if (fclose(fp) == -1) /* done with stream i/o */ goto filerr; +#if NFS sync(); /* flush NFS buffers */ +#endif /* start rpict process */ if (open_process(rpd, rpargv) <= 0) { fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); @@ -232,7 +266,7 @@ int *xp, *yp; return(0); } } - sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); + sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp); lseek(syncfd, 0L, 0); /* write new position */ write(syncfd, buf, strlen(buf)); fls.l_type = F_UNLCK; /* release sync file */ @@ -325,19 +359,24 @@ int xpos, ypos; exit(cleanup(1)); } /* check header from rpict */ - getheader(fromrp, NULL); - if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) { + guard_io(); + getheader(fromrp, NULL, NULL); + if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { fprintf(stderr, "%s: resolution mismatch from %s\n", progname, rpargv[0]); exit(cleanup(1)); } + unguard(); /* load new piece into buffer */ - for (y = 0; y < vr; y++) + for (y = 0; y < vr; y++) { + guard_io(); if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { fprintf(stderr, "%s: read error from %s\n", progname, rpargv[0]); exit(cleanup(1)); } + unguard(); + } #if MAXFORK /* fork so we don't slow rpict down */ if ((pid = fork()) > 0) { @@ -352,12 +391,14 @@ int xpos, ypos; #else pid = -1; /* no forking */ #endif - /* lock file section so NFS doesn't mess up */ - fls.l_whence = 0; fls.l_len = (long)vres*hmult*hres*sizeof(COLR); fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; +#if NFS + /* lock file section so NFS doesn't mess up */ + fls.l_whence = 0; fls.l_type = F_WRLCK; fcntl(outfd, F_SETLKW, &fls); +#endif /* write new piece to file */ if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) goto seekerr; @@ -376,8 +417,10 @@ int xpos, ypos; goto seekerr; } if (pid == -1) { /* fork failed */ +#if NFS fls.l_type = F_UNLCK; /* release lock */ fcntl(outfd, F_SETLKW, &fls); +#endif return(0); } _exit(0); /* else exit child process (releasing lock) */ @@ -388,3 +431,5 @@ writerr: fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); _exit(1); } + +#endif