| 1 | greg | 2.2 | /* Copyright (c) 1992 Regents of the University of California */ | 
| 2 | greg | 1.1 |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Find and open a Radiance library file. | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include <stdio.h> | 
| 12 |  |  |  | 
| 13 |  |  | #ifndef DEFPATH | 
| 14 |  |  | #define DEFPATH         ":/usr/local/lib/ray" | 
| 15 |  |  | #endif | 
| 16 |  |  | #ifndef ULIBVAR | 
| 17 |  |  | #define ULIBVAR         "RAYPATH" | 
| 18 |  |  | #endif | 
| 19 |  |  |  | 
| 20 | greg | 1.2 | char  *libpath = NULL;          /* library search path */ | 
| 21 | greg | 1.1 |  | 
| 22 | greg | 1.2 |  | 
| 23 | greg | 1.1 | FILE * | 
| 24 |  |  | fropen(fname)                   /* find file and open for reading */ | 
| 25 |  |  | register char  *fname; | 
| 26 |  |  | { | 
| 27 |  |  | extern char  *strcpy(), *getenv(); | 
| 28 |  |  | FILE  *fp; | 
| 29 |  |  | char  pname[256]; | 
| 30 |  |  | register char  *sp, *cp; | 
| 31 |  |  |  | 
| 32 |  |  | if (fname == NULL) | 
| 33 |  |  | return(NULL); | 
| 34 |  |  |  | 
| 35 |  |  | if (fname[0] == '/' || fname[0] == '.') /* absolute path */ | 
| 36 |  |  | return(fopen(fname, "r")); | 
| 37 |  |  |  | 
| 38 | greg | 1.2 | if (libpath == NULL) {                  /* get search path */ | 
| 39 |  |  | libpath = getenv(ULIBVAR); | 
| 40 |  |  | if (libpath == NULL) | 
| 41 |  |  | libpath = DEFPATH; | 
| 42 | greg | 1.1 | } | 
| 43 |  |  | /* check search path */ | 
| 44 | greg | 1.2 | sp = libpath; | 
| 45 | greg | 1.1 | do { | 
| 46 |  |  | cp = pname; | 
| 47 |  |  | while (*sp && (*cp = *sp++) != ':') | 
| 48 | greg | 2.2 | if (*cp == '\\') {              /* escape */ | 
| 49 |  |  | if (*sp) *cp++ = *sp++; | 
| 50 |  |  | } else | 
| 51 |  |  | cp++; | 
| 52 | greg | 1.1 | if (cp > pname && cp[-1] != '/') | 
| 53 |  |  | *cp++ = '/'; | 
| 54 |  |  | strcpy(cp, fname); | 
| 55 |  |  | if ((fp = fopen(pname, "r")) != NULL) | 
| 56 |  |  | return(fp);                     /* got it! */ | 
| 57 |  |  | } while (*sp); | 
| 58 |  |  | /* not found */ | 
| 59 |  |  | return(NULL); | 
| 60 |  |  | } |