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