ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/xform.c
(Generate patch)

Comparing ray/src/gen/xform.c (file contents):
Revision 2.13 by greg, Tue Jun 18 21:12:51 1996 UTC vs.
Revision 2.35 by schorsch, Sun Nov 16 09:23:46 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  xform.c - program to transform object files.
6   *              Transformations must preserve aspect ratio.
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   *     11/6/86          Finally added error checking!
10   */
11  
15 #include  "standard.h"
16
17 #include  "paths.h"
18
12   #include  <ctype.h>
13  
14 + #include  "platform.h"
15 + #include  "paths.h"
16 + #include  "rtprocess.h" /* win_popen() */
17 + #include  "rtio.h"
18 + #include  "rtmath.h"
19   #include  "object.h"
22
20   #include  "otypes.h"
21  
22   int  xac;                               /* global xform argument count */
# Line 29 | Line 26 | int  xfa;                              /* start of xf arguments */
26   XF  tot;                                /* total transformation */
27   int  reverse;                           /* boolean true if scene mirrored */
28  
29 < int  invert = 0;                        /* boolean true to invert surfaces */
29 > int  invert;                            /* boolean true to invert surfaces */
30  
31 < int  expand = 1;                        /* boolean true to expand commands */
31 > int  expand;                            /* boolean true to expand commands */
32  
33 < char  *newmod = NULL;                   /* new modifier for surfaces */
33 > char  *newmod;                          /* new modifier for surfaces */
34  
35 < char  *idprefix = NULL;                 /* prefix for object identifiers */
35 > char  *idprefix;                        /* prefix for object identifiers */
36  
37 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
37 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
38  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
39   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
40  
41   int  nrept = 1;                         /* number of array repetitions */
42  
43 < extern char  *malloc(), *fgetword();
43 > int stdinused = 0;                      /* stdin has been used by -f option? */
44  
45 < char  *mainfn[MAXPATH];                 /* main file name */
45 > char  mainfn[PATH_MAX];                 /* main file name */
46   FILE  *mainfp = NULL;                   /* main file pointer */
47  
48   #define  progname  (xav[0])
49  
50 + static int doargf(int ac, char **av, int fi);
51 + static int doarray(int ac, char **av, int ai);
52 + static void xform(char *name, FILE *fin);
53 + static void xfcomm(char *fname, FILE *fin);
54 + static void xfobject(char *fname, FILE *fin);
55 + static int addxform(FILE *fin);
56 + static int alias(FILE *fin);
57 + static void initotypes(void);
58 + static void openmain(char *iname);
59  
60 < main(argc, argv)                /* get transform options and transform file */
61 < int  argc;
62 < char  *argv[];
60 >
61 > int
62 > main(           /* get transform options and transform file */
63 >        int  argc,
64 >        char  *argv[]
65 > )
66   {
67 <        char  *fname;
67 >        int  mal_prefix = 0;
68          int  a;
69 <                                        /* check for array */
69 >                                        /* check for argument list file */
70          for (a = 1; a < argc; a++)
71 +                if (!strcmp(argv[a], "-f"))
72 +                        return(doargf(argc, argv, a));
73 +                                        /* check for regular array */
74 +        for (a = 1; a < argc; a++)
75                  if (!strcmp(argv[a], "-a"))
76                          return(doarray(argc, argv, a));
77  
78 <        initotypes();
78 >        initotypes();                   /* initialize */
79 >        invert = 0;
80 >        expand = 1;
81 >        newmod = NULL;
82 >        idprefix = NULL;
83  
84          for (a = 1; a < argc; a++) {
85                  if (argv[a][0] == '-')
86                          switch (argv[a][1]) {
87                          case 'm':
88 <                                if (argv[a][2] || a+1 >= argc)
88 >                                if (argv[a][2] | (a+1 >= argc))
89                                          break;
90 <                                newmod = argv[++a];
90 >                                a++;
91 >                                if (newmod == NULL)
92 >                                        newmod = argv[a];
93                                  continue;
94                          case 'n':
95 <                                if (argv[a][2] || a+1 >= argc)
95 >                                if (argv[a][2] | (a+1 >= argc))
96                                          break;
97 <                                idprefix = argv[++a];
97 >                                a++;
98 >                                if (idprefix == NULL)
99 >                                        idprefix = argv[a];
100 >                                else {
101 >                                        register char   *newp;
102 >                                        newp = (char *)malloc(strlen(idprefix)+
103 >                                                        strlen(argv[a])+2);
104 >                                        if (newp == NULL)
105 >                                                exit(2);
106 >                                        sprintf(newp, "%s.%s",
107 >                                                        idprefix, argv[a]);
108 >                                        if (mal_prefix++)
109 >                                                free((void *)idprefix);
110 >                                        idprefix = newp;
111 >                                }
112                                  continue;
113                          case 'c':
114                                  if (argv[a][2])
# Line 94 | Line 123 | char  *argv[];
123                          case 'I':
124                                  if (argv[a][2])
125                                          break;
126 <                                invert = 1;
126 >                                invert = !invert;
127                                  continue;
128                          }
129                  break;
# Line 105 | Line 134 | char  *argv[];
134  
135          a += xf(&tot, argc-a, argv+a);
136  
137 <        if (reverse = tot.sca < 0.0)
137 >        if ( (reverse = tot.sca < 0.0) )
138                  tot.sca = -tot.sca;
139          if (invert)
140                  reverse = !reverse;
# Line 124 | Line 153 | char  *argv[];
153          putchar('\n');
154                                          /* transform input */
155          if (xac == argc) {
156 +                if (stdinused) {
157 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
158 +                                        argv[0]);
159 +                        exit(1);
160 +                }
161                  openmain(NULL);
162                  xform(mainfn, mainfp);
163          } else
# Line 132 | Line 166 | char  *argv[];
166                          xform(mainfn, mainfp);
167                  }
168  
169 +        if (mal_prefix)
170 +                free((void *)idprefix);
171          return(0);
172   }
173  
174  
175 < doarray(ac, av, ai)                     /* make array */
176 < char  **av;
177 < int  ac, ai;
175 > int
176 > doargf(                 /* take argument list from file */
177 >        int ac,
178 >        char  **av,
179 >        int fi
180 > )
181   {
182 +        int  inquote;
183          char  *newav[256], **avp;
184 +        char  argbuf[1024];
185 +        char  newid[128];
186 +        char  *oldid;
187 +        register char   *cp;
188 +        FILE    *argfp;
189 +        int  n, i, k, newac, err;
190 +        
191 +        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
192 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
193 +                exit(1);
194 +        }
195 +        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
196 +                if (stdinused++) {
197 +                        fprintf(stderr,
198 +                                "%s: cannot use stdin more than once\n",
199 +                                        av[0]);
200 +                        exit(1);
201 +                }
202 +                argfp = stdin;
203 +                n = 100;                /* we just don't know! */
204 +        } else {
205 +                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
206 +                        fprintf(stderr,
207 +                                "%s: cannot open argument file \"%s\"\n",
208 +                                        av[0], av[fi+1]);
209 +                        exit(1);
210 +                }
211 +                n = 0;                  /* count number of lines in file */
212 +                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
213 +                        n += argbuf[0] && argbuf[0] != '#';
214 +                if (!n) {
215 +                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
216 +                                        av[0], av[fi+1]);
217 +                        exit(1);
218 +                }
219 +                nrept *= n;
220 +                rewind(argfp);
221 +        }
222 +        err = 0; k = 0;                 /* read each arg list and call main */
223 +        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
224 +                if (!argbuf[0] || argbuf[0] == '#')
225 +                        continue;
226 +                avp = newav+2;
227 +                avp[0] = av[0];
228 +                for (i = 1; i < fi; i++)
229 +                        avp[i] = av[i];
230 +                newac = i;
231 +                cp = argbuf;            /* parse new words */
232 +                if (*cp == '!') cp++;
233 +                if (!strncmp(cp, "xform ", 6)) cp += 6;
234 +                inquote = 0;
235 +                for ( ; ; ) {
236 +                skipspaces:
237 +                        while (isspace(*cp))    /* nullify spaces */
238 +                                *cp++ = '\0';
239 +                        if ((*cp == '"') | (*cp == '\''))
240 +                                inquote = *cp++;
241 +                        if (!*cp)               /* all done? */
242 +                                break;
243 +                        if (cp[0] == '\\' && cp[1])
244 +                                if (*++cp == '\n')
245 +                                        goto skipspaces;
246 +                        avp[newac++] = cp;      /* add argument to list */
247 +                        if (inquote) {
248 +                                while (*++cp)
249 +                                        if (*cp == inquote) {
250 +                                                *cp++ = '\0';
251 +                                                break;
252 +                                        }
253 +                        } else {
254 +                                while (*++cp && !isspace(*cp))
255 +                                        ;
256 +                        }
257 +                }
258 +                for (i = fi+2; i < ac; i++)
259 +                        avp[newac++] = av[i];
260 +                avp[newac] = NULL;
261 +                oldid = NULL;
262 +                for (i = 2; i < newac; i++)
263 +                        if (!strcmp(avp[i-1], "-n")) {
264 +                                oldid = avp[i];
265 +                                avp[i] = newid;
266 +                                break;
267 +                        }
268 +                if (oldid == NULL) {
269 +                        newav[0] = av[0];
270 +                        newav[1] = "-n";
271 +                        newav[2] = newid;
272 +                        avp = newav;
273 +                        newac += 2;
274 +                }
275 +                if (oldid == NULL)
276 +                        sprintf(newid, "i%d", k);
277 +                else
278 +                        sprintf(newid, "%s.%d", oldid, k);
279 +                err |= main(newac, avp);
280 +                k++;
281 +        }
282 +        fclose(argfp);
283 +        return(err);
284 + }
285 +
286 +
287 + int
288 + doarray(                        /* make array */
289 +        int  ac,
290 +        char  **av,
291 +        int  ai
292 + )
293 + {
294 +        char  *newav[256], **avp;
295          char  newid[128], repts[32];
296          char  *oldid = NULL;
297          int  n, i, err;
298          
299 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
300 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
301 +                exit(1);
302 +        }
303 +        nrept *= n;
304          avp = newav+2;
305          avp[0] = av[0];
306          for (i = 1; i < ac; i++)
# Line 163 | Line 319 | int  ac, ai;
319                  avp = newav;
320                  ac += 2;
321          }
166        nrept *= n = atoi(av[ai+1]);
322          err = 0;
323          for (i = 0; i < n; i++) {
324                  if (oldid == NULL)
# Line 177 | Line 332 | int  ac, ai;
332   }
333  
334  
335 < xform(name, fin)                        /* transform stream by tot.xfm */
336 < char  *name;
337 < register FILE  *fin;
335 > void
336 > xform(                  /* transform stream by tot.xfm */
337 >        char  *name,
338 >        register FILE  *fin
339 > )
340   {
341          int  nobjs = 0;
342          register int  c;
# Line 210 | Line 367 | register FILE  *fin;
367   }
368  
369  
370 < xfcomm(fname, fin)                      /* transform a command */
371 < char  *fname;
372 < FILE  *fin;
370 > void
371 > xfcomm(                 /* transform a command */
372 >        char  *fname,
373 >        FILE  *fin
374 > )
375   {
217        extern FILE  *popen();
218        extern char  *fgetline();
376          FILE  *pin;
377          char  buf[512];
378          int  i;
# Line 232 | Line 389 | FILE  *fin;
389          } else {
390                  printf("\n%s", buf);
391                  if (xac > 1) {
392 <                        printf(" | %s", xav[0]);
392 >                        printf(" | %s -e", xav[0]);     /* expand next time */
393                          for (i = 1; i < xac; i++)
394 <                                printf(" %s", xav[i]);
394 >                                if (i >= xfa || strcmp(xav[i], "-c"))
395 >                                        printf(" %s", xav[i]);
396                  }
397                  putchar('\n');
398          }
399   }
400  
401  
402 < xfobject(fname, fin)                            /* transform an object */
403 < char  *fname;
404 < FILE  *fin;
402 > void
403 > xfobject(                               /* transform an object */
404 >        char  *fname,
405 >        FILE  *fin
406 > )
407   {
248        extern char  *strcpy();
408          char  typ[16], nam[MAXSTR];
409          int  fn;
410                                                  /* modifier and type */
# Line 277 | Line 436 | FILE  *fin;
436   }
437  
438  
439 < o_default(fin)                  /* pass on arguments unchanged */
440 < FILE  *fin;
439 > int
440 > o_default(                      /* pass on arguments unchanged */
441 >        FILE  *fin
442 > )
443   {
444          register int  i;
445          FUNARGS  fa;
# Line 287 | Line 448 | FILE  *fin;
448                  return(-1);
449                                          /* string arguments */
450          printf("%d", fa.nsargs);
451 <        for (i = 0; i < fa.nsargs; i++)
452 <                printf(" %s", fa.sarg[i]);
451 >        for (i = 0; i < fa.nsargs; i++) {
452 >                fputc(' ', stdout);
453 >                fputword(fa.sarg[i], stdout);
454 >        }
455          printf("\n");
456   #ifdef  IARGS
457                                          /* integer arguments */
# Line 309 | Line 472 | FILE  *fin;
472   }
473  
474  
475 < addxform(fin)                   /* add xf arguments to strings */
476 < FILE  *fin;
475 > int
476 > addxform(                       /* add xf arguments to strings */
477 >        FILE  *fin
478 > )
479   {
480          register int  i;
481          int  resetarr = 0;
# Line 322 | Line 487 | FILE  *fin;
487          if (xac > xfa && strcmp(xav[xfa], "-i"))
488                  resetarr = 2;
489          printf("%d", fa.nsargs + resetarr + xac-xfa);
490 <        for (i = 0; i < fa.nsargs; i++)
491 <                printf(" %s", fa.sarg[i]);
490 >        for (i = 0; i < fa.nsargs; i++) {
491 >                fputc(' ', stdout);
492 >                fputword(fa.sarg[i], stdout);
493 >        }
494          if (resetarr)
495                  printf(" -i 1");
496          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 349 | Line 516 | FILE  *fin;
516  
517  
518   int
519 < otype(ofname)                   /* get object function number from its name */
520 < register char  *ofname;
519 > alias(                  /* transfer alias */
520 >        FILE  *fin
521 > )
522   {
355        register int  i;
356
357        for (i = 0; i < NUMTYPES; i++)
358                if (!strcmp(ofun[i].funame, ofname))
359                        return(i);
360
361        return(-1);             /* not found */
362 }
363
364
365 alias(fin)                      /* transfer alias */
366 FILE  *fin;
367 {
523          char  aliasnm[MAXSTR];
524  
525          if (fgetword(aliasnm, MAXSTR, fin) == NULL)
# Line 374 | Line 529 | FILE  *fin;
529   }
530  
531  
532 < m_glow(fin)                     /* transform arguments for proximity light */
533 < FILE  *fin;
532 > int
533 > m_glow(                 /* transform arguments for proximity light */
534 >        FILE  *fin
535 > )
536   {
537          FUNARGS  fa;
538  
# Line 392 | Line 549 | FILE  *fin;
549   }
550  
551  
552 < m_spot(fin)                     /* transform arguments for spotlight */
553 < FILE  *fin;
552 > int
553 > m_spot(                 /* transform arguments for spotlight */
554 >        FILE  *fin
555 > )
556   {
557          FVECT  v;
558          FUNARGS  fa;
# Line 412 | Line 571 | FILE  *fin;
571   }
572  
573  
574 < m_mist(fin)             /* transform arguments for mist */
575 < FILE  *fin;
574 > int
575 > m_mist(         /* transform arguments for mist */
576 >        FILE  *fin
577 > )
578   {
579          FUNARGS  fa;
580          int     i;
581  
582          if (readfargs(&fa, fin) != 1)
583                  return(-1);
584 <        if (fa.nfargs > 5)
584 >        if (fa.nfargs > 7)
585                  return(-1);
586          printf("%d", fa.nsargs);
587          if (idprefix == NULL)
# Line 446 | Line 607 | FILE  *fin;
607          if (fa.nfargs > 2)
608                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
609                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
610 <        if (fa.nfargs > 3)
611 <                printf(" %12.6g", fa.farg[3]);
451 <        if (fa.nfargs > 4)
452 <                printf(" %12.6g", fa.farg[4]);
610 >        for (i = 3; i < fa.nfargs; i++)
611 >                printf(" %12.6g", fa.farg[i]);
612          printf("\n");
613          freefargs(&fa);
614          return(0);
615   }
616  
617  
618 < m_dielectric(fin)               /* transform arguments for dielectric */
619 < FILE  *fin;
618 > int
619 > m_dielectric(           /* transform arguments for dielectric */
620 >        FILE  *fin
621 > )
622   {
623          FUNARGS  fa;
624  
# Line 476 | Line 637 | FILE  *fin;
637   }
638  
639  
640 < m_interface(fin)                /* transform arguments for interface */
641 < FILE  *fin;
640 > int
641 > m_interface(            /* transform arguments for interface */
642 >        FILE  *fin
643 > )
644   {
645          FUNARGS  fa;
646  
# Line 501 | Line 664 | FILE  *fin;
664   }
665  
666  
667 < text(fin)                       /* transform text arguments */
668 < FILE  *fin;
667 > int
668 > text(                   /* transform text arguments */
669 >        FILE  *fin
670 > )
671   {
672          int  i;
673          FVECT  v;
# Line 514 | Line 679 | FILE  *fin;
679                  return(-1);
680                                          /* string arguments */
681          printf("%d", fa.nsargs);
682 <        for (i = 0; i < fa.nsargs; i++)
683 <                printf(" %s", fa.sarg[i]);
682 >        for (i = 0; i < fa.nsargs; i++) {
683 >                fputc(' ', stdout);
684 >                fputword(fa.sarg[i], stdout);
685 >        }
686          printf("\n0\n%d\n", fa.nfargs);
687                                          /* anchor point */
688          multp3(v, fa.farg, tot.xfm);
# Line 538 | Line 705 | FILE  *fin;
705   }
706  
707  
708 < o_source(fin)                   /* transform source arguments */
709 < FILE  *fin;
708 > int
709 > o_source(                       /* transform source arguments */
710 >        FILE  *fin
711 > )
712   {
713          FVECT  dv;
714          FUNARGS  fa;
# Line 559 | Line 728 | FILE  *fin;
728   }
729  
730  
731 < o_sphere(fin)                   /* transform sphere arguments */
732 < FILE  *fin;
731 > int
732 > o_sphere(                       /* transform sphere arguments */
733 >        FILE  *fin
734 > )
735   {
736          FVECT  cent;
737          double  rad;
# Line 583 | Line 754 | FILE  *fin;
754   }
755  
756  
757 < o_face(fin)                     /* transform face arguments */
758 < FILE  *fin;
757 > int
758 > o_face(                 /* transform face arguments */
759 >        FILE  *fin
760 > )
761   {
762          FVECT  p;
763          register int  i;
# Line 609 | Line 782 | FILE  *fin;
782   }
783  
784  
785 < o_cone(fin)                     /* transform cone and cup arguments */
786 < FILE  *fin;
785 > int
786 > o_cone(                 /* transform cone and cup arguments */
787 >        FILE  *fin
788 > )
789   {
790          FVECT  p0, p1;
791          double  r0, r1;
# Line 636 | Line 811 | FILE  *fin;
811   }
812  
813  
814 < o_cylinder(fin)                 /* transform cylinder and tube arguments */
815 < FILE  *fin;
814 > int
815 > o_cylinder(                     /* transform cylinder and tube arguments */
816 >        FILE  *fin
817 > )
818   {
819          FVECT  p0, p1;
820          double  rad;
# Line 661 | Line 838 | FILE  *fin;
838   }
839  
840  
841 < o_ring(fin)                     /* transform ring arguments */
842 < FILE  *fin;
841 > int
842 > o_ring(                 /* transform ring arguments */
843 >        FILE  *fin
844 > )
845   {
846          FVECT  p0, pd;
847          double  r0, r1;
# Line 692 | Line 871 | FILE  *fin;
871   }
872  
873  
874 < initotypes()                    /* initialize ofun[] array */
874 > void
875 > initotypes(void)                        /* initialize ofun[] array */
876   {
877          register int  i;
878  
879          if (ofun[OBJ_SOURCE].funp == o_source)
880                  return;                 /* done already */
701                                        /* alias is additional */
702        ofun[ALIAS].funame = ALIASID;
703        ofun[ALIAS].flags = 0;
881                                          /* functions get new transform */
882 <        for (i = 0; i < NUMTYPES; i++)
882 >        for (i = 0; i < NUMOTYPE; i++)
883                  if (hasfunc(i))
884                          ofun[i].funp = addxform;
885                                          /* special cases */
# Line 715 | Line 892 | initotypes()                   /* initialize ofun[] array */
892          ofun[OBJ_CYLINDER].funp =
893          ofun[OBJ_TUBE].funp = o_cylinder;
894          ofun[OBJ_RING].funp = o_ring;
895 <        ofun[OBJ_INSTANCE].funp = addxform;
895 >        ofun[OBJ_INSTANCE].funp =
896 >        ofun[OBJ_MESH].funp = addxform;
897          ofun[MAT_GLOW].funp = m_glow;
898          ofun[MAT_SPOT].funp = m_spot;
899          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 724 | Line 902 | initotypes()                   /* initialize ofun[] array */
902          ofun[PAT_CTEXT].funp =
903          ofun[PAT_BTEXT].funp =
904          ofun[MIX_TEXT].funp = text;
905 <        ofun[ALIAS].funp = alias;
905 >        ofun[MOD_ALIAS].funp = alias;
906                                          /* surface inverses */
907          tinvers[OBJ_FACE] = OBJ_FACE;
908          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 740 | Line 918 | initotypes()                   /* initialize ofun[] array */
918  
919  
920   #ifdef  OLDXFORM
921 < openmain(fname)
922 < char  *fname;
921 > void
922 > openmain(
923 >        char  *fname
924 > )
925   {
926          if (fname == NULL) {
927                  strcpy(mainfn, "standard input");
# Line 763 | Line 943 | char  *fname;
943          strcpy(mainfn, fname);
944   }
945   #else
946 < openmain(fname)         /* open fname for input, changing to its directory */
947 < char  *fname;
946 > void
947 > openmain(               /* open input, changing directory for file */
948 >        char  *iname
949 > )
950   {
951 <        extern FILE  *tmpfile();
952 <        extern char  *getlibpath(), *getpath();
771 <        static char  origdir[MAXPATH];
772 <        static char  curfn[MAXPATH];
951 >        static char  origdir[PATH_MAX];
952 >        static char  curfn[PATH_MAX];
953          static int  diffdir;
954          register char  *fpath;
955  
956 <        if (fname == NULL) {                    /* standard input */
956 >        if (iname == NULL) {                    /* standard input */
957                  if (mainfp == NULL) {
958                          register int  c;
959                          strcpy(mainfn, "standard input");
# Line 790 | Line 970 | char  *fname;
970                          }
971                          while ((c = getc(stdin)) != EOF)
972                                  putc(c, mainfp);
793                        fclose(stdin);
973                  }
974                  rewind(mainfp);                 /* rewind copy */
975                  return;
976          }
977          if (mainfp == NULL) {                   /* first call, initialize */
978 <                getwd(origdir);
979 <        } else if (!strcmp(fname, curfn)) {     /* just need to rewind? */
978 >                getcwd(origdir, sizeof(origdir));
979 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
980                  rewind(mainfp);
981                  return;
982          } else {                                /* else close old stream */
983                  fclose(mainfp);
984 <                if (diffdir) {
984 >                mainfp = NULL;
985 >                if (diffdir) {                  /* return to our directory */
986                          chdir(origdir);
987                          diffdir = 0;
988                  }
989          }
990 <        strcpy(curfn, fname);                   /* remember file name */
991 <                                                /* get full path */
992 <        if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) {
990 >        strcpy(curfn, iname);                   /* remember input name */
991 >                                                /* get full path for file */
992 >        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
993                  fprintf(stderr, "%s: cannot find file \"%s\"\n",
994 <                                progname, fname);
994 >                                progname, iname);
995                  exit(1);
996          }
997          if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
# Line 839 | Line 1019 | char  *fname;
1019                          diffdir++;
1020                  }
1021                                                  /* get final path component */
1022 <                for (fpath = fname+strlen(fname);
1023 <                                fpath > fname && !ISDIRSEP(fpath[-1]); fpath--)
1022 >                for (fpath = iname+strlen(iname);
1023 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
1024                          ;
1025          }
1026 <                                                /* open the file */
1026 >                                                /* finally, open the file */
1027          if ((mainfp = fopen(fpath, "r")) == NULL) {
1028                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
1029                                  progname, mainfn);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines