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.12 by schorsch, Mon Jun 30 14:59:11 2003 UTC vs.
Revision 2.22 by greg, Mon Nov 22 18:23:56 2021 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 rtio.h
8   */
9  
10   #include "copyright.h"
11  
12 #include  <string.h>
12   #include  <ctype.h>
14 #ifndef  RHAS_GETPWNAM
15  #include  <pwd.h>
16  #include  <sys/types.h>
17 #endif
13  
14 + #include  "rtio.h"
15   #include  "paths.h"
16  
17  
18 <
23 < #ifdef _WIN32
18 > #if defined(_WIN32) || defined(_WIN64)
19   static char *
20   core_getpath    /* wrapped below: expand fname, return full path */
21   #else
# Line 28 | Line 23 | char *
23   getpath /* expand fname, return full path */
24   #endif
25   (
26 < register char  *fname,
27 < register char  *searchpath,
28 < int  mode
26 >        char  *fname,
27 >        char  *searchpath,
28 >        int  mode
29   )
30   {
36 #ifndef  RHAS_GETPWNAM
37        struct passwd  *pwent;
38 #endif
31          static char  pname[PATH_MAX];
32 <        register char  *cp;
32 >        char uname[512];
33 >        char  *cp;
34 >        int i;
35  
36 <        if (fname == NULL) { return(NULL); }
36 >        if (!fname | (fname == pname))
37 >                return(fname);
38  
39          pname[0] = '\0';                /* check for full specification */
40  
41 <        if (ISABS(fname)) { /* Can't use CASEDIRSEP below on Windows */
42 <                strncpy(pname, fname, sizeof(pname)-1);
41 >        if (ISABS(fname)) {             /* absolute path */
42 >                strlcpy(pname, fname, sizeof(pname));
43          } else {
44                  switch (*fname) {
45 <                        /* XXX This doesn't work on Windows */
46 <                        /* CASEDIRSEP: */                       /* relative to root */
52 <                        case '.':                               /* relative to cwd */
53 <                                strncpy(pname, fname, sizeof(pname)-1);
45 >                        case '.':       /* relative to cwd */
46 >                                strlcpy(pname, fname, sizeof(pname));
47                                  break;
48 <                        case '~':                               /* relative to home directory */
48 >                        case '~':       /* relative to home directory */
49                                  fname++;
50 <                                if (*fname == '\0' || ISDIRSEP(*fname)) {       /* ours */
51 <                                        if ((cp = getenv("HOME")) == NULL)
52 < #ifdef _WIN32  /* Windows sometimes uses a different var name */
60 <                                                if ((cp = getenv("HOMEDIR")) == NULL)
61 < #endif
62 <                                                        return(NULL);
63 <                                        strncpy(pname, cp, sizeof(pname)-1);
64 <                                        strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
65 <                                        break;
66 <                                }
67 < #ifndef RHAS_GETPWNAM
68 <                                /* XXX Should we request our own home directory from the
69 <                                   XXX system as well if the above fails? */
70 <                                /* XXX In any case, we need do the same thing on Windows... */
71 <                                cp = pname;                                     /* user */
72 <                                do
50 >                                cp = uname;
51 >                                for (i = 0; i < sizeof(uname) && *fname
52 >                                                && !ISDIRSEP(*fname); i++)
53                                          *cp++ = *fname++;
74                                while (*fname && !ISDIRSEP(*fname));
54                                  *cp = '\0';
55 <                                if ((pwent = getpwnam(pname)) == NULL)
56 <                                        return(NULL);
57 <                                strncpy(pname, pwent->pw_dir, sizeof(pname)-1);
79 <                                strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
55 >                                cp = gethomedir(uname, pname, sizeof(pname));
56 >                                if(cp == NULL) return NULL;
57 >                                strlcat(pname, fname, sizeof(pname));
58                                  break;
81 #endif
59                  }
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 <                strncpy(pname, fname, sizeof(pname)-1);
64 >        if (!searchpath) {              /* no search? */
65 >                strlcpy(pname, fname, sizeof(pname));
66                  return(pname);
67          }
68          /* check search path */
69          do {
70                  cp = pname;
71 <                while (*searchpath && (*cp = *searchpath++) != PATHSEP) {
72 <                        cp++;
73 <                }
97 <                if (cp > pname && !ISDIRSEP(cp[-1])) {
71 >                while (*searchpath && (*cp = *searchpath++) != PATHSEP)
72 >                        cp += (cp-pname < sizeof(pname)-2);
73 >                if (cp > pname && !ISDIRSEP(cp[-1]))
74                          *cp++ = DIRSEP;
75 <                }
76 <                strncpy(cp, fname, sizeof(pname)-strlen(pname)-1);
75 >                *cp = '\0';
76 >                strlcat(pname, fname, sizeof(pname));
77                  if (access(pname, mode) == 0)           /* file accessable? */
78                          return(pname);
79          } while (*searchpath);
# Line 106 | Line 82 | int  mode
82   }
83  
84  
85 < #ifdef _WIN32
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
# Line 117 | Line 93 | int  mode
93   */
94   char *
95   getpath(        /* expand fname, return full path */
96 < register char  *ffname,
97 < register char  *searchpath,
98 < int  mode
96 >        char  *ffname,
97 >        char  *searchpath,
98 >        int  mode
99   )
100   {
101 <        register char  *cp;
101 >        char  *cp;
102          char fname[PATH_MAX];
103  
104 <        if (ffname == NULL)
105 <                return(NULL);
104 >        if (!fname | (fname == pname))
105 >                return(fname);
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) || (strrchr(ffname, '.') != NULL)) {
110 >        if (!(mode & X_OK) || strchr(ffname, '.') > ffname) {
111                  return core_getpath(ffname, searchpath, mode);
112          }
113  
# Line 166 | Line 142 | int main()
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 HOMEDIR\n");
145 >        printf("Undefining HOME and HOMEPATH\n");
146          unsetenv("HOME");
147 <        unsetenv("HOMEDIR");
147 >        unsetenv("HOMEPATH");
148          fp = getpath("~", getenv("PATH"), F_OK);
149          printf(fmt,  "~",        "PATH", "F_OK", fp);
150 < #ifndef RHAS_GETPWNAM
151 <        fp = getpath("~lp", getenv("PATH"), F_OK);
176 <        printf(fmt, "~lp",         "PATH", "F_OK", fp);
177 < #endif
150 >        fp = getpath("~lp/blah", getenv("PATH"), F_OK);
151 >        printf(fmt, "~lp/blah",         "PATH", "F_OK", fp);
152   }
153   #endif
154  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines