static Int peek_mem_write_stream ( USES_REGS1 ) { /* '$peek_mem_write_stream'(+GLOBAL_Stream,?S0,?S) */ Int sno = Yap_CheckStream (ARG1, (Output_Stream_f | InMemory_Stream_f), "close/2"); Int i; Term tf = ARG2; CELL *HI; const char *ptr; if (sno < 0) return (FALSE); restart: HI = HR; #if MAY_WRITE if (fflush(GLOBAL_Stream[sno].file) == 0) { ptr = GLOBAL_Stream[sno].nbuf; i = GLOBAL_Stream[sno].nsize; } #else ptr = GLOBAL_Stream[sno].u.mem_string.buf; i = GLOBAL_Stream[sno].u.mem_string.pos; #endif while (i > 0) { --i; tf = MkPairTerm(MkIntTerm(ptr[i]),tf); if (HR + 1024 >= ASP) { UNLOCK(GLOBAL_Stream[sno].streamlock); HR = HI; if (!Yap_gcl((ASP-HI)*sizeof(CELL), 3, ENV, Yap_gcP()) ) { UNLOCK(GLOBAL_Stream[sno].streamlock); Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); return(FALSE); } i = GLOBAL_Stream[sno].u.mem_string.pos; tf = ARG2; LOCK(GLOBAL_Stream[sno].streamlock); goto restart; } } UNLOCK(GLOBAL_Stream[sno].streamlock); return (Yap_unify(ARG3,tf)); }
static Int read_stream_to_codes(USES_REGS1) { int sno = Yap_CheckStream(ARG1, Input_Stream_f, "reaMkAtomTerm (AtomEofd_line_to_codes/2"); CELL *HBASE = HR; CELL *h0 = &ARG4; if (sno < 0) return FALSE; while (!(GLOBAL_Stream[sno].status & Eof_Stream_f)) { /* skip errors */ Int ch = GLOBAL_Stream[sno].stream_getc(sno); Term t; if (ch == EOFCHAR) break; t = MkIntegerTerm(ch); h0[0] = AbsPair(HR); *HR = t; HR += 2; h0 = HR - 1; yhandle_t news, news1, st = Yap_StartSlots(); if (HR >= ASP - 1024) { RESET_VARIABLE(h0); news = Yap_InitSlot(AbsPair(HBASE)); news1 = Yap_InitSlot((CELL)(h0)); if (!Yap_gcl((ASP - HBASE) * sizeof(CELL), 3, ENV, Yap_gcP())) { Yap_Error(RESOURCE_ERROR_STACK, ARG1, "read_stream_to_codes/3"); return false; } /* build a legal term again */ h0 = (CELL *)(Yap_GetFromSlot(news1)); HBASE = RepPair(Yap_GetFromSlot(news)); } Yap_CloseSlots(st); } UNLOCK(GLOBAL_Stream[sno].streamlock); if (HR == HBASE) return Yap_unify(ARG2, ARG3); RESET_VARIABLE(HR - 1); Yap_unify(HR[-1], ARG3); return Yap_unify(AbsPair(HBASE), ARG2); }
static Int same_file(USES_REGS1) { char *f1 = RepAtom(AtomOfTerm(Deref(ARG1)))->StrOfAE; char *f2 = RepAtom(AtomOfTerm(Deref(ARG2)))->StrOfAE; if (strcmp(f1, f2) == 0) return TRUE; #if HAVE_LSTAT { int out; struct stat *b1, *b2; while ((char *)HR + sizeof(struct stat) * 2 > (char *)(ASP - 1024)) { if (!Yap_gcl(2 * sizeof(struct stat), 2, ENV, Yap_gcP())) { Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage); return FALSE; } } b1 = (struct stat *)HR; b2 = b1 + 1; if (strcmp(f1, "user_input") == 0) { if (fstat(fileno(GLOBAL_Stream[0].file), b1) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (strcmp(f1, "user_output") == 0) { if (fstat(fileno(GLOBAL_Stream[1].file), b1) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (strcmp(f1, "user_error") == 0) { if (fstat(fileno(GLOBAL_Stream[2].file), b1) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (stat(f1, b1) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } if (strcmp(f2, "user_input") == 0) { if (fstat(fileno(GLOBAL_Stream[0].file), b2) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (strcmp(f2, "user_output") == 0) { if (fstat(fileno(GLOBAL_Stream[1].file), b2) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (strcmp(f2, "user_error") == 0) { if (fstat(fileno(GLOBAL_Stream[2].file), b2) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } } else if (stat(f2, b2) == -1) { /* file does not exist, but was opened? Return -1 */ return FALSE; } out = (b1->st_ino == b2->st_ino #ifdef __LCC__ && memcmp((const void *)&(b1->st_dev), (const void *)&(b2->st_dev), sizeof(buf1.st_dev)) == 0 #else && b1->st_dev == b2->st_dev #endif ); return out; } #else return (FALSE); #endif }