--- ray/src/common/getpath.c 2003/06/26 00:58:09 2.11 +++ ray/src/common/getpath.c 2019/12/28 18:05:14 2.21 @@ -1,26 +1,21 @@ #ifndef lint -static const char RCSid[] = "$Id: getpath.c,v 2.11 2003/06/26 00:58:09 schorsch Exp $"; +static const char RCSid[] = "$Id: getpath.c,v 2.21 2019/12/28 18:05:14 greg Exp $"; #endif /* * getpath.c - function to search for file in a list of directories * - * External symbols declared in standard.h + * External symbols declared in rtio.h */ #include "copyright.h" -#include #include -#ifndef _WIN32 /* XXX was NIX, do we still compile on Amiga? */ - #include - #include -#endif +#include "rtio.h" #include "paths.h" - -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) static char * core_getpath /* wrapped below: expand fname, return full path */ #else @@ -28,76 +23,56 @@ char * getpath /* expand fname, return full path */ #endif ( -register char *fname, -register char *searchpath, -int mode + char *fname, + char *searchpath, + int mode ) { -#ifndef _WIN32 /* XXX was NIX, do we still compile on Amiga? */ - struct passwd *pwent; -#endif static char pname[PATH_MAX]; - register char *cp; + char uname[512]; + char *cp; + int i; if (fname == NULL) { return(NULL); } pname[0] = '\0'; /* check for full specification */ - if (ISABS(fname)) { /* Can't use CASEDIRSEP below on Windows */ - strncpy(pname, fname, sizeof(pname)-1); + if (ISABS(fname)) { /* absolute path */ + strlcpy(pname, fname, sizeof(pname)); } else { switch (*fname) { - /* XXX This doesn't work on Windows */ - /* CASEDIRSEP: */ /* relative to root */ - case '.': /* relative to cwd */ - strncpy(pname, fname, sizeof(pname)-1); + case '.': /* relative to cwd */ + strlcpy(pname, fname, sizeof(pname)); break; - case '~': /* relative to home directory */ + case '~': /* relative to home directory */ fname++; - if (*fname == '\0' || ISDIRSEP(*fname)) { /* ours */ - if ((cp = getenv("HOME")) == NULL) -#ifdef _WIN32 /* Windows sometimes uses a different var name */ - if ((cp = getenv("HOMEDIR")) == NULL) -#endif - return(NULL); - strncpy(pname, cp, sizeof(pname)-1); - strncat(pname, fname, sizeof(pname)-strlen(pname)-1); - break; - } -#ifndef _WIN32 /* XXX was NIX, do we still compile on Amiga? */ - /* XXX Should we request our own home directory from the - XXX system as well if the above fails? */ - /* XXX In any case, we need do the same thing on Windows... */ - cp = pname; /* user */ - do + cp = uname; + for (i = 0; i < sizeof(uname) && *fname + && !ISDIRSEP(*fname); i++) *cp++ = *fname++; - while (*fname && !ISDIRSEP(*fname)); *cp = '\0'; - if ((pwent = getpwnam(pname)) == NULL) - return(NULL); - strncpy(pname, pwent->pw_dir, sizeof(pname)-1); - strncat(pname, fname, sizeof(pname)-strlen(pname)-1); + cp = gethomedir(uname, pname, sizeof(pname)); + if(cp == NULL) return NULL; + strlcat(pname, fname, sizeof(pname)); break; -#endif } } if (pname[0]) /* got it, check access if search requested */ - return(searchpath==NULL||access(pname,mode)==0 ? pname : NULL); + return(!searchpath || access(pname,mode)==0 ? pname : NULL); - if (searchpath == NULL) { /* don't search */ - strncpy(pname, fname, sizeof(pname)-1); + if (!searchpath) { /* no search */ + strlcpy(pname, fname, sizeof(pname)); return(pname); } /* check search path */ do { cp = pname; - while (*searchpath && (*cp = *searchpath++) != PATHSEP) { - cp++; - } - if (cp > pname && !ISDIRSEP(cp[-1])) { + while (*searchpath && (*cp = *searchpath++) != PATHSEP) + cp += (cp-pname < sizeof(pname)-2); + if (cp > pname && !ISDIRSEP(cp[-1])) *cp++ = DIRSEP; - } - strncpy(cp, fname, sizeof(pname)-strlen(pname)-1); + *cp = '\0'; + strlcat(pname, fname, sizeof(pname)); if (access(pname, mode) == 0) /* file accessable? */ return(pname); } while (*searchpath); @@ -106,7 +81,7 @@ int mode } -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) /* This is a wrapper around the above, "emulating" access mode X_OK, which is not supported on Windows. If we see X_OK and the filename has no extension, then we'll remove @@ -117,12 +92,12 @@ int mode */ char * getpath( /* expand fname, return full path */ -register char *ffname, -register char *searchpath, -int mode + char *ffname, + char *searchpath, + int mode ) { - register char *cp; + char *cp; char fname[PATH_MAX]; if (ffname == NULL) @@ -166,15 +141,13 @@ int main() printf(fmt, "/", "PATH", "W_OK", fp); fp = getpath("~", getenv("PATH"), F_OK); printf(fmt, "~", "PATH", "F_OK", fp); - printf("Undefining HOME and HOMEDIR\n"); + printf("Undefining HOME and HOMEPATH\n"); unsetenv("HOME"); - unsetenv("HOMEDIR"); + unsetenv("HOMEPATH"); fp = getpath("~", getenv("PATH"), F_OK); printf(fmt, "~", "PATH", "F_OK", fp); -#ifndef _WIN32 /* XXX was NIX, do we still compile on Amiga? */ - fp = getpath("~lp", getenv("PATH"), F_OK); - printf(fmt, "~lp", "PATH", "F_OK", fp); -#endif + fp = getpath("~lp/blah", getenv("PATH"), F_OK); + printf(fmt, "~lp/blah", "PATH", "F_OK", fp); } #endif