1 |
greg |
2.5 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
greg |
1.1 |
|
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 |
greg |
2.7 |
#include "standard.h" |
12 |
|
|
|
13 |
greg |
2.6 |
#include "paths.h" |
14 |
greg |
2.5 |
|
15 |
greg |
2.7 |
#ifndef NIX |
16 |
greg |
1.1 |
#include <pwd.h> |
17 |
greg |
2.5 |
extern struct passwd *getpwnam(); |
18 |
greg |
2.4 |
#endif |
19 |
greg |
1.1 |
|
20 |
|
|
|
21 |
|
|
char * |
22 |
|
|
getpath(fname, searchpath, mode) /* expand fname, return full path */ |
23 |
|
|
register char *fname; |
24 |
|
|
register char *searchpath; |
25 |
|
|
int mode; |
26 |
|
|
{ |
27 |
greg |
2.7 |
#ifndef NIX |
28 |
greg |
2.4 |
struct passwd *pwent; |
29 |
|
|
#endif |
30 |
greg |
2.6 |
static char pname[MAXPATH]; |
31 |
greg |
1.1 |
register char *cp; |
32 |
|
|
|
33 |
|
|
if (fname == NULL) |
34 |
|
|
return(NULL); |
35 |
|
|
|
36 |
|
|
switch (*fname) { |
37 |
greg |
2.6 |
CASEDIRSEP: /* relative to root */ |
38 |
greg |
1.1 |
case '.': /* relative to cwd */ |
39 |
|
|
strcpy(pname, fname); |
40 |
|
|
return(pname); |
41 |
greg |
2.3 |
#ifndef NIX |
42 |
greg |
1.1 |
case '~': /* relative to home directory */ |
43 |
|
|
fname++; |
44 |
greg |
2.6 |
if (*fname == '\0' || ISDIRSEP(*fname)) { /* ours */ |
45 |
greg |
1.1 |
if ((cp = getenv("HOME")) == NULL) |
46 |
|
|
return(NULL); |
47 |
|
|
strcpy(pname, cp); |
48 |
|
|
strcat(pname, fname); |
49 |
|
|
return(pname); |
50 |
|
|
} |
51 |
|
|
cp = pname; /* user */ |
52 |
|
|
do |
53 |
|
|
*cp++ = *fname++; |
54 |
greg |
2.6 |
while (*fname && !ISDIRSEP(*fname)); |
55 |
greg |
1.1 |
*cp = '\0'; |
56 |
|
|
if ((pwent = getpwnam(pname)) == NULL) |
57 |
|
|
return(NULL); |
58 |
|
|
strcpy(pname, pwent->pw_dir); |
59 |
|
|
strcat(pname, fname); |
60 |
|
|
return(pname); |
61 |
greg |
2.3 |
#endif |
62 |
greg |
1.1 |
} |
63 |
|
|
|
64 |
|
|
if (searchpath == NULL) { /* don't search */ |
65 |
|
|
strcpy(pname, fname); |
66 |
|
|
return(pname); |
67 |
|
|
} |
68 |
|
|
/* check search path */ |
69 |
|
|
do { |
70 |
|
|
cp = pname; |
71 |
greg |
2.3 |
while (*searchpath && (*cp = *searchpath++) != PATHSEP) |
72 |
|
|
cp++; |
73 |
greg |
2.6 |
if (cp > pname && !ISDIRSEP(cp[-1])) |
74 |
greg |
2.3 |
*cp++ = DIRSEP; |
75 |
greg |
1.1 |
strcpy(cp, fname); |
76 |
|
|
if (access(pname, mode) == 0) /* file accessable? */ |
77 |
|
|
return(pname); |
78 |
|
|
} while (*searchpath); |
79 |
|
|
/* not found */ |
80 |
|
|
return(NULL); |
81 |
|
|
} |