ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/getpath.c
Revision: 2.2
Committed: Wed Mar 25 10:57:29 1992 UTC (32 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +5 -1 lines
Log Message:
added escape character for paths including colon

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * getpath.c - function to search for file in a list of directories
9     */
10    
11     #define NULL 0
12    
13     #include <pwd.h>
14    
15     extern char *strcpy(), *strcat(), *getenv();
16     extern struct passwd *getpwnam();
17    
18    
19     char *
20     getpath(fname, searchpath, mode) /* expand fname, return full path */
21     register char *fname;
22     register char *searchpath;
23     int mode;
24     {
25     static char pname[256];
26     struct passwd *pwent;
27     register char *cp;
28    
29     if (fname == NULL)
30     return(NULL);
31    
32     switch (*fname) {
33     case '/': /* relative to root */
34     case '.': /* relative to cwd */
35     strcpy(pname, fname);
36     return(pname);
37     case '~': /* relative to home directory */
38     fname++;
39     if (*fname == '\0' || *fname == '/') { /* ours */
40     if ((cp = getenv("HOME")) == NULL)
41     return(NULL);
42     strcpy(pname, cp);
43     strcat(pname, fname);
44     return(pname);
45     }
46     cp = pname; /* user */
47     do
48     *cp++ = *fname++;
49     while (*fname && *fname != '/');
50     *cp = '\0';
51     if ((pwent = getpwnam(pname)) == NULL)
52     return(NULL);
53     strcpy(pname, pwent->pw_dir);
54     strcat(pname, fname);
55     return(pname);
56     }
57    
58     if (searchpath == NULL) { /* don't search */
59     strcpy(pname, fname);
60     return(pname);
61     }
62     /* check search path */
63     do {
64     cp = pname;
65     while (*searchpath && (*cp = *searchpath++) != ':')
66 greg 2.2 if (*cp == '\\') { /* escape */
67     if (*searchpath)
68     *cp++ = *searchpath++;
69     } else
70     cp++;
71 greg 1.1 if (cp > pname && cp[-1] != '/')
72     *cp++ = '/';
73     strcpy(cp, fname);
74     if (access(pname, mode) == 0) /* file accessable? */
75     return(pname);
76     } while (*searchpath);
77     /* not found */
78     return(NULL);
79     }