int prPipeOpen(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *c; char mode[12]; PyrFile *pfile; FILE *file; a = g->sp - 2; b = g->sp - 1; c = g->sp; if (NotObj(c) || !isKindOf(slotRawObject(c), class_string) || NotObj(b) || !isKindOf(slotRawObject(b), class_string)) return errWrongType; if (slotRawObject(c)->size > 11) return errFailed; pfile = (PyrFile*)slotRawObject(a); char *commandLine = (char*)malloc(slotRawObject(b)->size + 1); memcpy(commandLine, slotRawString(b)->s, slotRawObject(b)->size); commandLine[slotRawString(b)->size] = 0; memcpy(mode, slotRawString(c)->s, slotRawObject(c)->size); mode[slotRawString(c)->size] = 0; pid_t pid; file = sc_popen(commandLine, &pid, mode); free(commandLine); if (file) { SetPtr(&pfile->fileptr, file); SetInt(a, pid); } else { SetNil(a); } return errNone; }
int prSignalOverWrite(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *c; int err; int index; a = g->sp - 2; b = g->sp - 1; c = g->sp; if (NotObj(b) || !isKindOf(slotRawObject(b), class_signal)) return errWrongType; err = slotIntVal(c, &index); if (err) return errWrongType; signal_overwrite(g, slotRawObject(a), slotRawObject(b), index); return errNone; }
int prFilePutString(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; PyrFile *pfile; FILE *file; PyrString *string; a = g->sp - 1; b = g->sp; pfile = (PyrFile*)slotRawObject(a); file = (FILE*)slotRawPtr(&pfile->fileptr); if (file == NULL) return errFailed; if (NotObj(b) || slotRawObject(b)->classptr != class_string) return errWrongType; string = slotRawString(b); if (string->size) { fwrite(string->s, 1, string->size, file); } return errNone; }
inline bool NotSignal(PyrSlot* slot) { return (NotObj(slot) || slotRawObject(slot)->classptr != class_signal); }
int prFileOpen(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *c; char filename[PATH_MAX]; char mode[12]; PyrFile *pfile; FILE *file; a = g->sp - 2; b = g->sp - 1; c = g->sp; if (NotObj(c) || !isKindOf(slotRawObject(c), class_string) || NotObj(b) || !isKindOf(slotRawObject(b), class_string)) return errWrongType; if (slotRawObject(b)->size > PATH_MAX - 1) return errFailed; if (slotRawObject(c)->size > 11) return errFailed; pfile = (PyrFile*)slotRawObject(a); memcpy(filename, slotRawString(b)->s, slotRawObject(b)->size); filename[slotRawString(b)->size] = 0; memcpy(mode, slotRawString(c)->s, slotRawObject(c)->size); mode[slotRawString(c)->size] = 0; #ifdef SC_WIN32 win32_ReplaceCharInString(filename,PATH_MAX,'/','\\'); if(strcmp(mode,"w") == 0) strcpy(mode,"wb"); if(strcmp(mode,"r") == 0) strcpy(mode,"rb"); #endif //SC_WIN32 file = fopen(filename, mode); if (file) { SetPtr(&pfile->fileptr, file); SetTrue(a); } else { #ifdef SC_WIN32 // check if directory exisits // create a temporary file (somewhere) for a handle // the file is deleted automatically when closed if (sc_DirectoryExists(filename)) { int err; #ifdef _MSC_VER err = tmpfile_s(&file); if (!err) { SetPtr(&pfile->fileptr, file); SetTrue(a); return errNone; } #elif defined(__MINGW32__) file = tmpfile(); if (file) { SetPtr(&pfile->fileptr, file); SetTrue(a); return errNone; } #else #error compiler unsupported #endif } #endif SetNil(a); SetFalse(a); } return errNone; }