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