ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/getpath.c
Revision: 2.8
Committed: Thu Aug 26 10:14:32 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +6 -3 lines
Log Message:
changed behavior to check access whenever search path is given

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