From c081d01e78b2b6c2bc8e4b0b17cb412c1897ccb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 3 Aug 2010 21:04:16 +0100 Subject: [PATCH] implement access_file/2 in C --- C/iopreds.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ H/iatoms.h | 3 +++ H/ratoms.h | 3 +++ H/tatoms.h | 6 +++++ misc/ATOMS | 3 +++ pl/boot.yap | 23 ------------------ 6 files changed, 85 insertions(+), 23 deletions(-) diff --git a/C/iopreds.c b/C/iopreds.c index 55a8911de..931780199 100755 --- a/C/iopreds.c +++ b/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 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); diff --git a/H/iatoms.h b/H/iatoms.h index f21574333..5b4c31bcf 100644 --- a/H/iatoms.h +++ b/H/iatoms.h @@ -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"); diff --git a/H/ratoms.h b/H/ratoms.h index a54066633..cc12ef29d 100644 --- a/H/ratoms.h +++ b/H/ratoms.h @@ -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); diff --git a/H/tatoms.h b/H/tatoms.h index 05ed9eacf..1c2e21f15 100644 --- a/H/tatoms.h +++ b/H/tatoms.h @@ -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_; diff --git a/misc/ATOMS b/misc/ATOMS index e2d951528..010272636 100644 --- a/misc/ATOMS +++ b/misc/ATOMS @@ -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" diff --git a/pl/boot.yap b/pl/boot.yap index 267194011..4a45b263b 100755 --- a/pl/boot.yap +++ b/pl/boot.yap @@ -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.