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.11 by schorsch, Thu Jun 26 00:58:09 2003 UTC vs.
Revision 2.21 by greg, Sat Dec 28 18:05:14 2019 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  _WIN32 /* XXX was NIX, do we still compile on Amiga? */
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  _WIN32 /* XXX was NIX, do we still compile on Amiga? */
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); }
37  
38          pname[0] = '\0';                /* check for full specification */
39  
40 <        if (ISABS(fname)) { /* Can't use CASEDIRSEP below on Windows */
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 <                        /* XXX This doesn't work on Windows */
45 <                        /* CASEDIRSEP: */                       /* relative to root */
52 <                        case '.':                               /* relative to cwd */
53 <                                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 <                                if (*fname == '\0' || ISDIRSEP(*fname)) {       /* ours */
50 <                                        if ((cp = getenv("HOME")) == NULL)
51 < #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 _WIN32 /* XXX was NIX, do we still compile on Amiga? */
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
49 >                                cp = uname;
50 >                                for (i = 0; i < sizeof(uname) && *fname
51 >                                                && !ISDIRSEP(*fname); i++)
52                                          *cp++ = *fname++;
74                                while (*fname && !ISDIRSEP(*fname));
53                                  *cp = '\0';
54 <                                if ((pwent = getpwnam(pname)) == NULL)
55 <                                        return(NULL);
56 <                                strncpy(pname, pwent->pw_dir, sizeof(pname)-1);
79 <                                strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
54 >                                cp = gethomedir(uname, pname, sizeof(pname));
55 >                                if(cp == NULL) return NULL;
56 >                                strlcat(pname, fname, sizeof(pname));
57                                  break;
81 #endif
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 <                }
97 <                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 106 | 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 117 | 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)
# Line 166 | Line 141 | int main()
141          printf(fmt,  "/",        "PATH", "W_OK", fp);
142          fp = getpath("~", getenv("PATH"), F_OK);
143          printf(fmt,  "~",        "PATH", "F_OK", fp);
144 <        printf("Undefining HOME and HOMEDIR\n");
144 >        printf("Undefining HOME and HOMEPATH\n");
145          unsetenv("HOME");
146 <        unsetenv("HOMEDIR");
146 >        unsetenv("HOMEPATH");
147          fp = getpath("~", getenv("PATH"), F_OK);
148          printf(fmt,  "~",        "PATH", "F_OK", fp);
149 < #ifndef _WIN32 /* XXX was NIX, do we still compile on Amiga? */
150 <        fp = getpath("~lp", getenv("PATH"), F_OK);
176 <        printf(fmt, "~lp",         "PATH", "F_OK", fp);
177 < #endif
149 >        fp = getpath("~lp/blah", getenv("PATH"), F_OK);
150 >        printf(fmt, "~lp/blah",         "PATH", "F_OK", fp);
151   }
152   #endif
153  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines