This commit is contained in:
Vitor Santos Costa
2018-02-14 10:37:07 +00:00
parent 311e1d697d
commit 76d0d54a34
4 changed files with 146 additions and 141 deletions

View File

@@ -165,7 +165,7 @@ static const char *PlExpandVars(const char *source, const char *root) {
CACHE_REGS
int lvl = push_text_stack();
const char *src = source;
char * result = Malloc(YAP_FILENAME_MAX + 1);
char *result = Malloc(YAP_FILENAME_MAX + 1);
if (strlen(source) >= YAP_FILENAME_MAX) {
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil,
@@ -344,13 +344,13 @@ char virtual_cwd[YAP_FILENAME_MAX + 1];
bool Yap_ChDir(const char *path) {
bool rc = false;
int lvl = push_text_stack();
int lvl = push_text_stack();
VFS_t *v;
if ((v = vfs_owner(path))) {
rc = v->chdir(v, path);
pop_text_stack(lvl);
return rc;
rc = v->chdir(v, path);
pop_text_stack(lvl);
return rc;
}
const char *qpath = Yap_AbsoluteFile(path, true);
#if _WIN32
@@ -362,94 +362,90 @@ bool Yap_ChDir(const char *path) {
#else
rc = (chdir(qpath) == 0);
#endif
pop_text_stack(lvl);
pop_text_stack(lvl);
return rc;
}
static char * close_path(char *b0,char *o0, char *o ){
static char *close_path(char *b0, char *o0, char *o) {
if (b0[0] == '\0') {
if (b0[0] == '\0') {
return o;
} else if (!strcmp(b0, "..")) {
while (o-- > o0) {
if (dir_separator(*o)) {
} else if (!strcmp(b0, "..")) {
while (o-- > o0) {
if (dir_separator(*o)) {
break;
}
}
}
} else if (strcmp(b0, ".") != 0) {
} else if (strcmp(b0, ".") != 0) {
*o++ = '/';
strcpy(o, b0);
o += strlen(b0);
}
return o;
}
return o;
}
static char * clean_path(const char *path)
{
const char *p, *p0;
int lvl = push_text_stack();
static char *clean_path(const char *path) {
const char *p, *p0;
int lvl = push_text_stack();
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " looking at %s", path) ;
char *o0 = Malloc(FILENAME_MAX+1),*o = o0;
int ch;
char *b0 = Malloc(FILENAME_MAX+1), *b = b0;
p = p0 = path;
while((ch = *p++)) {
if (dir_separator(ch)) {
if (b==b0) {
o = o0;
} else {
b[0] = '\0';
o = close_path(b0, o0, o);
b = b0;
}
} else {
*b++ = ch;
}
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " looking at %s", path);
char *o0 = Malloc(FILENAME_MAX + 1), *o = o0;
int ch;
char *b0 = Malloc(FILENAME_MAX + 1), *b = b0;
p = p0 = path;
while ((ch = *p++)) {
if (dir_separator(ch)) {
if (b == b0) {
o = o0;
} else {
b[0] = '\0';
o = close_path(b0, o0, o);
b = b0;
}
} else {
*b++ = ch;
}
if (!dir_separator(p[-1])) {
b[0] = '\0';
o = close_path(b0, o0, o);
}
if (o == o0)
*o++ = '/';
*o = '\0';
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " %s at %s, %p-%p", p0, o0, o, o0) ;
return pop_output_text_stack(lvl,o0);
}
if (!dir_separator(p[-1])) {
b[0] = '\0';
o = close_path(b0, o0, o);
}
if (o == o0)
*o++ = '/';
*o = '\0';
__android_log_print(ANDROID_LOG_INFO, "YAPDroid ", " %s at %s, %p-%p", p0, o0,
o, o0);
return pop_output_text_stack(lvl, o0);
}
static const char *myrealpath(const char *path USES_REGS) {
int lvl = push_text_stack();
VFS_t *v;
char *out, *o;
if (Yap_IsAbsolutePath(path,true)) {
o = clean_path(path);
if ((v = vfs_owner(o))
) {
return pop_output_text_stack(lvl, o);
char *out, *o;
if (Yap_IsAbsolutePath(path, true)) {
o = clean_path(path);
return pop_output_text_stack(lvl, o);
}
} else {
out = Malloc(FILENAME_MAX+1);
} else {
out = Malloc(FILENAME_MAX + 1);
Yap_getcwd(out, FILENAME_MAX);
strcat(out, "/");
strcat(out, path);
o = clean_path(out);
if ((v = vfs_owner(o))) {
return pop_output_text_stack(lvl, o);
}
}
if ((v = vfs_owner(o))) {
return pop_output_text_stack(lvl, o);
}
}
#if _WIN32
DWORD retval = 0;
// notice that the file does not need to exist
retval = GetFullPathName(path, YAP_FILENAME_MAX, o, NULL);
if (retval == 0) {
pop_text_stack(lvl);
Yap_WinError("Generating a full path name for a file");
pop_text_stack(lvl);
Yap_WinError("Generating a full path name for a file");
return NULL;
}
return pop_output_text_stack(lvl, o);
@@ -458,25 +454,30 @@ char *out, *o;
char *rc = realpath(path, o);
if (rc) {
rc = pop_output_text_stack(lvl, rc);
return pop_output_text_stack(lvl, rc);
}
// rc = NULL;
if (errno == ENOENT || errno == EACCES) {
char *base = Malloc(FILENAME_MAX + 1);
strncpy(base, path, FILENAME_MAX);
char *p = base+strlen(base);
while (p>base && !dir_separator(*--p));
if (p == base) p[1] = '\0';
else p[0] = '\0';
char *tmp = Malloc(FILENAME_MAX + 1);
char *p = base + strlen(base);
while (p > base && !dir_separator(*--p))
;
if (p == base)
p[1] = '\0';
else
p[0] = '\0';
char *tmp = Malloc(FILENAME_MAX + 1);
rc = realpath(base, tmp);
if (rc) {
// base may have been destroyed
char *b = base+strlen(base);
while (b>base && !dir_separator(*--b));
if (b[0] && !dir_separator(b[0])) b++;
size_t e = strlen(rc);
char *b = base + strlen(base);
while (b > base && !dir_separator(*--b))
;
if (b[0] && !dir_separator(b[0]))
b++;
size_t e = strlen(rc);
size_t bs = strlen(b);
if (rc != out && rc != base) {
@@ -536,7 +537,7 @@ const char *Yap_AbsoluteFile(const char *spec, bool ok) {
const char *rc;
const char *spec1;
const char *spec2;
int lvl = push_text_stack();
int lvl = push_text_stack();
/// spec gothe original spec;
/// rc0 may be an outout buffer
@@ -565,7 +566,7 @@ const char *Yap_AbsoluteFile(const char *spec, bool ok) {
spec2 = spec1;
}
rc = myrealpath(spec2 PASS_REGS);
return pop_output_text_stack(lvl,rc);
return pop_output_text_stack(lvl, rc);
}
static Term
@@ -754,15 +755,15 @@ static Int real_path(USES_REGS1) {
}
cmd = rc;
#endif
int lvl = push_text_stack();
int lvl = push_text_stack();
rc0 = myrealpath(cmd PASS_REGS);
if (!rc0) {
PlIOError(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, NULL);
}
bool out = Yap_unify(MkAtomTerm(Yap_LookupAtom(rc0)), ARG2);
pop_output_text_stack(lvl,rc0);
pop_output_text_stack(lvl, rc0);
return out;
return out;
}
#define EXPAND_FILENAME_DEFS() \
@@ -1163,8 +1164,8 @@ static Int true_file_name(USES_REGS1) {
Term t = Deref(ARG1);
const char *s;
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE;
}
if (IsAtomTerm(t)) {
@@ -1172,14 +1173,14 @@ static Int true_file_name(USES_REGS1) {
} else if (IsStringTerm(t)) {
s = StringOfTerm(t);
} else {
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
Yap_Error(TYPE_ERROR_ATOM, t, "argument to true_file_name");
return FALSE;
}
int l = push_text_stack();
int l = push_text_stack();
if (!(s = Yap_AbsoluteFile(s, true)))
return false;
bool rc = Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(s)));
pop_text_stack(l);
pop_text_stack(l);
return rc;
}
@@ -1207,30 +1208,29 @@ static Int p_expand_file_name(USES_REGS1) {
}
static Int true_file_name3(USES_REGS1) {
Term t = Deref(ARG1), t2 = Deref(ARG2);
Term t = Deref(ARG1), t2 = Deref(ARG2);
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR, t, "argument to true_file_name unbound");
return FALSE;
}
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, t, "argument to true_file_name");
return FALSE;
Yap_Error(TYPE_ERROR_ATOM, t2, "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;
}
int lvl = push_text_stack();
const char *tmp = Yap_AbsoluteFile(RepAtom(AtomOfTerm(t))->StrOfAE, true);
Atom at = NULL;
bool rc = (tmp != NULL &&
(at = Yap_LookupAtom(tmp)) != NULL);
pop_text_stack(lvl);
return rc && Yap_unify(ARG3, MkAtomTerm(at));
// root = RepAtom(AtomOfTerm(t2))->StrOfAE;
}
int lvl = push_text_stack();
const char *tmp = Yap_AbsoluteFile(RepAtom(AtomOfTerm(t))->StrOfAE, true);
Atom at = NULL;
bool rc = (tmp != NULL && (at = Yap_LookupAtom(tmp)) != NULL);
pop_text_stack(lvl);
return rc && Yap_unify(ARG3, MkAtomTerm(at));
}
/* Executes $SHELL under Prolog */