fix absolute_file_name

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1937 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2007-09-27 22:40:22 +00:00
parent 31ff28d3ee
commit c1917517cb
3 changed files with 74 additions and 30 deletions

View File

@ -2143,24 +2143,38 @@ check_bom(int sno, StreamDesc *st)
}
}
static Int
p_access(char *file_name)
{
#if HAVE_STAT
#if _MSC_VER || defined(__MINGW32__)
struct _stat ss;
if (_stat(file_name, &ss) != 0) {
#define SYSTEM_STAT _stat
#else
struct stat ss;
if (stat(file_name, &ss) != 0) {
#define SYSTEM_STAT stat
#endif
/* ignore errors while checking a file */
static Int
p_access(void)
{
Term tname = Deref(ARG1);
char *file_name;
if (IsVarTerm(tname)) {
Yap_Error(INSTANTIATION_ERROR, tname, "access");
return FALSE;
}
return TRUE;
} else if (!IsAtomTerm (tname)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "access");
return FALSE;
} else {
#if HAVE_STAT
struct SYSTEM_STAT ss;
file_name = RepAtom(AtomOfTerm(tname))->StrOfAE;
if (SYSTEM_STAT(file_name, &ss) != 0) {
/* ignore errors while checking a file */
return FALSE;
}
return TRUE;
#else
return FALSE;
return FALSE;
#endif
}
}
static Int

View File

@ -1589,7 +1589,7 @@ Yap_volume_header(char *file)
}
static int
TrueFileName (char *source, char *result, int in_lib)
TrueFileName (char *source, char *root, char *result, int in_lib)
{
register int ch;
register char *res0 = result, *work;
@ -1686,9 +1686,16 @@ TrueFileName (char *source, char *result, int in_lib)
}
*res0 = '\0';
/* step 3: get the full file name */
if (!dir_separator(result[0]) && !volume_header(result))
{
if (!dir_separator(result[0]) && !volume_header(result)) {
if (root) {
strncpy(ares1, root, YAP_FILENAME_MAX);
#if _MSC_VER || defined(__MINGW32__)
strncat (ares1, "\\", YAP_FILENAME_MAX);
#else
strncat (ares1, "/", YAP_FILENAME_MAX);
#endif
strncat (ares1, result, YAP_FILENAME_MAX);
} else {
#if __simplescalar__
/* does not implement getcwd */
strncpy(ares1,yap_pwd,YAP_FILENAME_MAX);
@ -1708,7 +1715,7 @@ TrueFileName (char *source, char *result, int in_lib)
if (in_lib) {
int tmpf;
if ((tmpf = open(ares1, O_RDONLY)) < 0) {
/* not in current directory, let us try the library */
/* not in current directory, let us try the library */
if (Yap_LibDir != NULL) {
strncpy(Yap_FileNameBuf, Yap_LibDir, YAP_FILENAME_MAX);
#if HAVE_GETENV
@ -1746,6 +1753,7 @@ TrueFileName (char *source, char *result, int in_lib)
strncpy (result, ares1, YAP_FILENAME_MAX);
}
}
}
/* step 4: simplifying the file name */
work = result;
while (*work != '\0')
@ -1792,7 +1800,7 @@ TrueFileName (char *source, char *result, int in_lib)
int
Yap_TrueFileName (char *source, char *result, int in_lib)
{
return TrueFileName (source, result, in_lib);
return TrueFileName (source, NULL, result, in_lib);
}
static Int
@ -1808,10 +1816,35 @@ p_true_file_name (void)
Yap_Error(TYPE_ERROR_ATOM,t,"argument to true_file_name");
return FALSE;
}
TrueFileName (RepAtom(AtomOfTerm(t))->StrOfAE, Yap_FileNameBuf, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t))->StrOfAE, NULL, Yap_FileNameBuf, FALSE);
return Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(Yap_FileNameBuf)));
}
static Int
p_true_file_name3 (void)
{
Term t = Deref(ARG1), t2 = Deref(ARG2);
char *root = NULL;
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,t,"argument to true_file_name unbound");
return FALSE;
}
if (!IsAtomTerm(t)) {
Yap_Error(TYPE_ERROR_ATOM,t,"argument to true_file_name");
return FALSE;
}
if (!IsVarTerm(t2)) {
if (!IsAtomTerm(t)) {
Yap_Error(TYPE_ERROR_ATOM,t2,"argument to true_file_name");
return FALSE;
}
root = RepAtom(AtomOfTerm(t2))->StrOfAE;
}
TrueFileName (RepAtom(AtomOfTerm(t))->StrOfAE, root, Yap_FileNameBuf, FALSE);
return Yap_unify(ARG3, MkAtomTerm(Yap_LookupAtom(Yap_FileNameBuf)));
}
static Int
p_getcwd(void)
@ -2001,8 +2034,8 @@ p_mv (void)
} else if (!IsAtomTerm(t2)) {
Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom");
}
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, oldname, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t2))->StrOfAE, newname, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, NULL, oldname, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t2))->StrOfAE, NULL, newname, FALSE);
if ((r = link (oldname, newname)) == 0 && (r = unlink (oldname)) != 0)
unlink (newname);
if (r != 0) {
@ -2036,7 +2069,7 @@ p_file_directory_name (void)
Yap_Error(TYPE_ERROR_ATOM, t1, "first arg of file_directory_name/2");
return FALSE;
}
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, Yap_FileNameBuf, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, NULL, Yap_FileNameBuf, FALSE);
chp = Yap_FileNameBuf+strlen(Yap_FileNameBuf);
while (!dir_separator(*--chp) && chp != Yap_FileNameBuf);
if (chp == Yap_FileNameBuf) {
@ -2056,7 +2089,7 @@ p_cd (void)
Yap_Error(INSTANTIATION_ERROR,t1,"argument to cd/1 is not valid");
return FALSE;
} else if (IsAtomTerm(t1)) {
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, Yap_FileNameBuf2, FALSE);
TrueFileName (RepAtom(AtomOfTerm(t1))->StrOfAE, NULL, Yap_FileNameBuf2, FALSE);
} else {
if (t1 == TermNil)
return TRUE;
@ -2064,7 +2097,7 @@ p_cd (void)
Yap_Error(TYPE_ERROR_ATOM,t1,"argument to cd/1 is not valid");
return FALSE;
}
TrueFileName (Yap_FileNameBuf, Yap_FileNameBuf2, FALSE);
TrueFileName (Yap_FileNameBuf, NULL, Yap_FileNameBuf2, FALSE);
}
#if HAVE_CHDIR
#if __simplescalar__
@ -2665,6 +2698,7 @@ Yap_InitSysPreds(void)
Yap_InitCPred ("$ld_path", 1, p_ld_path, SafePredFlag);
CurrentModule = SYSTEM_MODULE;
Yap_InitCPred ("true_file_name", 2, p_true_file_name, SyncPredFlag);
Yap_InitCPred ("true_file_name", 3, p_true_file_name3, SyncPredFlag);
CurrentModule = cm;
}

View File

@ -577,8 +577,7 @@ absolute_file_name(File,Opts,TrueFileName) :-
'$process_fn_opts'(V,_,_,_,_,_,_,_,_,G) :- var(V), !,
'$do_error'(instantiation_error, G).
'$process_fn_opts'([],[],CWD,source,read,error,first,false,false,_) :- !,
getcwd(CWD).
'$process_fn_opts'([],[],_,source,read,error,first,false,false,_) :- !.
'$process_fn_opts'([Opt|Opts],Extensions,RelTo,Type,Access,FErrors,Solutions,Expand,Debug,G) :- !,
'$process_fn_opt'(Opt,Extensions,RelTo,Type,Access,FErrors,Solutions,Expand,Debug,Extensions0,RelTo0,Type0,Access0,FErrors0,Solutions0,Expand0,Debug0,G),
'$process_fn_opts'(Opts,Extensions0,RelTo0,Type0,Access0,FErrors0,Solutions0,Expand0,Debug0,G).
@ -686,10 +685,7 @@ absolute_file_name(File,Opts,TrueFileName) :-
'$do_error'(domain_error(source_sink,File),Call).
'$get_abs_file'(File,opts(_,D0,_,_,_,_,_),AbsFile) :-
'$dir_separator'(D),
atom_codes(A,[D]),
atom_concat([D0,A,File],File1),
system:true_file_name(File1,AbsFile).
system:true_file_name(File,D0,AbsFile).
'$search_in_path'(File,opts(Extensions,_,_,Access,_,_,_),F) :-
'$add_extensions'(Extensions,File,F),