ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/getpath.c
Revision: 2.6
Committed: Tue Sep 8 10:04:34 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +7 -7 lines
Log Message:
modified testing of DIRSEP to allow multiple possible values

File Contents

# User Rev Content
1 greg 2.5 /* 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     * getpath.c - function to search for file in a list of directories
9     */
10    
11 greg 2.6 #include "paths.h"
12 greg 2.5
13 greg 1.1 #define NULL 0
14    
15 greg 2.4 #ifndef NIX
16 greg 1.1 #include <pwd.h>
17 greg 2.5 extern struct passwd *getpwnam();
18 greg 2.4 #endif
19 greg 1.1
20 greg 2.6 extern char *strcpy(), *strcat(), *getenv();
21 greg 1.1
22    
23     char *
24     getpath(fname, searchpath, mode) /* expand fname, return full path */
25     register char *fname;
26     register char *searchpath;
27     int mode;
28     {
29 greg 2.4 #ifndef NIX
30     struct passwd *pwent;
31     #endif
32 greg 2.6 static char pname[MAXPATH];
33 greg 1.1 register char *cp;
34    
35     if (fname == NULL)
36     return(NULL);
37    
38     switch (*fname) {
39 greg 2.6 CASEDIRSEP: /* relative to root */
40 greg 1.1 case '.': /* relative to cwd */
41     strcpy(pname, fname);
42     return(pname);
43 greg 2.3 #ifndef NIX
44 greg 1.1 case '~': /* relative to home directory */
45     fname++;
46 greg 2.6 if (*fname == '\0' || ISDIRSEP(*fname)) { /* ours */
47 greg 1.1 if ((cp = getenv("HOME")) == NULL)
48     return(NULL);
49     strcpy(pname, cp);
50     strcat(pname, fname);
51     return(pname);
52     }
53     cp = pname; /* user */
54     do
55     *cp++ = *fname++;
56 greg 2.6 while (*fname && !ISDIRSEP(*fname));
57 greg 1.1 *cp = '\0';
58     if ((pwent = getpwnam(pname)) == NULL)
59     return(NULL);
60     strcpy(pname, pwent->pw_dir);
61     strcat(pname, fname);
62     return(pname);
63 greg 2.3 #endif
64 greg 1.1 }
65    
66     if (searchpath == NULL) { /* don't search */
67     strcpy(pname, fname);
68     return(pname);
69     }
70     /* check search path */
71     do {
72     cp = pname;
73 greg 2.3 while (*searchpath && (*cp = *searchpath++) != PATHSEP)
74     cp++;
75 greg 2.6 if (cp > pname && !ISDIRSEP(cp[-1]))
76 greg 2.3 *cp++ = DIRSEP;
77 greg 1.1 strcpy(cp, fname);
78     if (access(pname, mode) == 0) /* file accessable? */
79     return(pname);
80     } while (*searchpath);
81     /* not found */
82     return(NULL);
83     }