4 |
|
/* |
5 |
|
* getpath.c - function to search for file in a list of directories |
6 |
|
* |
7 |
< |
* External symbols declared in standard.h |
7 |
> |
* External symbols declared in rtio.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
/* ==================================================================== |
11 |
< |
* The Radiance Software License, Version 1.0 |
12 |
< |
* |
13 |
< |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
< |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
< |
* |
16 |
< |
* Redistribution and use in source and binary forms, with or without |
17 |
< |
* modification, are permitted provided that the following conditions |
18 |
< |
* are met: |
19 |
< |
* |
20 |
< |
* 1. Redistributions of source code must retain the above copyright |
21 |
< |
* notice, this list of conditions and the following disclaimer. |
22 |
< |
* |
23 |
< |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
< |
* notice, this list of conditions and the following disclaimer in |
25 |
< |
* the documentation and/or other materials provided with the |
26 |
< |
* distribution. |
27 |
< |
* |
28 |
< |
* 3. The end-user documentation included with the redistribution, |
29 |
< |
* if any, must include the following acknowledgment: |
30 |
< |
* "This product includes Radiance software |
31 |
< |
* (http://radsite.lbl.gov/) |
32 |
< |
* developed by the Lawrence Berkeley National Laboratory |
33 |
< |
* (http://www.lbl.gov/)." |
34 |
< |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
< |
* if and wherever such third-party acknowledgments normally appear. |
36 |
< |
* |
37 |
< |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
< |
* and "The Regents of the University of California" must |
39 |
< |
* not be used to endorse or promote products derived from this |
40 |
< |
* software without prior written permission. For written |
41 |
< |
* permission, please contact [email protected]. |
42 |
< |
* |
43 |
< |
* 5. Products derived from this software may not be called "Radiance", |
44 |
< |
* nor may "Radiance" appear in their name, without prior written |
45 |
< |
* permission of Lawrence Berkeley National Laboratory. |
46 |
< |
* |
47 |
< |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
< |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
< |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
< |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
< |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
< |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
< |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
< |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
< |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
< |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
< |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
< |
* SUCH DAMAGE. |
59 |
< |
* ==================================================================== |
60 |
< |
* |
61 |
< |
* This software consists of voluntary contributions made by many |
62 |
< |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
< |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
< |
* <http://www.lbl.gov/>. |
65 |
< |
*/ |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "standard.h" |
12 |
> |
#include <ctype.h> |
13 |
|
|
14 |
+ |
#include "rtio.h" |
15 |
|
#include "paths.h" |
16 |
|
|
71 |
– |
#ifndef NIX |
72 |
– |
#include <pwd.h> |
73 |
– |
extern struct passwd *getpwnam(); |
74 |
– |
#endif |
17 |
|
|
18 |
< |
|
18 |
> |
#if defined(_WIN32) || defined(_WIN64) |
19 |
> |
static char * |
20 |
> |
core_getpath /* wrapped below: expand fname, return full path */ |
21 |
> |
#else |
22 |
|
char * |
23 |
< |
getpath(fname, searchpath, mode) /* expand fname, return full path */ |
79 |
< |
register char *fname; |
80 |
< |
register char *searchpath; |
81 |
< |
int mode; |
82 |
< |
{ |
83 |
< |
#ifndef NIX |
84 |
< |
struct passwd *pwent; |
23 |
> |
getpath /* expand fname, return full path */ |
24 |
|
#endif |
25 |
< |
static char pname[MAXPATH]; |
26 |
< |
register char *cp; |
25 |
> |
( |
26 |
> |
char *fname, |
27 |
> |
char *searchpath, |
28 |
> |
int mode |
29 |
> |
) |
30 |
> |
{ |
31 |
> |
static char pname[PATH_MAX]; |
32 |
> |
char uname[512]; |
33 |
> |
char *cp; |
34 |
> |
int i; |
35 |
|
|
36 |
< |
if (fname == NULL) |
37 |
< |
return(NULL); |
36 |
> |
if (!fname | (fname == pname)) |
37 |
> |
return(fname); |
38 |
|
|
39 |
|
pname[0] = '\0'; /* check for full specification */ |
40 |
< |
switch (*fname) { |
41 |
< |
CASEDIRSEP: /* relative to root */ |
42 |
< |
case '.': /* relative to cwd */ |
43 |
< |
strcpy(pname, fname); |
44 |
< |
break; |
45 |
< |
#ifndef NIX |
46 |
< |
case '~': /* relative to home directory */ |
47 |
< |
fname++; |
48 |
< |
if (*fname == '\0' || ISDIRSEP(*fname)) { /* ours */ |
49 |
< |
if ((cp = getenv("HOME")) == NULL) |
50 |
< |
return(NULL); |
51 |
< |
strcpy(pname, cp); |
52 |
< |
strcat(pname, fname); |
53 |
< |
break; |
40 |
> |
|
41 |
> |
if (ISABS(fname)) { /* absolute path */ |
42 |
> |
strlcpy(pname, fname, sizeof(pname)); |
43 |
> |
} else { |
44 |
> |
switch (*fname) { |
45 |
> |
case '.': /* relative to cwd */ |
46 |
> |
strlcpy(pname, fname, sizeof(pname)); |
47 |
> |
break; |
48 |
> |
case '~': /* relative to home directory */ |
49 |
> |
fname++; |
50 |
> |
cp = uname; |
51 |
> |
for (i = 0; i < sizeof(uname) && *fname |
52 |
> |
&& !ISDIRSEP(*fname); i++) |
53 |
> |
*cp++ = *fname++; |
54 |
> |
*cp = '\0'; |
55 |
> |
cp = gethomedir(uname, pname, sizeof(pname)); |
56 |
> |
if(cp == NULL) return NULL; |
57 |
> |
strlcat(pname, fname, sizeof(pname)); |
58 |
> |
break; |
59 |
|
} |
108 |
– |
cp = pname; /* user */ |
109 |
– |
do |
110 |
– |
*cp++ = *fname++; |
111 |
– |
while (*fname && !ISDIRSEP(*fname)); |
112 |
– |
*cp = '\0'; |
113 |
– |
if ((pwent = getpwnam(pname)) == NULL) |
114 |
– |
return(NULL); |
115 |
– |
strcpy(pname, pwent->pw_dir); |
116 |
– |
strcat(pname, fname); |
117 |
– |
break; |
118 |
– |
#endif |
60 |
|
} |
61 |
|
if (pname[0]) /* got it, check access if search requested */ |
62 |
< |
return(searchpath==NULL||access(pname,mode)==0 ? pname : NULL); |
62 |
> |
return(!searchpath || access(pname,mode)==0 ? pname : NULL); |
63 |
|
|
64 |
< |
if (searchpath == NULL) { /* don't search */ |
65 |
< |
strcpy(pname, fname); |
64 |
> |
if (!searchpath) { /* no search? */ |
65 |
> |
strlcpy(pname, fname, sizeof(pname)); |
66 |
|
return(pname); |
67 |
|
} |
68 |
< |
/* check search path */ |
68 |
> |
/* check search path */ |
69 |
|
do { |
70 |
|
cp = pname; |
71 |
|
while (*searchpath && (*cp = *searchpath++) != PATHSEP) |
72 |
< |
cp++; |
72 |
> |
cp += (cp-pname < sizeof(pname)-2); |
73 |
|
if (cp > pname && !ISDIRSEP(cp[-1])) |
74 |
|
*cp++ = DIRSEP; |
75 |
< |
strcpy(cp, fname); |
75 |
> |
*cp = '\0'; |
76 |
> |
strlcat(pname, fname, sizeof(pname)); |
77 |
|
if (access(pname, mode) == 0) /* file accessable? */ |
78 |
|
return(pname); |
79 |
|
} while (*searchpath); |
80 |
< |
/* not found */ |
80 |
> |
/* not found */ |
81 |
|
return(NULL); |
82 |
|
} |
83 |
+ |
|
84 |
+ |
|
85 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
86 |
+ |
/* This is a wrapper around the above, "emulating" access mode X_OK, |
87 |
+ |
which is not supported on Windows. |
88 |
+ |
If we see X_OK and the filename has no extension, then we'll remove |
89 |
+ |
the X_OK from the mode, append ".exe" to the file name, and search |
90 |
+ |
with the resulting string. If that fails, we'll try again with ".bat". |
91 |
+ |
Theoretically, we might still not have execute rights on a file we find |
92 |
+ |
like this, but that's rare enough not to be worth checking the ACLs. |
93 |
+ |
*/ |
94 |
+ |
char * |
95 |
+ |
getpath( /* expand fname, return full path */ |
96 |
+ |
char *ffname, |
97 |
+ |
char *searchpath, |
98 |
+ |
int mode |
99 |
+ |
) |
100 |
+ |
{ |
101 |
+ |
char *cp; |
102 |
+ |
char fname[PATH_MAX]; |
103 |
+ |
|
104 |
+ |
if (!ffname | (ffname == fname)) |
105 |
+ |
return(ffname); |
106 |
+ |
|
107 |
+ |
/* if we have a dot in the string, we assume there is a file name |
108 |
+ |
extension present */ |
109 |
+ |
/* XXX We'd better test for .exe/.bat/.etc explicitly */ |
110 |
+ |
if (!(mode & X_OK) || strchr(ffname, '.') > ffname) { |
111 |
+ |
return core_getpath(ffname, searchpath, mode); |
112 |
+ |
} |
113 |
+ |
|
114 |
+ |
/* We're looking for an executable, Windows doesn't have X_OK. */ |
115 |
+ |
mode &= ~X_OK; |
116 |
+ |
/* Append .exe */ |
117 |
+ |
strncpy(fname, ffname, sizeof(fname)-5); |
118 |
+ |
strcat(fname, ".exe"); |
119 |
+ |
cp = core_getpath(fname, searchpath, mode); |
120 |
+ |
if (cp != NULL) return cp; |
121 |
+ |
|
122 |
+ |
/* Try with .bat this time... */ |
123 |
+ |
strncpy(fname, ffname, sizeof(fname)-5); |
124 |
+ |
strcat(fname, ".bat"); |
125 |
+ |
cp = core_getpath(fname, searchpath, mode); |
126 |
+ |
return cp; |
127 |
+ |
} |
128 |
+ |
#endif /* _WIN32 */ |
129 |
+ |
|
130 |
+ |
|
131 |
+ |
#ifdef TEST_MODULE |
132 |
+ |
int main() |
133 |
+ |
{ |
134 |
+ |
char * fp; |
135 |
+ |
char fmt[] = "%15s %-10s %s: %s\n"; |
136 |
+ |
|
137 |
+ |
fp = getpath("rayinit.cal", getenv("RAYPATH"), R_OK); |
138 |
+ |
printf(fmt, "rayinit.cal", "RAYPATH", "R_OK", fp); |
139 |
+ |
fp = getpath("mkillum", getenv("PATH"), X_OK); |
140 |
+ |
printf(fmt, "mkillum", "PATH", "X_OK", fp); |
141 |
+ |
fp = getpath("/", getenv("PATH"), W_OK); |
142 |
+ |
printf(fmt, "/", "PATH", "W_OK", fp); |
143 |
+ |
fp = getpath("~", getenv("PATH"), F_OK); |
144 |
+ |
printf(fmt, "~", "PATH", "F_OK", fp); |
145 |
+ |
printf("Undefining HOME and HOMEPATH\n"); |
146 |
+ |
unsetenv("HOME"); |
147 |
+ |
unsetenv("HOMEPATH"); |
148 |
+ |
fp = getpath("~", getenv("PATH"), F_OK); |
149 |
+ |
printf(fmt, "~", "PATH", "F_OK", fp); |
150 |
+ |
fp = getpath("~lp/blah", getenv("PATH"), F_OK); |
151 |
+ |
printf(fmt, "~lp/blah", "PATH", "F_OK", fp); |
152 |
+ |
} |
153 |
+ |
#endif |
154 |
+ |
|