implement access_file/2 in C

This commit is contained in:
Vítor Santos Costa 2010-08-03 21:04:16 +01:00
parent 69b21df917
commit c081d01e78
6 changed files with 85 additions and 23 deletions

View File

@ -2332,6 +2332,75 @@ p_access(void)
}
}
static Int
p_access2(void)
{
Term tname = Deref(ARG1);
Term tmode = Deref(ARG2);
char ares[YAP_FILENAME_MAX];
Atom atmode;
if (IsVarTerm(tmode)) {
Yap_Error(INSTANTIATION_ERROR, tmode, "access");
return FALSE;
} else if (!IsAtomTerm (tmode)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "access");
return FALSE;
}
atmode = AtomOfTerm(tmode);
if (IsVarTerm(tname)) {
Yap_Error(INSTANTIATION_ERROR, tname, "access");
return FALSE;
} else if (!IsAtomTerm (tname)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "access");
return FALSE;
} else {
if (atmode == AtomNone)
return TRUE;
if (!Yap_TrueFileName (RepAtom(AtomOfTerm(tname))->StrOfAE, ares, (atmode == AtomCsult)))
return FALSE;
}
#if HAVE_ACCESS
{
int mode;
if (atmode == AtomExist)
mode = F_OK;
else if (atmode == AtomWrite)
mode = W_OK;
else if (atmode == AtomRead)
mode = R_OK;
else if (atmode == AtomAppend)
mode = W_OK;
else if (atmode == AtomCsult)
mode = R_OK;
else if (atmode == AtomExecute)
mode = X_OK;
else {
Yap_Error(DOMAIN_ERROR_IO_MODE, tmode, "access_file/2");
return FALSE;
}
if (access(ares, mode) != 0) {
/* ignore errors while checking a file */
return FALSE;
}
return TRUE;
}
#elif HAVE_STAT
{
struct SYSTEM_STAT ss;
if (SYSTEM_STAT(ares, &ss) != 0) {
/* ignore errors while checking a file */
return FALSE;
}
return TRUE;
}
#else
return FALSE;
#endif
}
static Int
p_exists_directory(void)
{
@ -6455,6 +6524,7 @@ Yap_InitIOPreds(void)
Yap_InitCPred ("get0", 2, p_get0, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$get0_line_codes", 2, p_get0_line_codes, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("$get_byte", 2, p_get_byte, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("access_file", 2, p_access2, SafePredFlag|HiddenPredFlag);
Yap_InitCPred ("$access", 1, p_access, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred ("exists_directory", 1, p_exists_directory, SafePredFlag|SyncPredFlag);
Yap_InitCPred ("$open", 6, p_open, SafePredFlag|SyncPredFlag|HiddenPredFlag);

View File

@ -90,10 +90,12 @@
AtomError = Yap_LookupAtom("error");
AtomEvaluable = Yap_LookupAtom("evaluable");
AtomEvaluationError = Yap_LookupAtom("evaluation_error");
AtomExecute = Yap_LookupAtom("execute");
AtomExecAnswers = Yap_LookupAtom("exec_answers");
AtomExecuteInMod = Yap_FullLookupAtom("$execute_in_mod");
AtomExecuteWithin = Yap_FullLookupAtom("$execute_within");
AtomExecuteWoMod = Yap_FullLookupAtom("$execute_wo_mod");
AtomExist = Yap_LookupAtom("exist");
AtomExistenceError = Yap_LookupAtom("existence_error");
AtomExpectedNumber = Yap_LookupAtom("expected_number_syntax");
AtomExtendFileSearchPath = Yap_FullLookupAtom("$extend_file_search_path");
@ -172,6 +174,7 @@
AtomNbTerm = Yap_LookupAtom("nb_term");
AtomNew = Yap_LookupAtom("new");
AtomNoMemory = Yap_LookupAtom("no_memory");
AtomNone = Yap_LookupAtom("none");
AtomNonEmptyList = Yap_LookupAtom("non_empty_list");
AtomNot = Yap_LookupAtom("\\+");
AtomNotImplemented = Yap_LookupAtom("not_implemented");

View File

@ -90,10 +90,12 @@
AtomError = AtomAdjust(AtomError);
AtomEvaluable = AtomAdjust(AtomEvaluable);
AtomEvaluationError = AtomAdjust(AtomEvaluationError);
AtomExecute = AtomAdjust(AtomExecute);
AtomExecAnswers = AtomAdjust(AtomExecAnswers);
AtomExecuteInMod = AtomAdjust(AtomExecuteInMod);
AtomExecuteWithin = AtomAdjust(AtomExecuteWithin);
AtomExecuteWoMod = AtomAdjust(AtomExecuteWoMod);
AtomExist = AtomAdjust(AtomExist);
AtomExistenceError = AtomAdjust(AtomExistenceError);
AtomExpectedNumber = AtomAdjust(AtomExpectedNumber);
AtomExtendFileSearchPath = AtomAdjust(AtomExtendFileSearchPath);
@ -172,6 +174,7 @@
AtomNbTerm = AtomAdjust(AtomNbTerm);
AtomNew = AtomAdjust(AtomNew);
AtomNoMemory = AtomAdjust(AtomNoMemory);
AtomNone = AtomAdjust(AtomNone);
AtomNonEmptyList = AtomAdjust(AtomNonEmptyList);
AtomNot = AtomAdjust(AtomNot);
AtomNotImplemented = AtomAdjust(AtomNotImplemented);

View File

@ -178,6 +178,8 @@
#define AtomEvaluable Yap_heap_regs->AtomEvaluable_
Atom AtomEvaluationError_;
#define AtomEvaluationError Yap_heap_regs->AtomEvaluationError_
Atom AtomExecute_;
#define AtomExecute Yap_heap_regs->AtomExecute_
Atom AtomExecAnswers_;
#define AtomExecAnswers Yap_heap_regs->AtomExecAnswers_
Atom AtomExecuteInMod_;
@ -186,6 +188,8 @@
#define AtomExecuteWithin Yap_heap_regs->AtomExecuteWithin_
Atom AtomExecuteWoMod_;
#define AtomExecuteWoMod Yap_heap_regs->AtomExecuteWoMod_
Atom AtomExist_;
#define AtomExist Yap_heap_regs->AtomExist_
Atom AtomExistenceError_;
#define AtomExistenceError Yap_heap_regs->AtomExistenceError_
Atom AtomExpectedNumber_;
@ -342,6 +346,8 @@
#define AtomNew Yap_heap_regs->AtomNew_
Atom AtomNoMemory_;
#define AtomNoMemory Yap_heap_regs->AtomNoMemory_
Atom AtomNone_;
#define AtomNone Yap_heap_regs->AtomNone_
Atom AtomNonEmptyList_;
#define AtomNonEmptyList Yap_heap_regs->AtomNonEmptyList_
Atom AtomNot_;

View File

@ -95,10 +95,12 @@ A Eq N "="
A Error N "error"
A Evaluable N "evaluable"
A EvaluationError N "evaluation_error"
A Execute N "execute"
A ExecAnswers N "exec_answers"
A ExecuteInMod F "$execute_in_mod"
A ExecuteWithin F "$execute_within"
A ExecuteWoMod F "$execute_wo_mod"
A Exist N "exist"
A ExistenceError N "existence_error"
A ExpectedNumber N "expected_number_syntax"
A ExtendFileSearchPath F "$extend_file_search_path"
@ -177,6 +179,7 @@ A Nb N "nb"
A NbTerm N "nb_term"
A New N "new"
A NoMemory N "no_memory"
A None N "none"
A NonEmptyList N "non_empty_list"
A Not N "\\+"
A NotImplemented N "not_implemented"

View File

@ -1120,29 +1120,6 @@ bootstrap(F) :-
'$do_error'(type_error(callable,H),P).
'$check_head'(_,_).
% Path predicates
access_file(F,Mode) :-
'$exists'(F,Mode).
'$exists'(_,none) :- !.
'$exists'(F,exist) :- !,
'$access'(F).
'$exists'(F,Mode) :-
get_value(fileerrors,V),
set_value(fileerrors,0),
operating_system_support:true_file_name(F, F1),
(
'$open'(F1, Mode, S, 0, 1, F)
->
'$close'(S),
set_value(fileerrors,V)
;
set_value(fileerrors,V),
fail
).
% term expansion
%
% return two arguments: Expanded0 is the term after "USER" expansion.