implement access_file/2 in C
This commit is contained in:
parent
69b21df917
commit
c081d01e78
70
C/iopreds.c
70
C/iopreds.c
@ -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
|
static Int
|
||||||
p_exists_directory(void)
|
p_exists_directory(void)
|
||||||
{
|
{
|
||||||
@ -6455,6 +6524,7 @@ Yap_InitIOPreds(void)
|
|||||||
Yap_InitCPred ("get0", 2, p_get0, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
Yap_InitCPred ("get0", 2, p_get0, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
||||||
Yap_InitCPred ("$get0_line_codes", 2, p_get0_line_codes, 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 ("$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 ("$access", 1, p_access, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
||||||
Yap_InitCPred ("exists_directory", 1, p_exists_directory, SafePredFlag|SyncPredFlag);
|
Yap_InitCPred ("exists_directory", 1, p_exists_directory, SafePredFlag|SyncPredFlag);
|
||||||
Yap_InitCPred ("$open", 6, p_open, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
Yap_InitCPred ("$open", 6, p_open, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
||||||
|
@ -90,10 +90,12 @@
|
|||||||
AtomError = Yap_LookupAtom("error");
|
AtomError = Yap_LookupAtom("error");
|
||||||
AtomEvaluable = Yap_LookupAtom("evaluable");
|
AtomEvaluable = Yap_LookupAtom("evaluable");
|
||||||
AtomEvaluationError = Yap_LookupAtom("evaluation_error");
|
AtomEvaluationError = Yap_LookupAtom("evaluation_error");
|
||||||
|
AtomExecute = Yap_LookupAtom("execute");
|
||||||
AtomExecAnswers = Yap_LookupAtom("exec_answers");
|
AtomExecAnswers = Yap_LookupAtom("exec_answers");
|
||||||
AtomExecuteInMod = Yap_FullLookupAtom("$execute_in_mod");
|
AtomExecuteInMod = Yap_FullLookupAtom("$execute_in_mod");
|
||||||
AtomExecuteWithin = Yap_FullLookupAtom("$execute_within");
|
AtomExecuteWithin = Yap_FullLookupAtom("$execute_within");
|
||||||
AtomExecuteWoMod = Yap_FullLookupAtom("$execute_wo_mod");
|
AtomExecuteWoMod = Yap_FullLookupAtom("$execute_wo_mod");
|
||||||
|
AtomExist = Yap_LookupAtom("exist");
|
||||||
AtomExistenceError = Yap_LookupAtom("existence_error");
|
AtomExistenceError = Yap_LookupAtom("existence_error");
|
||||||
AtomExpectedNumber = Yap_LookupAtom("expected_number_syntax");
|
AtomExpectedNumber = Yap_LookupAtom("expected_number_syntax");
|
||||||
AtomExtendFileSearchPath = Yap_FullLookupAtom("$extend_file_search_path");
|
AtomExtendFileSearchPath = Yap_FullLookupAtom("$extend_file_search_path");
|
||||||
@ -172,6 +174,7 @@
|
|||||||
AtomNbTerm = Yap_LookupAtom("nb_term");
|
AtomNbTerm = Yap_LookupAtom("nb_term");
|
||||||
AtomNew = Yap_LookupAtom("new");
|
AtomNew = Yap_LookupAtom("new");
|
||||||
AtomNoMemory = Yap_LookupAtom("no_memory");
|
AtomNoMemory = Yap_LookupAtom("no_memory");
|
||||||
|
AtomNone = Yap_LookupAtom("none");
|
||||||
AtomNonEmptyList = Yap_LookupAtom("non_empty_list");
|
AtomNonEmptyList = Yap_LookupAtom("non_empty_list");
|
||||||
AtomNot = Yap_LookupAtom("\\+");
|
AtomNot = Yap_LookupAtom("\\+");
|
||||||
AtomNotImplemented = Yap_LookupAtom("not_implemented");
|
AtomNotImplemented = Yap_LookupAtom("not_implemented");
|
||||||
|
@ -90,10 +90,12 @@
|
|||||||
AtomError = AtomAdjust(AtomError);
|
AtomError = AtomAdjust(AtomError);
|
||||||
AtomEvaluable = AtomAdjust(AtomEvaluable);
|
AtomEvaluable = AtomAdjust(AtomEvaluable);
|
||||||
AtomEvaluationError = AtomAdjust(AtomEvaluationError);
|
AtomEvaluationError = AtomAdjust(AtomEvaluationError);
|
||||||
|
AtomExecute = AtomAdjust(AtomExecute);
|
||||||
AtomExecAnswers = AtomAdjust(AtomExecAnswers);
|
AtomExecAnswers = AtomAdjust(AtomExecAnswers);
|
||||||
AtomExecuteInMod = AtomAdjust(AtomExecuteInMod);
|
AtomExecuteInMod = AtomAdjust(AtomExecuteInMod);
|
||||||
AtomExecuteWithin = AtomAdjust(AtomExecuteWithin);
|
AtomExecuteWithin = AtomAdjust(AtomExecuteWithin);
|
||||||
AtomExecuteWoMod = AtomAdjust(AtomExecuteWoMod);
|
AtomExecuteWoMod = AtomAdjust(AtomExecuteWoMod);
|
||||||
|
AtomExist = AtomAdjust(AtomExist);
|
||||||
AtomExistenceError = AtomAdjust(AtomExistenceError);
|
AtomExistenceError = AtomAdjust(AtomExistenceError);
|
||||||
AtomExpectedNumber = AtomAdjust(AtomExpectedNumber);
|
AtomExpectedNumber = AtomAdjust(AtomExpectedNumber);
|
||||||
AtomExtendFileSearchPath = AtomAdjust(AtomExtendFileSearchPath);
|
AtomExtendFileSearchPath = AtomAdjust(AtomExtendFileSearchPath);
|
||||||
@ -172,6 +174,7 @@
|
|||||||
AtomNbTerm = AtomAdjust(AtomNbTerm);
|
AtomNbTerm = AtomAdjust(AtomNbTerm);
|
||||||
AtomNew = AtomAdjust(AtomNew);
|
AtomNew = AtomAdjust(AtomNew);
|
||||||
AtomNoMemory = AtomAdjust(AtomNoMemory);
|
AtomNoMemory = AtomAdjust(AtomNoMemory);
|
||||||
|
AtomNone = AtomAdjust(AtomNone);
|
||||||
AtomNonEmptyList = AtomAdjust(AtomNonEmptyList);
|
AtomNonEmptyList = AtomAdjust(AtomNonEmptyList);
|
||||||
AtomNot = AtomAdjust(AtomNot);
|
AtomNot = AtomAdjust(AtomNot);
|
||||||
AtomNotImplemented = AtomAdjust(AtomNotImplemented);
|
AtomNotImplemented = AtomAdjust(AtomNotImplemented);
|
||||||
|
@ -178,6 +178,8 @@
|
|||||||
#define AtomEvaluable Yap_heap_regs->AtomEvaluable_
|
#define AtomEvaluable Yap_heap_regs->AtomEvaluable_
|
||||||
Atom AtomEvaluationError_;
|
Atom AtomEvaluationError_;
|
||||||
#define AtomEvaluationError Yap_heap_regs->AtomEvaluationError_
|
#define AtomEvaluationError Yap_heap_regs->AtomEvaluationError_
|
||||||
|
Atom AtomExecute_;
|
||||||
|
#define AtomExecute Yap_heap_regs->AtomExecute_
|
||||||
Atom AtomExecAnswers_;
|
Atom AtomExecAnswers_;
|
||||||
#define AtomExecAnswers Yap_heap_regs->AtomExecAnswers_
|
#define AtomExecAnswers Yap_heap_regs->AtomExecAnswers_
|
||||||
Atom AtomExecuteInMod_;
|
Atom AtomExecuteInMod_;
|
||||||
@ -186,6 +188,8 @@
|
|||||||
#define AtomExecuteWithin Yap_heap_regs->AtomExecuteWithin_
|
#define AtomExecuteWithin Yap_heap_regs->AtomExecuteWithin_
|
||||||
Atom AtomExecuteWoMod_;
|
Atom AtomExecuteWoMod_;
|
||||||
#define AtomExecuteWoMod Yap_heap_regs->AtomExecuteWoMod_
|
#define AtomExecuteWoMod Yap_heap_regs->AtomExecuteWoMod_
|
||||||
|
Atom AtomExist_;
|
||||||
|
#define AtomExist Yap_heap_regs->AtomExist_
|
||||||
Atom AtomExistenceError_;
|
Atom AtomExistenceError_;
|
||||||
#define AtomExistenceError Yap_heap_regs->AtomExistenceError_
|
#define AtomExistenceError Yap_heap_regs->AtomExistenceError_
|
||||||
Atom AtomExpectedNumber_;
|
Atom AtomExpectedNumber_;
|
||||||
@ -342,6 +346,8 @@
|
|||||||
#define AtomNew Yap_heap_regs->AtomNew_
|
#define AtomNew Yap_heap_regs->AtomNew_
|
||||||
Atom AtomNoMemory_;
|
Atom AtomNoMemory_;
|
||||||
#define AtomNoMemory Yap_heap_regs->AtomNoMemory_
|
#define AtomNoMemory Yap_heap_regs->AtomNoMemory_
|
||||||
|
Atom AtomNone_;
|
||||||
|
#define AtomNone Yap_heap_regs->AtomNone_
|
||||||
Atom AtomNonEmptyList_;
|
Atom AtomNonEmptyList_;
|
||||||
#define AtomNonEmptyList Yap_heap_regs->AtomNonEmptyList_
|
#define AtomNonEmptyList Yap_heap_regs->AtomNonEmptyList_
|
||||||
Atom AtomNot_;
|
Atom AtomNot_;
|
||||||
|
@ -95,10 +95,12 @@ A Eq N "="
|
|||||||
A Error N "error"
|
A Error N "error"
|
||||||
A Evaluable N "evaluable"
|
A Evaluable N "evaluable"
|
||||||
A EvaluationError N "evaluation_error"
|
A EvaluationError N "evaluation_error"
|
||||||
|
A Execute N "execute"
|
||||||
A ExecAnswers N "exec_answers"
|
A ExecAnswers N "exec_answers"
|
||||||
A ExecuteInMod F "$execute_in_mod"
|
A ExecuteInMod F "$execute_in_mod"
|
||||||
A ExecuteWithin F "$execute_within"
|
A ExecuteWithin F "$execute_within"
|
||||||
A ExecuteWoMod F "$execute_wo_mod"
|
A ExecuteWoMod F "$execute_wo_mod"
|
||||||
|
A Exist N "exist"
|
||||||
A ExistenceError N "existence_error"
|
A ExistenceError N "existence_error"
|
||||||
A ExpectedNumber N "expected_number_syntax"
|
A ExpectedNumber N "expected_number_syntax"
|
||||||
A ExtendFileSearchPath F "$extend_file_search_path"
|
A ExtendFileSearchPath F "$extend_file_search_path"
|
||||||
@ -177,6 +179,7 @@ A Nb N "nb"
|
|||||||
A NbTerm N "nb_term"
|
A NbTerm N "nb_term"
|
||||||
A New N "new"
|
A New N "new"
|
||||||
A NoMemory N "no_memory"
|
A NoMemory N "no_memory"
|
||||||
|
A None N "none"
|
||||||
A NonEmptyList N "non_empty_list"
|
A NonEmptyList N "non_empty_list"
|
||||||
A Not N "\\+"
|
A Not N "\\+"
|
||||||
A NotImplemented N "not_implemented"
|
A NotImplemented N "not_implemented"
|
||||||
|
23
pl/boot.yap
23
pl/boot.yap
@ -1120,29 +1120,6 @@ bootstrap(F) :-
|
|||||||
'$do_error'(type_error(callable,H),P).
|
'$do_error'(type_error(callable,H),P).
|
||||||
'$check_head'(_,_).
|
'$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
|
% term expansion
|
||||||
%
|
%
|
||||||
% return two arguments: Expanded0 is the term after "USER" expansion.
|
% return two arguments: Expanded0 is the term after "USER" expansion.
|
||||||
|
Reference in New Issue
Block a user