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