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

Comparing ray/src/common/getpath.c (file contents):
Revision 2.17 by greg, Thu Feb 12 18:55:50 2004 UTC vs.
Revision 2.24 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 4 | Line 4 | static const char      RCSid[] = "$Id$";
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 paths.h
8   */
9  
10   #include "copyright.h"
11  
12 #include  <string.h>
12   #include  <ctype.h>
13  
15 #include  "rtio.h"
14   #include  "paths.h"
15  
16  
17 < #ifdef _WIN32
17 > #if defined(_WIN32) || defined(_WIN64)
18   static char *
19   core_getpath    /* wrapped below: expand fname, return full path */
20   #else
# Line 24 | Line 22 | char *
22   getpath /* expand fname, return full path */
23   #endif
24   (
25 < register char  *fname,
26 < register char  *searchpath,
27 < int  mode
25 >        char  *fname,
26 >        char  *searchpath,
27 >        int  mode
28   )
29   {
30          static char  pname[PATH_MAX];
31          char uname[512];
32 <        register char  *cp;
32 >        char  *cp;
33          int i;
34  
35 <        if (fname == NULL) { return(NULL); }
35 >        if (!fname | (fname == pname))
36 >                return(fname);
37  
38          pname[0] = '\0';                /* check for full specification */
39  
40 <        if (ISABS(fname)) { /* absolute path */
41 <                strncpy(pname, fname, sizeof(pname)-1);
40 >        if (ISABS(fname)) {             /* absolute path */
41 >                strlcpy(pname, fname, sizeof(pname));
42          } else {
43                  switch (*fname) {
44 <                        case '.':                               /* relative to cwd */
45 <                                strncpy(pname, fname, sizeof(pname)-1);
44 >                        case '.':       /* relative to cwd */
45 >                                strlcpy(pname, fname, sizeof(pname));
46                                  break;
47 <                        case '~':                               /* relative to home directory */
47 >                        case '~':       /* relative to home directory */
48                                  fname++;
49                                  cp = uname;
50 <                                for (i=0;i<sizeof(uname)&&*fname!='\0'&&!ISDIRSEP(*fname);i++)
50 >                                for (i = 0; i < sizeof(uname) && *fname
51 >                                                && !ISDIRSEP(*fname); i++)
52                                          *cp++ = *fname++;
53                                  *cp = '\0';
54                                  cp = gethomedir(uname, pname, sizeof(pname));
55                                  if(cp == NULL) return NULL;
56 <                                strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
56 >                                strlcat(pname, fname, sizeof(pname));
57                                  break;
58                  }
59          }
60          if (pname[0])           /* got it, check access if search requested */
61 <                return(searchpath==NULL||access(pname,mode)==0 ? pname : NULL);
61 >                return(!searchpath || access(pname,mode)==0 ? pname : NULL);
62  
63 <        if (searchpath == NULL) {                       /* don't search */
64 <                strncpy(pname, fname, sizeof(pname)-1);
63 >        if (!searchpath) {              /* no search? */
64 >                strlcpy(pname, fname, sizeof(pname));
65                  return(pname);
66          }
67          /* check search path */
68          do {
69                  cp = pname;
70 <                while (*searchpath && (*cp = *searchpath++) != PATHSEP) {
71 <                        cp++;
72 <                }
73 <                if (cp > pname && !ISDIRSEP(cp[-1])) {
70 >                while (*searchpath && (*cp = *searchpath++) != PATHSEP)
71 >                        cp += (cp-pname < sizeof(pname)-2);
72 >                if (cp > pname && !ISDIRSEP(cp[-1]))
73                          *cp++ = DIRSEP;
74 <                }
75 <                strncpy(cp, fname, sizeof(pname)-strlen(pname)-1);
74 >                *cp = '\0';
75 >                strlcat(pname, fname, sizeof(pname));
76                  if (access(pname, mode) == 0)           /* file accessable? */
77                          return(pname);
78          } while (*searchpath);
# Line 82 | Line 81 | int  mode
81   }
82  
83  
84 < #ifdef _WIN32
84 > #if defined(_WIN32) || defined(_WIN64)
85   /* This is a wrapper around the above, "emulating" access mode X_OK,
86     which is not supported on Windows.
87     If we see X_OK and the filename has no extension, then we'll remove
# Line 93 | Line 92 | int  mode
92   */
93   char *
94   getpath(        /* expand fname, return full path */
95 < register char  *ffname,
96 < register char  *searchpath,
97 < int  mode
95 >        char  *ffname,
96 >        char  *searchpath,
97 >        int  mode
98   )
99   {
100 <        register char  *cp;
100 >        char  *cp;
101          char fname[PATH_MAX];
102  
103 <        if (ffname == NULL)
104 <                return(NULL);
103 >        if (!ffname | (ffname == fname))
104 >                return(ffname);
105  
106          /* if we have a dot in the string, we assume there is a file name
107             extension present */
108          /* XXX We'd better test for .exe/.bat/.etc explicitly */
109 <        if (!(mode & X_OK) || (strrchr(ffname, '.') != NULL)) {
109 >        if (!(mode & X_OK) || strchr(ffname, '.') > ffname) {
110                  return core_getpath(ffname, searchpath, mode);
111          }
112  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines