| 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 |
greg |
2.4 |
#ifndef NIX |
| 14 |
greg |
1.1 |
#include <pwd.h> |
| 15 |
greg |
2.4 |
#endif |
| 16 |
greg |
1.1 |
|
| 17 |
greg |
2.3 |
#ifndef DIRSEP |
| 18 |
|
|
#define DIRSEP '/' |
| 19 |
|
|
#endif |
| 20 |
|
|
#ifndef PATHSEP |
| 21 |
|
|
#define PATHSEP ':' |
| 22 |
|
|
#endif |
| 23 |
|
|
|
| 24 |
greg |
1.1 |
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 |
greg |
2.4 |
#ifndef NIX |
| 35 |
|
|
struct passwd *pwent; |
| 36 |
|
|
#endif |
| 37 |
greg |
1.1 |
static char pname[256]; |
| 38 |
|
|
register char *cp; |
| 39 |
|
|
|
| 40 |
|
|
if (fname == NULL) |
| 41 |
|
|
return(NULL); |
| 42 |
|
|
|
| 43 |
|
|
switch (*fname) { |
| 44 |
greg |
2.3 |
case DIRSEP: /* relative to root */ |
| 45 |
greg |
1.1 |
case '.': /* relative to cwd */ |
| 46 |
|
|
strcpy(pname, fname); |
| 47 |
|
|
return(pname); |
| 48 |
greg |
2.3 |
#ifndef NIX |
| 49 |
greg |
1.1 |
case '~': /* relative to home directory */ |
| 50 |
|
|
fname++; |
| 51 |
greg |
2.3 |
if (*fname == '\0' || *fname == DIRSEP) { /* ours */ |
| 52 |
greg |
1.1 |
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 |
greg |
2.3 |
while (*fname && *fname != DIRSEP); |
| 62 |
greg |
1.1 |
*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 |
greg |
2.3 |
#endif |
| 69 |
greg |
1.1 |
} |
| 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 |
greg |
2.3 |
while (*searchpath && (*cp = *searchpath++) != PATHSEP) |
| 79 |
|
|
cp++; |
| 80 |
|
|
if (cp > pname && cp[-1] != DIRSEP) |
| 81 |
|
|
*cp++ = DIRSEP; |
| 82 |
greg |
1.1 |
strcpy(cp, fname); |
| 83 |
|
|
if (access(pname, mode) == 0) /* file accessable? */ |
| 84 |
|
|
return(pname); |
| 85 |
|
|
} while (*searchpath); |
| 86 |
|
|
/* not found */ |
| 87 |
|
|
return(NULL); |
| 88 |
|
|
} |