static char *
dln_find_1(fname, path, exe_flag)
const char *fname;
const char *path;
int exe_flag; /* non 0 if looking for executable. */
{
register const char *dp;
register const char *ep;
register char *bp;
struct stat st;
#ifdef __MACOS__
const char* mac_fullpath;
#endif
if (!fname) return (char *)fname;
if (fname[0] == '/') return (char *)fname;
if (strncmp("./", fname, 2) == 0 || strncmp("../", fname, 3) == 0)
return (char *)fname;
if (exe_flag && strchr(fname, '/')) return (char *)fname;
#ifdef DOSISH
if (fname[0] == '\\') return (char *)fname;
# ifdef DOSISH_DRIVE_LETTER
if (strlen(fname) > 2 && fname[1] == ':') return (char *)fname;
# endif
if (strncmp(".\\", fname, 2) == 0 || strncmp("..\\", fname, 3) == 0)
return (char *)fname;
if (exe_flag && strchr(fname, '\\')) return (char *)fname;
#endif
for (dp = path;; dp = ++ep) {
register int l;
int i;
int fspace;
/* extract a component */
ep = strchr(dp, PATH_SEP[0]);
if (ep == NULL)
ep = dp+strlen(dp);
/* find the length of that component */
l = ep - dp;
bp = fbuf;
fspace = sizeof fbuf - 2;
if (l > 0) {
/*
** If the length of the component is zero length,
** start from the current directory. If the
** component begins with "~", start from the
** user's $HOME environment variable. Otherwise
** take the path literally.
*/
if (*dp == '~' && (l == 1 ||
#if defined(DOSISH)
dp[1] == '\\' ||
#endif
dp[1] == '/')) {
char *home;
home = getenv("HOME");
if (home != NULL) {
i = strlen(home);
if ((fspace -= i) < 0)
goto toolong;
memcpy(bp, home, i);
bp += i;
}
dp++;
l--;
}
if (l > 0) {
if ((fspace -= l) < 0)
goto toolong;
memcpy(bp, dp, l);
bp += l;
}
/* add a "/" between directory and filename */
if (ep[-1] != '/')
*bp++ = '/';
}
/* now append the file name */
i = strlen(fname);
if ((fspace -= i) < 0) {
toolong:
fprintf(stderr, "openpath: pathname too long (ignored)\n");
*bp = '\0';
fprintf(stderr, "\tDirectory \"%s\"\n", fbuf);
fprintf(stderr, "\tFile \"%s\"\n", fname);
goto next;
}
memcpy(bp, fname, i + 1);
#if defined(DOSISH)
if (exe_flag) {
static const char extension[][5] = {
#if defined(MSDOS)
".com", ".exe", ".bat",
#if defined(DJGPP)
".btm", ".sh", ".ksh", ".pl", ".sed",
#endif
#elif defined(__EMX__) || defined(_WIN32)
".exe", ".com", ".cmd", ".bat",
/* end of __EMX__ or _WIN32 */
#else
".r", ".R", ".x", ".X", ".bat", ".BAT",
/* __human68k__ */
#endif
};
int j;
for (j = 0; j < sizeof(extension) / sizeof(extension[0]); j++) {
if (fspace < strlen(extension[j])) {
fprintf(stderr, "openpath: pathname too long (ignored)\n");
fprintf(stderr, "\tDirectory \"%.*s\"\n", (int) (bp - fbuf), fbuf);
fprintf(stderr, "\tFile \"%s%s\"\n", fname, extension[j]);
continue;
}
strcpy(bp + i, extension[j]);
#ifndef __MACOS__
if (stat(fbuf, &st) == 0)
return fbuf;
#else
if (mac_fullpath = _macruby_exist_file_in_libdir_as_posix_name(fbuf))
return mac_fullpath;
#endif
}
goto next;
}
#endif /* MSDOS or _WIN32 or __human68k__ or __EMX__ */
#ifndef __MACOS__
if (stat(fbuf, &st) == 0) {
if (exe_flag == 0) return fbuf;
/* looking for executable */
if (!S_ISDIR(st.st_mode) && eaccess(fbuf, X_OK) == 0)
return fbuf;
}
#else
if (mac_fullpath = _macruby_exist_file_in_libdir_as_posix_name(fbuf)) {
if (exe_flag == 0) return mac_fullpath;
/* looking for executable */
if (stat(mac_fullpath, &st) == 0) {
if (!S_ISDIR(st.st_mode) && eaccess(mac_fullpath, X_OK) == 0)
return mac_fullpath;
}
}
#endif
next:
/* if not, and no other alternatives, life is bleak */
if (*ep == '\0') {
return NULL;
}
/* otherwise try the next component in the search path */
}
}