ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/getpath.c
Revision: 2.10
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.9: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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