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