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