16 ** Implements all of the File Access word set that can be implemented in portable C.
20 static void pushIor(FICL_VM *pVM, int success)
27 stackPushINT(pVM->pStack, ior);
32 static void ficlFopen(FICL_VM *pVM, char *writeMode) /* ( c-addr u fam -- fileid ior ) */
34 int fam = stackPopINT(pVM->pStack);
35 int length = stackPopINT(pVM->pStack);
36 void *address = (void *)stackPopPtr(pVM->pStack);
40 char *filename = (char *)alloca(length + 1);
41 memcpy(filename, address, length);
46 switch (FICL_FAM_OPEN_MODE(fam))
49 stackPushPtr(pVM->pStack, NULL);
50 stackPushINT(pVM->pStack, EINVAL);
56 strcat(mode, writeMode);
58 case FICL_FAM_READ | FICL_FAM_WRITE:
59 strcat(mode, writeMode);
64 strcat(mode, (fam & FICL_FAM_BINARY) ? "b" : "t");
66 f = fopen(filename, mode);
68 stackPushPtr(pVM->pStack, NULL);
71 ficlFILE *ff = (ficlFILE *)malloc(sizeof(ficlFILE));
72 strcpy(ff->filename, filename);
74 stackPushPtr(pVM->pStack, ff);
76 fseek(f, 0, SEEK_SET);
78 pushIor(pVM, f != NULL);
83 static void ficlOpenFile(FICL_VM *pVM) /* ( c-addr u fam -- fileid ior ) */
89 static void ficlCreateFile(FICL_VM *pVM) /* ( c-addr u fam -- fileid ior ) */
95 static int closeFiclFILE(ficlFILE *ff) /* ( fileid -- ior ) */
102 static void ficlCloseFile(FICL_VM *pVM) /* ( fileid -- ior ) */
104 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
105 pushIor(pVM, closeFiclFILE(ff));
108 static void ficlDeleteFile(FICL_VM *pVM) /* ( c-addr u -- ior ) */
110 int length = stackPopINT(pVM->pStack);
111 void *address = (void *)stackPopPtr(pVM->pStack);
113 char *filename = (char *)alloca(length + 1);
114 memcpy(filename, address, length);
115 filename[length] = 0;
117 pushIor(pVM, !unlink(filename));
120 static void ficlRenameFile(FICL_VM *pVM) /* ( c-addr1 u1 c-addr2 u2 -- ior ) */
127 length = stackPopINT(pVM->pStack);
128 address = (void *)stackPopPtr(pVM->pStack);
129 to = (char *)alloca(length + 1);
130 memcpy(to, address, length);
133 length = stackPopINT(pVM->pStack);
134 address = (void *)stackPopPtr(pVM->pStack);
136 from = (char *)alloca(length + 1);
137 memcpy(from, address, length);
140 pushIor(pVM, !rename(from, to));
143 static void ficlFileStatus(FICL_VM *pVM) /* ( c-addr u -- x ior ) */
147 int length = stackPopINT(pVM->pStack);
148 void *address = (void *)stackPopPtr(pVM->pStack);
150 char *filename = (char *)alloca(length + 1);
151 memcpy(filename, address, length);
152 filename[length] = 0;
154 if (stat(filename, &statbuf) == 0)
157 ** the "x" left on the stack is implementation-defined.
158 ** I push the file's access mode (readable, writeable, is directory, etc)
159 ** as defined by ANSI C.
161 stackPushINT(pVM->pStack, statbuf.st_mode);
162 stackPushINT(pVM->pStack, 0);
166 stackPushINT(pVM->pStack, -1);
167 stackPushINT(pVM->pStack, ENOENT);
172 static void ficlFilePosition(FICL_VM *pVM) /* ( fileid -- ud ior ) */
174 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
175 long ud = ftell(ff->f);
176 stackPushINT(pVM->pStack, ud);
177 pushIor(pVM, ud != -1);
182 static long fileSize(FILE *f)
185 statbuf.st_size = -1;
186 if (fstat(fileno(f), &statbuf) != 0)
188 return statbuf.st_size;
193 static void ficlFileSize(FICL_VM *pVM) /* ( fileid -- ud ior ) */
195 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
196 long ud = fileSize(ff->f);
197 stackPushINT(pVM->pStack, ud);
198 pushIor(pVM, ud != -1);
204 static void ficlIncludeFile(FICL_VM *pVM) /* ( i*x fileid -- j*x ) */
206 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
207 CELL id = pVM->sourceID;
208 int result = VM_OUTOFTEXT;
209 long currentPosition, totalSize;
211 pVM->sourceID.p = (void *)ff;
213 currentPosition = ftell(ff->f);
214 totalSize = fileSize(ff->f);
215 size = totalSize - currentPosition;
217 if ((totalSize != -1) && (currentPosition != -1) && (size > 0))
219 char *buffer = (char *)malloc(size);
220 long got = fread(buffer, 1, size, ff->f);
222 result = ficlExecC(pVM, buffer, size);
226 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
227 CELL id = pVM->sourceID;
232 pVM->sourceID.p = (void *)ff;
234 /* feed each line to ficlExec */
236 while (keepGoing && fgets(cp, nLINEBUF, ff->f))
238 int len = strlen(cp) - 1;
247 result = ficlExec(pVM, cp);
263 ** Pass an empty line with SOURCE-ID == -1 to flush
264 ** any pending REFILLs (as required by FILE wordset)
266 pVM->sourceID.i = -1;
275 static void ficlReadFile(FICL_VM *pVM) /* ( c-addr u1 fileid -- u2 ior ) */
277 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
278 int length = stackPopINT(pVM->pStack);
279 void *address = (void *)stackPopPtr(pVM->pStack);
283 result = fread(address, 1, length, ff->f);
285 stackPushINT(pVM->pStack, result);
286 pushIor(pVM, ferror(ff->f) == 0);
291 static void ficlReadLine(FICL_VM *pVM) /* ( c-addr u1 fileid -- u2 flag ior ) */
293 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
294 int length = stackPopINT(pVM->pStack);
295 char *address = (char *)stackPopPtr(pVM->pStack);
301 stackPushINT(pVM->pStack, -1);
302 stackPushINT(pVM->pStack, 0);
303 stackPushINT(pVM->pStack, 0);
309 fgets(address, length, ff->f);
311 error = ferror(ff->f);
314 stackPushINT(pVM->pStack, -1);
315 stackPushINT(pVM->pStack, 0);
316 stackPushINT(pVM->pStack, error);
320 length = strlen(address);
322 if (length && ((address[length - 1] == '\r') || (address[length - 1] == '\n')))
325 stackPushINT(pVM->pStack, length);
326 stackPushINT(pVM->pStack, flag);
327 stackPushINT(pVM->pStack, 0); /* ior */
332 static void ficlWriteFile(FICL_VM *pVM) /* ( c-addr u1 fileid -- ior ) */
334 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
335 int length = stackPopINT(pVM->pStack);
336 void *address = (void *)stackPopPtr(pVM->pStack);
339 fwrite(address, 1, length, ff->f);
340 pushIor(pVM, ferror(ff->f) == 0);
345 static void ficlWriteLine(FICL_VM *pVM) /* ( c-addr u1 fileid -- ior ) */
347 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
348 size_t length = (size_t)stackPopINT(pVM->pStack);
349 void *address = (void *)stackPopPtr(pVM->pStack);
352 if (fwrite(address, 1, length, ff->f) == length)
353 fwrite("\n", 1, 1, ff->f);
354 pushIor(pVM, ferror(ff->f) == 0);
359 static void ficlRepositionFile(FICL_VM *pVM) /* ( ud fileid -- ior ) */
361 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
362 size_t ud = (size_t)stackPopINT(pVM->pStack);
364 pushIor(pVM, fseek(ff->f, ud, SEEK_SET) == 0);
369 static void ficlFlushFile(FICL_VM *pVM) /* ( fileid -- ior ) */
371 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
372 pushIor(pVM, fflush(ff->f) == 0);
377 #if FICL_HAVE_FTRUNCATE
379 static void ficlResizeFile(FICL_VM *pVM) /* ( ud fileid -- ior ) */
381 ficlFILE *ff = (ficlFILE *)stackPopPtr(pVM->pStack);
382 size_t ud = (size_t)stackPopINT(pVM->pStack);
384 pushIor(pVM, ftruncate(fileno(ff->f), ud) == 0);
387 #endif /* FICL_HAVE_FTRUNCATE */
389 #endif /* FICL_WANT_FILE */
393 void ficlCompileFile(FICL_SYSTEM *pSys)
396 FICL_DICT *dp = pSys->dp;
399 dictAppendWord(dp, "create-file", ficlCreateFile, FW_DEFAULT);
400 dictAppendWord(dp, "open-file", ficlOpenFile, FW_DEFAULT);
401 dictAppendWord(dp, "close-file", ficlCloseFile, FW_DEFAULT);
402 dictAppendWord(dp, "include-file", ficlIncludeFile, FW_DEFAULT);
403 dictAppendWord(dp, "read-file", ficlReadFile, FW_DEFAULT);
404 dictAppendWord(dp, "read-line", ficlReadLine, FW_DEFAULT);
405 dictAppendWord(dp, "write-file", ficlWriteFile, FW_DEFAULT);
406 dictAppendWord(dp, "write-line", ficlWriteLine, FW_DEFAULT);
407 dictAppendWord(dp, "file-position", ficlFilePosition, FW_DEFAULT);
408 dictAppendWord(dp, "file-size", ficlFileSize, FW_DEFAULT);
409 dictAppendWord(dp, "reposition-file", ficlRepositionFile, FW_DEFAULT);
410 dictAppendWord(dp, "file-status", ficlFileStatus, FW_DEFAULT);
411 dictAppendWord(dp, "flush-file", ficlFlushFile, FW_DEFAULT);
413 dictAppendWord(dp, "delete-file", ficlDeleteFile, FW_DEFAULT);
414 dictAppendWord(dp, "rename-file", ficlRenameFile, FW_DEFAULT);
416 #ifdef FICL_HAVE_FTRUNCATE
417 dictAppendWord(dp, "resize-file", ficlResizeFile, FW_DEFAULT);
419 ficlSetEnv(pSys, "file", FICL_TRUE);
420 ficlSetEnv(pSys, "file-ext", FICL_TRUE);
421 #endif /* FICL_HAVE_FTRUNCATE */
424 #endif /* FICL_WANT_FILE */